Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've tried, mainly for the purpose of this question of making a simple "calculator" type program. When it runs the last 3 sentences will print out without letting the user enter anything. How do I make the program only ask a question after input?
Thanks for any help in advance.

What I have tried:

Java
import java.util.Scanner;
public class hello1{
public static void main(String[]args)
{

 Scanner scan = new Scanner(System.in);
 String s1 ="";
 int num1=0;
 int square=0;

    do
    {
    	System.out.println("Please enter a number between 1 and 20");
    	num1 = scan.nextInt();
    	System.out.println("Do you want to square this number?");
    	String s2 = scan.nextLine();
    	if(s2.equalsIgnoreCase("Yes"))
    	{
    		square = num1*num1;
    		System.out.println("The square is " + square);
    	}
    	else
    	{
    		System.out.println("No square");
    	}

        System.out.println("Do you want to continue? Y/N");//Print
        s1 =scan.nextLine();//Read

    }

    while(s1.equalsIgnoreCase("Y"));
}
}
Posted
Updated 4-Nov-16 23:30pm
v2

Just add the extra line in your code as shown:
Java
System.out.println("Please enter a number between 1 and 20");
num1 = scan.nextInt();
scan.nextLine(); // add this line to consume the newline character
System.out.println("Do you want to square this number?");
 
Share this answer
 
Comments
Patrice T 5-Nov-16 6:26am    
I am afraid the solution can't be so simple !
The OP is already using scan.nextLine() in second and third input.
It don't match the problem description.
Richard MacCutchan 5-Nov-16 6:27am    
Yes it is and it does.
Patrice T 5-Nov-16 6:37am    
If adding a scan.nextLine() is the solution, the description should speak about a skip of second input.
Richard MacCutchan 5-Nov-16 6:42am    
I tested my fix and it does what OP wants. If your test does not work then please provide the details.
Patrice T 5-Nov-16 6:54am    
In this case, I suspect the problem description is wrong, that is why I suggest to debug the program to see what really happen.
I don't have Java to test it.
With the debugger, you will see exactly what num1, s2 and s1 contain at runtime.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900