hamc17
hamc17

Reputation: 15

Java: Input comparison

I'm wondering why when I type 'y' after being asked if there are any more digits in this method the code works correctly and leaves the loop, but upon typing 'n' and hitting return, I need to type 'n' and hit return again or the process just hangs.

Why?

The string 'input' is being passed from the user's input in the main method and is in this case "addition".

private int add(String input)
    {
        int additionValue = 0;
        boolean keepGoing = true;
        if (input.matches("addition"))
        {

            while (keepGoing == true)
            {
                System.out.println("Next digit = (Type the digit)");
                additionValue = additionValue + scan.nextInt();
                System.out.println("Any more digits? Type y/n");
                if (scan.next().matches("Y|y"))
                {
                    keepGoing = true;
                }
                else if (scan.next().matches("N|n"))
                {
                    keepGoing = false;
                }
                else
                {
                    System.out.println("Great, you broke it.");
                    System.exit(1);
                }
            }
        }
    }

I've managed to get the code working by using

System.out.println("Any more digits? Type y/n"); 
String yayOrNay = scan.next();
if (yayOrNay.length()==1 && yayOrNay.charAt(0)=='y')
                {
                    keepGoing = true;
                }

but that seems a little too complicated to me for all that it's doing.

Upvotes: 1

Views: 109

Answers (6)

Jojo John
Jojo John

Reputation: 400

Please Try This,

Scanner scan = new Scanner(System.in);
        int additionValue=0;
        while (true)
        {
            System.out.println("Any more digits? Type y/n");  
            if (scan.next().matches("Y|y"))
            {
                 System.out.println("Next digit = (Type the digit)");
                 additionValue = additionValue + scan.nextInt();
                 //System.out.println("Any more digits? Type y/n");
            }
            else 
            {
                System.out.println("Thanks");
               break;
            }
        }

I am not sure whether this logic will work for you. 'If you want you can process 'n' also'

output

Any more digits? Type y/n
y
Next digit = (Type the digit)
5
Any more digits? Type y/n
n
Thanks

Upvotes: 0

Francis
Francis

Reputation: 1090

You need to enter 'n' twice because you scan for input in each if condition. So if the letter is not a 'y', your program will wait for user input in the next if statement.

You can simply do:

String yayOrNay = scan.next();
if (yayOrNay.matches("Y|y"))
{
    keepGoing = true;
}
else if (yayOrNay.matches("N|n"))
{
    keepGoing = false;
}
...

Upvotes: 1

buburs
buburs

Reputation: 64

This is because you call scan.next() two times. You have to call it one time, putting the resulting string in a variable.

String input2 = scan.next();
if (input2.matches("Y|y"))
{
    keepGoing = true;
}
else if (input2.matches("N|n"))
{
    keepGoing = false;
}

Upvotes: 1

Daniel
Daniel

Reputation: 36710

Your error is to call scan.next() twice, which requests two imputs.

private int add(String input)
{
    int additionValue = 0;
    boolean keepGoing = true;
    if (input.matches("addition"))
    {

        while (keepGoing == true)
        {
            System.out.println("Next digit = (Type the digit)");
            additionValue = additionValue + scan.nextInt();
            System.out.println("Any more digits? Type y/n");
            String s = scan.next();
            if (s.matches("Y|y"))
            {
                keepGoing = true;
            }
            else if (s.matches("N|n"))
            {
                keepGoing = false;
            }
            else
            {
                System.out.println("Great, you broke it.");
                System.exit(1);
            }
        }
    }
}

Upvotes: 0

tolanj
tolanj

Reputation: 3724

                if (scan.next().matches("Y|y"))
            {
                keepGoing = true;
            }
            else if (scan.next().matches("N|n"))

You call scan.next twice

1st time you check if its Y, if it isn't you read from input again

so

        somevar=scan.next()
        if (somevar.matches("Y|y"))
        {
            keepGoing = true;
        }
        else if (somevar.matches("N|n"))
        {
            keepGoing = false;
        }
        else ..

Upvotes: 0

Garrett Hall
Garrett Hall

Reputation: 30032

scan.next() pulls a new character from the input steam.

So when you check for 'n' and scan.next().matches("Y|y") executes, it is actually skipping 'n' for your next comparison.

The solution is to assign scan.next() into a variable you can use:

        while (keepGoing == true)
        {
            System.out.println("Next digit = (Type the digit)");
            additionValue = additionValue + scan.nextInt();
            System.out.println("Any more digits? Type y/n");
            String next = scan.next();
            if (next.matches("Y|y"))
            {
                keepGoing = true;
            }
            else if (next.matches("N|n"))
            {
                keepGoing = false;
            }
            else
            {
                System.out.println("Great, you broke it.");
                System.exit(1);
            }
        }

Upvotes: 4

Related Questions