Assignment 4
Assignment 4
1. Write a java program to accept the data from a user and write it into the file.
import java.io.*;
import java.util.*;
class demo
{
public static void main(String args[])throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println("enter data to insert into file");
String str;
str=sc.nextLine();
FileWriter fw=new FileWriter("a.txt");
fw.write(str);
System.out.println("data inserted successfully..");
fw.close();
}
}
Output:
2. Write a java program to display ASCII values of the characters from a file.
import java.io.*;
import java.util.*;
class demo
{
public static void main(String args[])throws Exception
{
fin.close();
}
}
Output:
3. Write a java program to count number of digits, spaces and characters from a file.
import java.io.*;
import java.util.*;
class demo
{
public static void main(String args[])throws Exception
{
Set A Q.5
5. Write a java program to accept a number from user, If it is greater than 100 then throw user
defined exception “Number is out of Range” otherwise do the addition of digits of that number.
(Use static keyword)
import java.util.*;
import java.lang.*;
class demo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no");
int rem,no,sum;
no=sc.nextInt();
try
{
if(no>100)
{
throw new MyException();
}
else
{
sum=0;
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
System.out.println("sum of digit="+sum);
}
}
catch(MyException e)
{
System.out.println(e.toString());
}
}
}
class MyException extends Exception
{
MyException()
{
}
public String toString()
{
return "number is out of range";
}
}
Output1:
Output2:
import java.util.*;
import java.lang.*;
class demo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no");
int rem,no,sum,temp;
no=sc.nextInt();
temp=no;
try
{
if(no<=0)
{
throw new MyException();
}
else
{
sum=0;
while(no>0)
{
rem=no%10;
sum=sum+rem*rem*rem;
no=no/10;
}
if(sum==temp)
{
System.out.println("no is armstrong");
}
else
{
System.out.println("no is not armstrong");
}
}
}
catch(MyException e)
{
System.out.println(e.toString());
}
}
}
class MyException extends Exception
{
MyException()
{
}
public String toString()
{
return "no is invalid";
}
}
Outputs:
setA 4. Write a java program to accept a number from a user, if it is zero then throw user
defined Exception “Number is Zero”. If it is non-numeric then generate an error “Number is
Invalid” otherwise check whether it is palindrome or not.
import java.util.*;
import java.lang.*;
class demo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no");
int rem,no,rev,temp;
try
{
no=sc.nextInt();
temp=no;
if(no==0)
{
throw new ZeroException();
}
else
{
rev=0;
while(no>0)
{
rem=no%10;
rev=rev*10+rem;
no=no/10;
}
if(rev==temp)
{
System.out.println("no is palindrom");
}
else
{
System.out.println("no is not palindrom");
}
}
}
catch(ZeroException e)
{
System.out.println(e.toString());
}
catch(InputMismatchException e)
{
System.out.println("invalid no");
}
}
}
class ZeroException extends Exception
{
ZeroException()
{
}
public String toString()
{
return "no is Zero";
}
}
Outputs:
import java.io.*;
class demo
{
public static void main(String args[])throws Exception
{
int i;
for(i=0;i<args.length;i++)
{
File f=new File(args[i]);
String extension;
int index = args[i].lastIndexOf('.');
extension = args[i].substring(index + 1);
if(extension.equals("txt"))
{
f.delete();
}
else
{
System.out.println("Name of file="+f.getName());
System.out.println("location of file="+f.getPath());
System.out.println("size of file="+f.length());
}
}
}
}
Output:
Set b
1.
import java.io.*;
import java.util.*;
class demo
{
public static void main(String args[])throws Exception
{
Console:
2.
import java.io.*;
import java.util.*;
class demo
{
public static void main(String args[])throws Exception
{
String str;
int i;
FileOutputStream fout=new FileOutputStream("b.txt");
int value;
System.out.println("enter data to write in file");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
for(i=0;i<str.length();i++)
{
value=(int)str.charAt(i);
fout.write(value);
}
System.out.println("file write successfully....");
fout.close();
}
}
Output: