A. Hodges A. Hodges - 1 month ago 6
Java Question

How to make a while completely loop?

I have a code with four different methods (not pictured below) and have created a menu for the user to choose which method they want to choose.

How do I loop the method to where it constantly goes back to the Menu Screen unless a 5 is entered by the user?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ReactionAnalysis {
public static void main (String [] args) throws FileNotFoundException
{
/// Menu Screen
System.out.println("What would you like to do?");
System.out.println("1: Get the score of a word");
System.out.println("2: Get the average score of words in a file (one word per line)");
System.out.println("3: Find the highest/lowest scoring words in a file");
System.out.println("4: Sort words from a file into positive.txt and negative.txt");
System.out.println("5: Exit the program");

Scanner in = new Scanner(System.in);
System.out.println("Enter a number 1-5");
int numberChoice = in.nextInt();
////

while (numberChoice != 5)
{
if (numberChoice == 1)
{
String methodOne = methodOne();
System.out.println(methodOne);
}
else if (numberChoice == 2)
{
String methodTwo = methodTwo();
System.out.println(methodTwo);
}
else if (numberChoice == 3)
{
String methodThree = methodThree();
System.out.println(methodThree);
}
else if (numberChoice == 4)
{
String methodFour = methodFour();
System.out.println(methodFour);
}
}
}
}


Right now, after I go through one of the steps fully, it just repeats the step. I need it to go through the Step, and then go back to the menu, and repeat unless a 5 is entered in the menu.

Answer

Sorry for the formatting, just copy pasted most of your code: I moved the loop to the top, and initialized the int to 0 so it would run through at least once.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;

    public class ReactionAnalysis {
      public static void main (String [] args) throws FileNotFoundException
      {
        int numberChoice = 0;
    //loop everything
    while (numberChoice != 5)
    {
        /// Menu Screen
    System.out.println("What would you like to do?");
    System.out.println("1: Get the score of a word");
    System.out.println("2: Get the average score of words in a file (one word per line)");
    System.out.println("3: Find the highest/lowest scoring words in a file");
    System.out.println("4: Sort words from a file into positive.txt and negative.txt");
    System.out.println("5: Exit the program");

    Scanner in = new Scanner(System.in);
    System.out.println("Enter a number 1-5");
    numberChoice = in.nextInt();
    ////

      if (numberChoice == 1)
      {
        String methodOne = methodOne();
        System.out.println(methodOne);
      }
      else if (numberChoice == 2)
      {
        String methodTwo = methodTwo();
        System.out.println(methodTwo);
      }
      else if (numberChoice == 3)
      {
        String methodThree = methodThree();
        System.out.println(methodThree);
      }
      else if (numberChoice == 4)
      {
        String methodFour = methodFour();
        System.out.println(methodFour);
      }
    }
}
}