Hi guys, my program im working on ends when it shouldnt.....
here's the main method.

public static void main(String[] args)
    {
        String answer;
        do
        {
            boolean whole=false;
            boolean whole2=false;
            Scanner sc = new Scanner(System.in);
            int one=0, two=0;
            do
            {
                try{
                    System.out.print("Enter the first whole number: ");
                    one = sc.nextInt();
                    whole = true;
                }
                catch(Exception e)
                {
                    System.out.println("You did not enter a whole number.");
                }
            }while(whole=false);

            do
            {
                try{
                    System.out.print("Enter the second whole number: ");
                    two = sc.nextInt();
                    whole2=true;
                }
                catch(Exception e)
                {
                    System.out.println("You did not enter a whole number.");
                }
            }while(whole2=false);
            System.out.println("Results from adding: " + add(one,two));
            System.out.println("Results from subtracting: " + sub(one,two));
            System.out.println("Results from multiplying: " + mult(one,two));
            System.out.println("Results from dividing: " + div(one,two));
            System.out.println("Results from modulus: " + mod(one,two));
            System.out.println("Results from Math.pow: " + pow(one,two) + "\n\n");

            System.out.println("Do you wish to enter two more numbers? y/n: ");
            answer = sc.nextLine();
        }while(answer.equalsIgnoreCase("y"));
}

if you notice nearly the entire method is in a do while loop..... Now what happens is, i execute the program i enter the two numbers, it does all the correct method calling and prints everything out correctly.... now the last line asks the user if they want to enter in another two numbers.... but what happens is my program displays that line and doesnt give the user a chance to enter "y" or "n"..... it just stops.... i tried making

String answer = "y";

at the beginning of the method but the do while loop still doesnt repeat.... any ideas?

Recommended Answers

All 3 Replies

If you read the API docs for the Scanner class, you will see:
Scanner's nextLine method "advances this scanner past the current line and returns the input that was skipped."

Just replace nextLine() with next()

It worked, ok i like the Java API stuff but their wording is terrible. What does "advances this scanner past the current line and returns the input that was skipped" mean in plain english. and why did .nextLine() work earlier in the program but not at the end? lol sorry i need to learn!
EDIT;;
Ohhhhhhhhhhhhhh basically what nextline does is lets say i have three lines
Hi
HELLO
HOLA

i set the .nextline between hello and hola... and what it does then is the scanner jumps down below hola.... and if the user enters anything between hello and hola it will still grab it. And the reason it didnt work with my program is because it jumped to the next line and it was the end of the program. right ? :P

Member Avatar for ztini

The API is a pain. It's like trying to learn Chinese while looking at an English-to-Chinese dictionary. You should really look at Oracle's Tutorials. They are jam packed with examples that actually make sense.

Best of luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.