Hi all,
I'm trying to write a fairly simple program in Java that will allow the user to write to a text file, specifiying the File name and Content.
I keep getting the same error though that comes up with 'Variable [Variable name] may not be initialized' I've struggled for ages now but can't find the solution
Any help would be greatly appreciated! Sorry about the lack of comments.
P.S the variables that are causing the errors are 'fileName' and 'inputText'
I'm trying to write a fairly simple program in Java that will allow the user to write to a text file, specifiying the File name and Content.
I keep getting the same error though that comes up with 'Variable [Variable name] may not be initialized' I've struggled for ages now but can't find the solution
Any help would be greatly appreciated! Sorry about the lack of comments.
Code:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class FileMakerApp extends Object
{
public static void main(String[] argStrings) throws Exception
{
String lnBreak = ("----------------------------------");
int menu = 0;
do
{
Scanner choice = new Scanner(System.in);
System.out.print(" What would you now like to do? ");
System.out.println(lnBreak);
System.out.println("1: Create new file");
System.out.println("2: View name of last file created");
System.out.println("3: View content of last file created");
System.out.println("4: View location of last file created");
System.out.println("5: Exit Program");
System.out.println(lnBreak);
menu = choice.nextInt();
switch (menu)
{
case 1:
System.out.print(" Please insert the name of the text file (E.G input.txt)you wish to create: ");
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
PrintStream out = new PrintStream(fileName);
System.out.print(" Please insert the text you wish to be saved to the file and press 'enter': ");
Scanner inputs = new Scanner(System.in);
String inputText = inputs.nextLine();
out.println(inputText);
out.close();
case 2:
System.out.println();
System.out.println("File called: " + fileName + ".txt");
System.out.println();
break;
case 3:
System.out.println();
System.out.println("File content:");
System.out.println();
System.out.println(inputText);
System.out.println();
System.out.println(lnBreak);
break;
case 4:
System.out.println();
System.out.println("File located at: D:\\files\\2\\classes");
System.out.println();
break;
case 5:
System.out.println();
System.out.println("Program Successfully Exited");
System.out.println();
default:
System.out.println();
System.out.println("No such option. Please try again.");
System.out.println();
break;
}
}
while (menu !=5);
}
}
P.S the variables that are causing the errors are 'fileName' and 'inputText'
Comment