-3

I have a problem where once I select one of the four options with my scanner, it'll default to that switch case, so say I initially press 1, input my string of words, then press 2, it'll still execute as if I hit case 1. What have I done wrong here?

  public static void menu()
{
    FileClass f = new FileClass();

    Scanner scan = new Scanner(System.in);
    System.out.println("Choose an option");
    System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\n4: Exit Program");
    int choice = scan.nextInt();

    while(choice < 5) {
        switch(choice) {

            case 1:
            System.out.println("Enter a line you wish to write to the file: ");
            Scanner inputW = new Scanner(System.in);
            String lineWrite = inputW.nextLine();
            f.writeToFile(lineWrite);
            break;

            case 2:
            System.out.println("This is Everything on the File:");
            f.readFromFile();
            break;

            case 3:
            System.out.println("Enter a line you wish to delete from the file: ");
            Scanner inputD = new Scanner(System.in);
            String lineDelete = inputD.nextLine();
            f.deleteFromFile(lineDelete);
            break;

            case 4:
            System.exit(0);
            break;

        }
        System.out.println("Choose an option");
        System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\nExit Program");
        scan.nextInt();
    }
    System.exit(0);
}
Layken
  • 35
  • 2

2 Answers2

1

I think your last usage of scan.nextInt() is redundand. I think your program could look like this:

public static void main(String... args) {
    try (Scanner scan = new Scanner(System.in)) {
        int choice = 0;

        while (choice < 4) {
            System.out.println("Choose an option");
            System.out.println("1: Write To File");
            System.out.println("2: Read From File");
            System.out.println("3: Delete From File");
            System.out.println("4: Exit Program");

            choice = scan.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter a line you wish to write to the file: ");
                    break;
                case 2:
                    System.out.println("This is Everything on the File:");
                    break;
                case 3:
                    System.out.println("Enter a line you wish to delete from the file: ");
                    break;
            }
        }
    }
}
oleg.cherednik
  • 12,764
  • 2
  • 17
  • 25
0

You have a choice variable that you initialize once outside your loop and never assign to again. The initial value you set it to just keeps getting reused in an infinite loop. You should assign to it within your loop if you wish to see any changes between iterations.

Another point is that you are repeating your menu code twice. This can be easily avoided by putting it in the loop as well.

A Scanner for a particular steam only needs to be created once and reused. There is no need for multiple scanners on System.in.

Technically, you are treating inputs >= 4 as exit commands. There is no need for a separate switch entry, at least in your currently posted code.

Every time a user hits enter, you will get a newline in the input. The newline will prevent you from parsing things correctly, so you need to get rid of it. A good way would be to call Scanner.nextLine between calls to nextInt:

public static void menu()
{
    FileClass f = new FileClass();

    Scanner scan = new Scanner(System.in);
    int choice = 0;
    while(choice < 4) {
        System.out.println("Choose an option");
        System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\n4: Exit Program");
        choice = scan.nextInt();
        scan.nextLine(); // Get rid of the trailing newline character

        switch(choice) {
            case 1:
                System.out.println("Enter a line you wish to write to the file: ");
                String lineWrite = scan.nextLine();
                f.writeToFile(lineWrite);
                break;

            case 2:
                System.out.println("This is Everything on the File:");
                f.readFromFile();
                break;

            case 3:
                System.out.println("Enter a line you wish to delete from the file: ");
                String lineDelete = scan.nextLine();
                f.deleteFromFile(lineDelete);
                break;
        }
    }
    System.exit(0);
}
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186