1

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.

1
  • Brilliant, thanks for all the replies, I now understand where I was getting mixed up! Commented Oct 17, 2013 at 17:36

6 Answers 6

4

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);
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

Comments

1

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;
}
...

Comments

0
                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 ..

Comments

0

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);
            }
        }
    }
}

Comments

0

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

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.