The program requested must have a text menu like this: 0 - Exit 1 - Select file
ID: 3624787 • Letter: T
Question
The program requested must have a text menu like this:0 - Exit
1 - Select file
2 - Display
4 - To upper case
5 - To lower case
Select option:
The menu is displayed and the user must select an option (a number between 0 and 5). The action corresponding to the selection is performed, then the menu is displayed again and the user can choose another option. This cycle is repeated until the user selects 0, which exits the loop and ends the program. We may assume we are working with text files only. The maximum length of the file is 20000 (tewnty thousand) characters. All your code must be in one single file, say "p2.java". The executable should be named "p2".
The options are:
0 - Exit
This options ends the program
1 - Select file
The user is prompted for a file name. This is the first options that must be selected by the user. All the options below are working on the file selected here. After performing several operations on the selected file, the user can select another file and work on it.
2 - Display
This option displays the content of the selected file on the screen. If no file was selected an error message is displayed.
3 - Rename
The user is prompted for a new name for the selected file, then the file is renamed. If no file was selected an error message is displayed.
4 - To upper case
The file is read into memory, all of it in one buffer, all the characters are converted to UPPER CASE, then the file is overwritten. If no file was selected an error message is displayed.
5 - To lower case
The file is read into memory, all of it in one buffer, all the characters are converted to lower case, then the file is overwritten. If no file was selected an error message is displayed.
Good Luck!
PS: the options should work correctly, no matter the order of the operations chosen from the menu.
PPS: processing the file one character at a time is _extremely_ inefficient and therefore it is forbidden (getchar(), putchar() and the like are forbidden).
Explanation / Answer
please rate - thanks
import java.util.*;
import java.io.*;
import java.io.FileOutputStream;
public class p2
{public static void main(String[] args)throws FileNotFoundException,IOException
{int choice;
File F=new File(" ");
Scanner in=new Scanner(System.in);
choice=menu(in);
while( choice!=1&&choice!=0)
{System.out.println("1 must be the first choice!");
choice=menu(in);
}
do
{switch(choice)
{case 0: System.exit(0);
case 1: F=select(F,in);
break;
case 2: display(F);
break;
case 3: F=newname(F,in);
break;
case 4: upper(F);
break;
case 5: lower(F);
}
choice=menu(in);
}while(choice!=0);
}
public static void display(File f)throws FileNotFoundException
{Scanner input=new Scanner(f);
String buffer;
while(input.hasNext())
{buffer=input.nextLine();
System.out.println(buffer);
}
}
public static File newname(File f,Scanner in)
{String filename;
System.out.print("Enter new filename: ");
filename=in.next();
File newname=new File(filename);
f.renameTo(newname);
return f;
}
public static void upper(File f)throws FileNotFoundException,IOException
{FileInputStream in = new FileInputStream( f );
int len = (int) f.length();
byte buf[] = new byte[len];
char cbuf ;
int offset = 0;
int rc = 0;
while( offset < len ) {
rc = in.read( buf, offset, len - offset );
offset += rc;
}
in.close();
for(int i=0;i<len;i++)
{cbuf=Character.toUpperCase((char)buf[i]);
buf[i]=(byte)cbuf;
}
rc = 0;
OutputStream oldOut = getNewOutputStream();
FileOutputStream newOut=new FileOutputStream(f);
oldOut.write(buf);
newOut.write(buf);
oldOut.close();
newOut.close();
}
public static OutputStream getNewOutputStream(){
OutputStream out = new ByteArrayOutputStream(2000);
return out;
}
public static void lower(File f)throws FileNotFoundException,IOException
{FileInputStream in = new FileInputStream( f );
int len = (int) f.length();
byte buf[] = new byte[len];
char cbuf ;
int offset = 0;
int rc = 0;
while( offset < len ) {
rc = in.read( buf, offset, len - offset );
offset += rc;
}
in.close();
for(int i=0;i<len;i++)
{cbuf=Character.toLowerCase((char)buf[i]);
buf[i]=(byte)cbuf;
}
rc = 0;
OutputStream oldOut = getNewOutputStream();
FileOutputStream newOut=new FileOutputStream(f);
oldOut.write(buf);
newOut.write(buf);
oldOut.close();
newOut.close();
}
public static File select(File f,Scanner in)
{String filename;
System.out.print("Enter filename: ");
filename=in.next();
f=new File(filename);
return f;
}
public static int menu(Scanner in)
{int n;
do
{System.out.println(" MENU");
System.out.println("0 - Exit ");
System.out.println("1 - Select file ");
System.out.println("2 - Display ");
System.out.println("3 - Rename ");
System.out.println("4 - To upper case ");
System.out.println("5 - To lower case ");
System.out.println("What would you like to do? ");
n=in.nextInt();
if(n<0||n>5)
System.out.println("Invalid entry");
}while(n<0||n>5);
return n;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.