0

I have a for loop that is entered if the user chooses so.

System.out.println("Single Player=1\t Two Player=2\t Report Single  
Tables=3\t Report all tables=4\t Exit=5");
choice= sc.nextInt();


if(choice ==1){
    for (int i = 0; i <= np.length; i++) {
        if (np[i] == 0) {
            np[i]++;
            System.out.print("Enter name:");
            String str = sc.nextLine();
            player1[i] = str;
            break;

    }
}}

When i enter the for loop, the enter name prompt pops up as well as the menu again, which is supposed to appear after the loop finishes. What i am confused about is why it is breaking before i can even enter the name.

  • Your bracketing isn't correct. Can you please close them properly? I assume you are missing 2 of them at the end, to close the for basic block and the if basic block. –  Feb 10 '15 at 01:59
  • 1
    What is `sc`? Also you most likely don't want `i <= np.length`. – StenSoft Feb 10 '15 at 02:00
  • 1
    `String str = sc.nextLine(); player1[i] = str;` could be replaced with: `player1[i] = sc.nextLine();` and what is `np` ? how can we help you without knowing what it contains ? – Nir Alfasi Feb 10 '15 at 02:03
  • really think about what `i = 0 && i <= np.length` means when both `i == 0` and `np.length() == 0` –  Feb 10 '15 at 02:04
  • @irradiatedcat - read for comprehension, I did not ask what it was for, I asked what do you think the logic is when the array is empty and `i == 0` –  Feb 10 '15 at 02:06
  • possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Feb 10 '15 at 02:48

2 Answers2

2

Show all the code

Otherwise everyone is just guessing.

Learn to use a step debugger

look at what np.length equals when i == 0 and then you will most likely have your answer.

Spelling it out

first pass through the look i == 0 and most likely np.length == 0 as well but we have absolutely no idea because you did not post all the relevant code.

What would i == 0 && i <= np.length be when the array is 0 length?

for (i = 0; i <= 0; i++) 
{ 
  /* how many times do you think this will loop? */ 
}
Community
  • 1
  • 1
0

You should add sc.nextLine(); right after choice= sc.nextInt();. It'll solve your problem.

Because when you enter the number for choice, you press Enter. Number is read to choice by sc.nextInt() but your Enter still in input stream. Then, String str = sc.nextLine(); read your Enter and break your loop instead of waiting you to Enter a name.

System.out.println("Single Player=1\t Two Player=2\t Report Single  
Tables=3\t Report all tables=4\t Exit=5");
choice= sc.nextInt();

sc.nextLine(); //this line will consume your Enter (end line) char

if(choice ==1){
    for (int i = 0; i <= np.length; i++) {
        if (np[i] == 0) {
             np[i]++;
             System.out.print("Enter name:");
             String str = sc.nextLine();
             player1[i] = str;
             break;
          }
     }
}
Antony Dao
  • 425
  • 2
  • 14