0

I would like to add a name to a queue (linked), one name at a time from a text file. If the user selects choice 1 then it should take the next name in from the lists.

Case 1 is not letting me input another choice if I want to add another name.

   int choice = console.nextInt();

   FileReader names = new FileReader("Customer.txt");

   Scanner lookup = new Scanner(names);

   Queue a = new Queue();


   String customerName;
                                      // This loop was just to verify that 
   while(lookup.hasNextLine() ){      // It was actually reading

  customerName = lookup.next(); 
  System.out.println(customerName);

   }


   while(true){

       switch(choice){

    case 1:
            customerName = lookup.next();//For some reason its not giving me
           a.enqueue(customerName); // The choice to enter another number
                   break;           //even though I made the case while(true)

    case 2:

                   break;

    case 3:

                   break;

    case 4:

                   break;

    case 5:

                   break;

    case 6:
               System.out.println(" Exiting......");
        break;


    default:

        continue;


    }

    break;
}
2
  • 2
    Why should that let you enter something? You're reading from the file there, not from the console. And you read only one input from the user. Commented Oct 31, 2015 at 22:40
  • 1
    You have a break after your switch block, which terminates the while loop. Commented Oct 31, 2015 at 22:41

1 Answer 1

1

The problem here is that there is a break after the switch statement. This is causing your code to jump out of the while loop after one pass of the switch statement.

The solution is to remove the break, as such:

while(true){

       switch(choice){

    case 1:
            customerName = lookup.next();//For some reason its not giving me
           a.enqueue(customerName); // The choice to enter another number
                   break;           //even though I made the case while(true)

    case 2:

                   break;

    case 3:

                   break;

    case 4:

                   break;

    case 5:

                   break;

    case 6:
               System.out.println(" Exiting......");
        break;


    default:

        continue;


    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.