2

I am trying to get the name and jersey number of 3 hockey players from the user. I then make an object from my created class called HockeyPlayer with the data I have. I then put it into the array. The second iteration does not work. Please help! Thank you in advance.

ArrayList<HockeyPlayer> array = new ArrayList<HockeyPlayer>();

//For loop to input names
for(int i=0; i < 3; i++)
{   
    System.out.print("Enter name of Player " + i +":");
    startName = keyboard.nextLine();
    System.out.print("Enter jersey number of Player " + i +":");
    playNum = keyboard.nextInt();

    //Make objects and add to array
    HockeyPlayer p = new HockeyPlayer(startName, playNum);
    array.add(p);
}
keyboard.close();
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
Brandy Au
  • 19
  • 5
  • Sorry i'm relatively new to programming and this site. What do you mean any exception? – Brandy Au Oct 12 '13 at 07:02
  • 1
    any error in your console / log – Pankaj Sharma Oct 12 '13 at 07:03
  • 1
    Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at HockeyTester.main(HockeyTester.java:28) – Brandy Au Oct 12 '13 at 07:05
  • `java.util.InputMismatchException`. It seems that you're not entering a number (`keyboard.nextInt()` expects an integer, but it gets something else) – Sadjad Oct 12 '13 at 07:06
  • Ah yes. My intention is for it to ask the name first, then the number. When I run it the first iteration is fine, but the second one displays "Enter name of Player 1:Enter jersey number of Player 1:" on the same line. – Brandy Au Oct 12 '13 at 07:11
  • @BrandyAu I think I know why. Lemme write up the answer real quick. – Dennis Meng Oct 12 '13 at 07:11
  • @home just trying to help – Pankaj Sharma Oct 12 '13 at 07:16
  • 1
    @John: NP, but code should always be posted in the question itself... – home Oct 12 '13 at 07:18
  • @Brandy Au please check your mail – Pankaj Sharma Oct 12 '13 at 07:58

5 Answers5

4

The problem here is that in every iteration of your loop, you make a call to nextLine(), then a call to nextInt(), but after you make the call to nextInt(), the newline character has not been read. Basically, if the input is something like

First Player Name
1
Second Player Name
2

then, after the first iteration of your loop, the Scanner has just finished reading in the 1, but not the newline right after it. Hence, in the second iteration, the nextLine() deals with the newline after 1, but only that newline. Then, the nextInt() call will try to turn Second into an int, and throws the InputMismatchException.

Common ways of going around it are to either put another nextLine() call right after the call to nextInt() (and just throw away this extra newline), or to just read in the line with the number all at once with a call to nextLine(), and parse out the int using Integer.parseInt().

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • Dennis, wow! Using the nextLine call really worked. Thank you so much. But I've actually discovered another problem. I'm trying to print out the data using an "enhanced-for loop" but it's not coming out the way I want. for(HockeyPlayer val : array) { System.out.println("The players are:"); System.out.println(val); } – Brandy Au Oct 12 '13 at 07:28
  • Lemme guess, you want to print out "The players are:", then each of the players' names? – Dennis Meng Oct 12 '13 at 07:29
  • Exactly, on different lines with their numbers as well. So an example: Brandy 33 John 21 – Brandy Au Oct 12 '13 at 07:30
  • Then pull `System.out.println("The players are:")` out of the loop, and print player information inside the loop. Tweak the whitespace however you want. – Dennis Meng Oct 12 '13 at 07:31
  • 2
    Also, please don't edit questions to deal with a completely different issue. If you end up with substantial problems getting your pretty printing the way you want, post a different question. – Dennis Meng Oct 12 '13 at 07:32
  • Thanks for the help Dennis, you're awesome. And sorry about the editing! – Brandy Au Oct 12 '13 at 07:38
2

From InputMismatchException's JavaDoc:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

It seems that you entered a string whereas nextInt() expects an integer.

pnathan
  • 713
  • 3
  • 9
  • Yes, you are right, but I want it to ask for a string first, then ask for an int. I have no idea why it wants the int first. – Brandy Au Oct 12 '13 at 07:20
0

If by second iteration you mean the second for, you probably have to override your HockeyPlayer.toString() method.

public String toString() {
  return name+" "+startNum;
}
Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • In the comments, she mentions that she was getting an `InputMismatchException` in the first loop. After she fixed the original issue, she said she was having issues printing out the information the way she wanted, and only then was the question edited to have the second for loop. – Dennis Meng Oct 12 '13 at 07:42
0

I assume your keyboard variable is of type java.util.Scanner. If that is true then you need to call keybord.reset() at the end of loop.

Your problem is that keyboard.nextInt() does not consumes end of line which is produced when you hit enter. This end of line character is responsible for your exceptions.

This code works:

        HockeyPlayer [] hArr = new HockeyPlayer[3];

        for(int i=0; i < 3; i++)
        {   
            String startName = "";
            Scanner scanner = new Scanner(System.in);
            int playNum = 0;

            System.out.print("Enter name of Player " + i +":");
            startName = scanner.nextLine();
            System.out.print("Enter jersey number of Player " + i +":");
            playNum = scanner.nextInt();
            scanner.reset();

            HockeyPlayer p = new HockeyPlayer(startName, playNum);
            hArr[i] = p;
        }
0

It is good to go with:

int playNum = Integer.parseInt(sc.nextLine());
LSerni
  • 55,617
  • 10
  • 65
  • 107