-1

My full code is pasted here. Below are the 2 methods that are related to my question.

private static boolean playGameAgain()
{
        char playAgain = 'y';
        Scanner input = new Scanner(System.in);
        while(playAgain != 'n')
        {
                System.out.println("Go again?(y/n)");
/// PROBLEM HERE....  Somehow, it will NOT wait for INPUT. It just throws an error
                playAgain = input.next().charAt(0);
        }
        input.close();
        return (playAgain != 'y')?false:true;
}

// [Trimmed]

public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
{
        Scanner input = new Scanner(System.in);
        for (int i = 1; i <= 5; i++)
        {
                System.out.println("Enter the name for score #" + i + ":");
                names.add(input.nextLine());
                System.out.println();
                System.out.println("Enter the score for score #" + i + ":");
                while(!input.hasNextInt())
                {
                        //input.nextLine();
                        System.out.println("Please input a valid integer value for the score for #" + i + ":");        
                        if(!input.hasNextInt())
                                 input.nextLine();
                }
                scores.add(input.nextInt());
                input.nextLine();
        }
        input.close();
}

I have tried many combinations of how to read in one character. This used to work, that I could read in one character by using:

Scanner input = new Scanner(System.in);
char temp = input.next().charAt(0);

But, somehow, in this program I wrote, it won't work.

It's a program that will read in 5 user names (strings) & 5 scores (integers) and put them in 2 arrays. It will sort the arrays in descending order and then it will print them out. So, the only problem I have is asking if they want to play again and taking some char input to see if they want to play again (y/n)?

Please help if you can. I've tried many combinations of: if (input.hasNext()), to no avail.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • input.next().charAt(0) would require the user to enter a newline, are you considering that correctly? – vikingsteve Feb 22 '13 at 15:40
  • I have edited your question to include the part that I think are crucial to the question. **Please check whether you want to include more**. You can take a look at the [revision](http://stackoverflow.com/posts/15027759/revisions) to see what was changed. – nhahtdh Feb 22 '13 at 15:51

1 Answers1

0

You are not setting playAgain to something other than 'y' in playGameAgain(). You have a bug here.

Sezin Karli
  • 2,437
  • 16
  • 24