0% found this document useful (0 votes)
163 views

Assignment 4

This document contains several code examples that demonstrate reading and writing files in Java. The first example shows how to accept user input and write it to a file. The second displays the ASCII values of characters read from a file. The third counts the number of digits, spaces, and characters in a file. The fourth handles exceptions when reading a number from the user and checks if it is Armstrong. The fifth also uses exceptions to check if a number is zero, non-numeric, or a palindrome.

Uploaded by

Shubham Harpude
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Assignment 4

This document contains several code examples that demonstrate reading and writing files in Java. The first example shows how to accept user input and write it to a file. The second displays the ASCII values of characters read from a file. The third counts the number of digits, spaces, and characters in a file. The fourth handles exceptions when reading a number from the user and checks if it is Armstrong. The fifth also uses exceptions to check if a number is zero, non-numeric, or a palindrome.

Uploaded by

Shubham Harpude
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practice set

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
{

FileInputStream fin=new FileInputStream("a.txt");


int value;
value=fin.read();
System.out.println("character and their ASCII value");
while(value!=-1)
{
System.out.println((char)value+"="+value);
value=fin.read();
}

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
{

FileInputStream fin=new FileInputStream("a.txt");


int value,digit=0,cnt=0,space=0;
value=fin.read();
char ch;
System.out.println("content of file");
while(value!=-1)
{
ch=(char)value;
if(ch==' ')
{
space++;
}
else if(ch>='0' && ch<='9')
{
digit++;
}
else
{
cnt++;
}
System.out.print((char)value);
value=fin.read();
}
System.out.println("");
System.out.println("total no of character in file"+cnt);
System.out.println("total no of digit in file"+digit);
System.out.println("total no of space in file"+space);
fin.close();
}
}
Output:

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:

Practice set Q.4


4. Write a java program to accept a number from user if it is non-zero then check whether it is
Armstrong or not, otherwise throws user defined Exception “Number is Invalid”.

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
{

FileInputStream fin=new FileInputStream("a.txt");


FileOutputStream fout=new FileOutputStream("b.txt");
int value;
value=fin.read();
char ch;
while(value!=-1)
{
ch=(char)value;
if(ch>='0' && ch<='9')
{
ch='*';
}
else if(Character.isUpperCase(ch))
{
ch=Character.toLowerCase(ch);
}
else
{
ch=Character.toUpperCase(ch);
}
fout.write(ch);
value=fin.read();
}
System.out.println("file copy successfully....");
fout.close();
fin.close();
}
}
Output :

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:

You might also like