Variable may not have been initialzed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FooFan
    New Member
    • Dec 2007
    • 2

    #1

    Variable may not have been initialzed

    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.

    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'
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Suppose the user first types '2' (check it in your source code); obviously variable
    'fileName' isn't initialized yet. That's why the compiler is complaining.

    kind regards,

    Jos

    Comment

    • FooFan
      New Member
      • Dec 2007
      • 2

      #3
      Ah I see...

      I've now taken the file writing part of the code out of the Do.. while loop, but can't think of a way to enable a 'create new file' option in the menu that will allow the other menu options to be kept updated.

      I tried repeating the code as a case 5, but just got the 'variable is already defined' error.

      The program runs fine without it, but it would've been a nice feature.

      Thanks for your help.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Declaring variables in switch cases is going to get you into trouble. With switch statements I recommend keeping the code in each case as simple as possible, say just call a subroutine method you've defined:
        [CODE=java]
        switch (menu) {
        case 1:
        createNewFile() ;
        break;
        case 2:
        viewLastFilenam e();
        break;
        ...
        }
        [/CODE]
        You can either pass arguments to these subroutines and return values, or use the state of the current object.

        Comment

        Working...