Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

Java question. Will post code in the comment below. The loop runs fine the first time. The second time it runs, it prints the first question (which pile) then it skips allowing me to input an answer and moves straight onto the next println and question.

OpenStudy (anonymous):

(Yes, they are initialised etc before the loop) do{ System.out.println("Choose a pile:"); current = keyboard.nextLine(); System.out.println(""); System.out.println("How many to remove from pile?"); take = keyboard.nextInt(); if(current.equalsIgnoreCase("a")){ a = a - take; }else if (current.equalsIgnoreCase("b")){ b = b - take; }else if (current.equalsIgnoreCase("c")){ c = c - take; }else{ System.out.println("error"); } System.out.println("A: " + a + " B: " + b + " C: " + c); System.out.println(""); }while((a>0) && (b>0) && (c>0));

OpenStudy (anonymous):

do: -instruction(choose a pile) -make string current equal the pile name (a, b, c) -instruction(how many to remove) -make int take equal the remove amount -if the pile chosen was a -remove the amount specified from the pile a total -if the pile chosen was b -remove the amount specified from the pile b total -if the pile chosen was c -remove the amount specified from the pile c total -or print error -print display showing totals of each pile -space -unless all piles are 0 or less, go again

OpenStudy (anonymous):

The problem is not in the if/else statements, if they are removed (and adjusted) the problem remains.

OpenStudy (anonymous):

It sounds like a problem that can occur in C when reading from a keyboard. Could be that the last carriage return is not being read in, and is left pending, and the next time around, reading a line comes back with a blank value which is not what you want. Looking up for this issue in Java, it seems that nextInt() does not read the carriage return (it's not an integer), and your nextLine() reads it in, which also means done. The solution is to put a keyboard.nextLine() after each nextInt() to absorb the "abandoned" carriage return.

OpenStudy (woodrow73):

Sounds about spot on.,. I think you need an empty keyboard.nextLine(); or keyboard.next(); after anything that's not a nextLine();, and before any nextLine();

OpenStudy (anonymous):

yes that works! First time I have heard of carriage return thanks. Added an unused String cr = keyboard.nextLine() after the nextInt(), worked a treat.

OpenStudy (woodrow73):

Nice! And, if you're just doing the String cr = keyboard.nextLine() after the nextInt(), and you aren't using the variable cr, you can just do -- keyboard.nextLine() after the nextInt() - nextLine() will still return a String, but it doesn't matter that there isn't a String variable to return it to.

OpenStudy (anonymous):

Oh haha somehow I just assumed it would be necessary thankyou

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!