#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    57
    Rep Power
    2

    Switch Statement With FILE IO Help


    Ok, so i am having issues with IO and a switch statement. When a user selects an option to do (1 = read file), then it runs the method for reading in a file, but once it reads the text in the File, the code terminates itself rather than bringing up the menu again.

    This is the structure...

    Main Menu - 1) Calculations 2) Files 3)Quit.

    If user types 1, then another menu is brought up in the Calculation section, that will allow users to do some Calculations... (WORKS)

    If user types 2..
    Files Menu - 1) Open File 2) Create File 3) Back.

    -Now if the user selects 1, 2 or 3 then the method will run and it works, however it terminates the code. So if the user types 3 then the code will output some code, then quit, rather than going back to the Main Menu like Calculations.

    - So all of the methods work and run correctly. It read in the file, and create / write to the file. It just do not keep the program going. It should bring me back to the MAIN MENU. It Calculations Sections does the same thing and it works. (It doesnt read in a file, but it brings up a menu...)

    Code:
    public void PrintMenu() 	{ 		 		
    System.out.println("Here is where you can check out ALL classes in a text file. \nPlease Select an Option: \n" + 				
    "1) Open a File to View \n2) Update a File (Not Coded) \n3) Create New File \n4) Back to Main Menu"); 		
    int menu = scan.nextInt();  		 		
    switch(menu){ 		
    case 1: 			
    scan.nextLine(); 			
    System.out.println("NOTE: DO NOT ENTER '.txt' AT THE END OF THE FILE! \nPlease enter a text File: "); 			
    String fileName = scan.nextLine(); 			
    System.out.println(readTextFile(fileName + ".txt")); 			
    break;
    //System.out.println("'Update' File or 'Quit': "); 			
    //String results = scan.nextLine().toLowerCase(); //Convert the String into LowerCase 			 			
    //if(results.equals("update")){ 				
    //System.out.println("This section isn't coded yet. Will be updated in the next version!"); 				
    //break; 			
    //} 			 	
    		 		
    case 2: 			
    System.out.println("Coming Soon! In Version 1.1!"); 			
    break; 			 		
    
    case 3: 			
    scan.nextLine(); 			
    System.out.println("NOTE: DO NOT ENTER '.txt' AT THE END OF THE FILE! \nPlease enter a text File to Update: "); 		    fileName = scan.nextLine(); 		    
    System.out.println("Enter the content you want to add: "); 		    
    String text = scan.nextLine(); 		    
    writeTextFile(fileName + ".txt",text); 		   
     System.out.println("Successful"); 		    
    break; 		           
    
      case 4:         	
    System.out.println("Quit the Files Application.");         	
    break;         	 		
    
    //WILL OUTPUT "ENTER A VALID OPTION" THEN TERMINATE THE PROGRAM!
    default: 			
    System.out.println("Enter a valid option!\n"); 			
    } 		 	
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    42
    Rep Power
    4
    A switch statement will only execute once. By either changing the switch to a while loop or enclosing the switch in a while loop you will be able to achieve the desired effect.

IMN logo majestic logo threadwatch logo seochat tools logo