0

When I set Case C it runs through the list correctly but it does not return to the Main menu until I enter a string. I want it to return like the other two, any thoughts? It does not get stuck in the loop and I do not understand why entering a string returns to the menu. All case logic works correctly.

public class Postfix_Notation
{
static ArrayList<String> wordlist = new ArrayList<>(); 

public static void main(String[] args) 
{
 Scanner scanner = new Scanner(System.in);
 String choice = "Z";


     while(!choice.equals("D"))
  {
    System.out.println("Enter A, B  or C to perform an operation or enter D to exit.");
    System.out.println("A- Evaluate a user input postfix expressions");
    System.out.println("B- Convert, display infix expressions to postfix expressions then evaluate and display the result of thepostfix expression");
    System.out.println("C- Reads words from a the text file (hangman.txt) in a LinkedList and use an iterator on it to displays all the words (duplicates allowed) in descending alphabetical order");
    System.out.println("D- Exit");
    choice = new Scanner(System.in).next();

        switch (choice) 
         {
          case "A":
          Scanner input3 = new Scanner(System.in);
          System.out.println("Enter an postfix expression, do not add spaces! I.E. 12-3*3");
          String postfix = new Scanner(System.in).next();
          System.out.println(evaluatePostfix(postfix));
          System.out.println("\n");
          break;

          case "B":
          Scanner input2 = new Scanner(System.in);
          System.out.println("Enter an inflix expression, do not add spaces! I.E. 1*2(3+9)");
          String inflix = new Scanner(System.in).next();
      System.out.println("The posfix expression of " + inflix + "is " + infixToPostfix(inflix));
          System.out.println("\n");    
          break;

          case "C":
          Scanner input = new Scanner(System.in);
          String content = new String();
          int count=1;
          int n;
          File file = new File("Hangman.txt");
          LinkedList<String> list = new LinkedList<String>();
          //file.length();

          try 
            {
              Scanner sc = new Scanner(new FileInputStream(file));
              while (sc.hasNextLine())
              {
                   content = sc.nextLine();
                   list.add(content);
              }
               sc.close();
              }
              catch(FileNotFoundException fnf)
              {
               fnf.printStackTrace();
              }
               catch (Exception e) 
              {
               e.printStackTrace();
               System.out.println("\nProgram terminated Safely...");
              }

               Collections.reverse(list);
               Iterator i = list.iterator();
               while (i.hasNext()) 
               {
                System.out.println(i.next());
               }
               File hangman = new File("hangman.txt"); //hangman.txt must be in the project folder or the program terminates
               if (!hangman.exists()) 
              {
                System.out.println(hangman.getAbsolutePath());
                System.out.println(hangman + "File does not exist.");
                System.exit(1);
              }

             try 
             {
               Scanner input4 = new Scanner(hangman);
               while (input.hasNext()) 
               {
                wordlist.add(input.next());
                break;
               }
              } 

             catch (FileNotFoundException ex) 
             {
              ex.printStackTrace();
             }
               System.out.println("Enter a Character to return to menu");
               System.out.println("\n");
               break;

               case "D":
               System.out.println("Program Ended");
               break;

               default:
               System.out.println("Invalid Input");  
     }  
  }
  • 1
    It loops because you have`while(!choice.equals("D"))` - `break` in a `switch` statement only breaks the switch. If necessary consider `labelled break` https://stackoverflow.com/questions/14960419/is-using-a-labeled-break-a-good-practice-in-java – Scary Wombat Apr 23 '18 at 00:58
  • 1
    You should only ever use a single instance of `new Scanner(System.in)` to read all of your console input. Since you've already created the instance `scanner` at the top of your `main` method, just use that everywhere you need input instead of always doing `new Scanner(System.in)`. – Kevin Anderson Apr 23 '18 at 01:07

0 Answers0