Java program asking user to continue - java

public static void main(String [] args) {
Scanner input = new Scanner(System.in);
double dblNumber, dblSquare, dblSqrt;
String answer;
String answery = "yes";
String answern = "no";
while (true) {
System.out.println( "Welcome to Squarez! Where we do the math for you.\nPlease input a number you would like us to do some math with.");
dblNumber = input.nextDouble();
dblSqrt = Math.sqrt(dblNumber);
dblSquare = Math.pow(dblNumber, 3);
System.out.println("" + dblSquare + " " + dblSqrt);
System.out.println("Would you like to continue?");
answer = input.nextLine();
if (answer.equals(answery)) {
System.out.println("You answered yes");
}
if (answer.equals(answern)) {
System.out.println("You answered no.");
System.exit(0);
}
}
}
The program runs and completely ignores the prompt for asking the user if they want to continue. It goes straight back to the prompts for first number. Why is it skipping that?

You have to consume the line break after your double:
System.out.println("Would you like to continue?");
input.nextLine(); // <-- consumes the last line break
answer = input.nextLine(); // <-- consumes your answer (yes/no)

You read the number with the statement
dblNumber = input.nextDouble();
Although this line blocks until a whole line - including a newline character - is entered by the user, only the characters without the newline are parsed and returned as a double.
That means, the newline character is still waiting to be retrieved from the scanner! The line
answer = input.nextLine();
directly does exactly that. It consumes the newline character, feeding the variable answer with an empty string.
So, what's the solution? Use always input.nextLine() for reading user input and then parse the resulting string in whatever you need:
String line = input.nextLine();
dblNumber = Double.parseDouble(line);

that's how it should be:
String answer;
String answery = "yes";
String answern = "no";
System.out.println("Would you like to continue?");
input.nextLine();
answer = input.nextLine();
And you just have two decisions to make either "yes" or "no", I don't really see why you used 2 if statements; while you'd have just done this for the Yes part of the answer and the opposite will be a NO.
if(answer.equals(answery))
{
System.out.println("You answered yes");
}
else
System.out.println("You answered no.");
System.exit(0);

Related

How do I prevent an error message from repeating in Java?

I'm trying to write a program to calculate factorial but I can't figure out why the Error message displays twice if I enter a letter instead of an integer.
I feel like the issue has to do with Line 29 c = sc.next().charAt(0);, but am not sure how to fix it. Any help is appreciated.
My program:
public class Factorials {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char c = 'Y';
int num = 0;
do
{
System.out.print("Enter a number to calculate its factorial: ");
while (!sc.hasNextInt()) {
System.out.println("Invalid Entry - Enter only Integers! Try Again: ");
sc.nextLine();
}
int result = 1;
num = sc.nextInt();
for(int i = 1; i <= num; i++) {
result = result * i;
}
System.out.println("The factorial of " + num + " is: " + result);
System.out.println("Do you wish to continue? Y/N: ");
c = sc.next().charAt(0);
}while(c == 'y' || c == 'Y');
sc.close();
}
}
Simple fix: Change the sc.nextLine(); in your code to a sc.next() and you should be good to go. This error occurs because .nextLine() considers the enter/return key as a separate character, while .next() doesn't. (The enter key when you press it after entering either 'y' or 'n': if you try it, the error message doesn't print twice if you enter a letter the first time you run the program).
Side note: You probably want it to be a .print(/*invalid input sentence*/) instead of a .println() to go along with how you take in your other number values.
Otherwise, you're good!
Finds and returns the next complete token from this scanner.
A complete token is preceded and followed by input that matches
the delimiter pattern
As jdk doc shows, the 'sc.next' method will return when it reaches space, enter or return. So when you enter 'y' with enter, the enter character is still in buffer. You can assign sc.nextLine to a variable, like
String str = sc.nextLine();
System.out.println(str);
You can see the enter character and your input character.
Both #TheSj and #Lahiru Danushka answer could solve this problem.
add sc.nextLine(); after c = sc.next().charAt(0);

Why is the confirmation statement printing to the console twice? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
Whenever I run the program, everything works fine but for some reason the Confirmation prints are happening twice and I can't figure out why. Any help would be appreciated.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String Z = "Z";
int number;
String Znumber = "";
String Confirmation = "";
int ExamType;
// Ask user for Z number
do {
System.out.println("Please enter your Z number. Example: 12345678");
number = input.nextInt();
Znumber = Z + number;
} while ((Znumber.length() != 9));
//Confirm Z number entry
do {
System.out.printf(
"Your Z number is: %s. Is this correct? Enter Y/N: \n",
Znumber);
Confirmation = input.nextLine();
} while (!Confirmation.equalsIgnoreCase("y"));
// Ask user which exam they would like to take
ExamType = 0;
Confirmation = "";
System.out.println("Which exam would you like to take? Select 1-3: ");
System.out.println("1: English");
System.out.println("2: Spanish");
System.out.println("3: Math");
ExamType = input.nextInt();
do {
System.out.printf("You selected %s. Are you sure? Enter Y/N: \n",
ExamType);
Confirmation = input.nextLine();
} while (!Confirmation.equalsIgnoreCase("y"));
// Begin exam
if (ExamType == 1) {
System.out.println("Welcome to the English exam!");
// Start code from JavaExam.java
} else if (ExamType == 2) {
System.out.println("Welcome to the Spanish exam!");
// Start code from MathExam.java
} else if (ExamType == 3) {
System.out.println("Welcome to the Math exam!");
// Start code from EnglishExam.java
Instead of Confirmation = input.nextLine();, use Confirmation = input.next(); and you should be good. Tested and confirmed.
You really don't need nextLine here in your do-while loop.
Your first loop (// Ask user for Z number)
number = input.nextInt();
does not read the last '\n'.
I think you need to add a nextLine() in that loop.
In your do-while when you run for the first time print Your Z number is: %s. Is this correct? Enter Y/N:. after that the condition !Confirmation.equalsIgnoreCase("y") is evaluated in this case gives true for this cause try to run the loop do-while in second time.
do {
System.out.printf("Your Z number is: %s. Is this correct? Enter Y/N: \n",
....
} while (!Confirmation.equalsIgnoreCase("y"));

Scanner loop doesn't cycle when hitting enter twice

boolean loop = false;
double numberOfStudents;
System.out.print("Enter a number: ");
if ((scnr.nextLine().trim().isEmpty()) ) {
loop = true;
}
while (loop) {
System.out.println("Enter a number");
if (scnr.hasNextDouble() ){
System.out.println("Loop has stopped");
numberOfStudents = scnr.nextDouble();
loop = false;
}
}
System.out.println("You're outside the loop!");
I'm trying to get the program to say "Enter a number" until the user has entered an actual number (no white spaces or letters or signs). When the user has entered a number, it sets numberOfStudents equal to that number and breaks out of the loop.
But if you hit enter twice, it doesn't iterate. It only displays "Enter a number" once.
What is wrong with the loop logic? Why isn't it looping until valid input is taken?
For the actual answer to your question of "Why doesn't 'Enter a number' display more than once?" see Tom's comment (update: Tom's answer).
I've rewritten your loop in a way which preserves your code, but also makes it a little easier to handle format exceptions (though at the risk of silently swallowing an exception -- should be acceptable for this use case).
Can be up to you to use this design, here is an SO post on why empty catch blocks can be a bad practice.
public static void main(String args[])
{
boolean loop = true;
double numberOfStudents;
Scanner scnr = new Scanner(System.in);
while(loop){
System.out.print("Enter a number: ");
String input = scnr.nextLine();
try{
numberOfStudents = Double.parseDouble(input);
loop = false;
}catch(NumberFormatException e){
}
}
System.out.println("You're outside the loop!");
}
Output:
Enter a number:
Enter a number:
Enter a number:
Enter a number: 50
You're outside the loop!
First of all: Since you're reading from System.in a call to the input stream will block until the user entered a valid token.
So let's check first scan using your scnr variable:
scnr.nextLine()
nextLine() reads everything til the next line delimiter. So if you just press return, then it will successfully read it and will perform the next stuff.
The next call is:
scnr.hasNextDouble()
This call expects a "real" token and ignores white spaces, except as a delimiter between tokens. So if you just press return again it doesn't actually read that input. So it still waits for more (for the first token). That is why it stucks in your loop and you won't get another "Enter a number" output.
You can fix that by either enter a real token, like a number, or by changing the loop like trobbins said.
I hope you now understand your program flow a bit more :).
While trobbins code basically solves your problem, it's bad practice to use exceptions for flow control.
I used a small regexp to check if the value is a number. But this example is not complete, it will still crash it the user enters for example two decimal points. So you would need to create a proper number check or just use integers where the check is much easier.
Someone in the comments pointed out that people may want to enter scientific notation like 5e10, so this would also be another case to check for. If this is just some code you need as a proof of concept or something quick and dirty, you can go with the exception handling method but in production code you should avoid using exceptions this way.
double numberOfStudents;
Scanner scnr = new Scanner(System.in);
while(true) {
System.out.print("Enter a number: ");
String input = scnr.nextLine().trim();
if(input.matches("^[0-9\\.]{1,}$")) {
System.out.println("Loop has stopped");
numberOfStudents = Double.parseDouble(input);
break;
}
}
System.out.println("You're outside the loop!");
The following code should help you:
double numberOfStudents = 0;
Scanner scnr = new Scanner(System.in);
boolean readValue = false; //Check if the valid input is received
boolean shouldAskForNumber = true; //Need to ask for number again? Case for Enter
do {
if (shouldAskForNumber) {
System.out.print("Enter a number:");
shouldAskForNumber = false;
}
if (scnr.hasNextDouble()) {
numberOfStudents = scnr.nextDouble();
readValue = true;
} else {
String token = scnr.next();
if (!"".equals(token.trim())) { //Check for Enter or space
shouldAskForNumber = true;
}
}
} while (!readValue);
System.out.printf("Value read is %.0f\n", numberOfStudents);
System.out.println("You're outside the loop!");
Update
Understood the following statement in question different way:
But if you hit enter twice, it doesn't loop back. It only displays
"Enter a number" once.
The code is set to print "Enter a number" only once if the user hits RETURN/ENTER or enters space character. You may remove the special check and use the code if needed.
import java.util.Scanner;
public class Testing {
public static boolean checkInt(String s)
{
try
{
Integer.parseInt(s);
return true;
} catch (NumberFormatException ex)
{
return false;
}
}
public static void main(String[] args) {
boolean loop = false;
double numberOfStudents;
Scanner scnr = new Scanner(System.in);
String input = "";
while (!(checkInt(input))) {
System.out.println("Enter a number");
input = scnr.nextLine();
}
numberOfStudents = Integer.parseInt(input);
System.out.println("Number of students: " + numberOfStudents );
}
}
//this code is working fine, if you want you check it out.
//In your code your taking another input if the first is an int/double; if the first input is not a number then you have mentioned to take input again..
Use a debugger to see what the code is actually doing. Here's a guide on debugging in Eclipse. After you have finished debugging your code, you will probably know what the problem is.
Below code will help you
boolean loop = true;
double numberOfStudents;
Scanner scnr = new Scanner(System.in);
System.out.print("Enter a number: ");
String input = scnr.nextLine();
while(!scnr.hasNextDouble()){
System.out.print("Enter a number: ");
try{
numberOfStudents = Double.parseDouble(input);
break;
}catch(NumberFormatException e){
}
input = scnr.nextLine();
}
System.out.println("You're outside the loop!");
The following code is working,
boolean loop = true;
double numberOfStudents;
Scanner scnr=new Scanner(System.in);
while(loop) {
System.out.println("Enter a number");
if ((scnr.nextLine().trim().isEmpty()) ) {
loop = true;
}
if (scnr.hasNextDouble() ){
System.out.println("Loop has stopped");
numberOfStudents = scnr.nextDouble();
loop = false;
}
}
System.out.println("You're outside the loop!");
The output is,
run:
Enter a number
hj
po
Enter a number
lhf
Enter a number
o
Enter a number
p
Enter a number
a
Enter a number
34
Loop has stopped
You're outside the loop!
You have to scan the next line if you want to get more values form the scanner again. The code should be like:
while (loop) {
System.out.println("Enter a number");
if(!(scnr.nextLine().trim().isEmpty())){
if (scnr.hasNextDouble() ){
System.out.println("Loop has stopped");
numberOfStudents = scnr.nextDouble();
loop = false;
}
}
}

Scanner input validation in while loop

I've got to show Scanner inputs in a while loop: the user has to insert inputs until he writes "quit". So, I've got to validate each input to check if he writes "quit". How can I do that?
while (!scanner.nextLine().equals("quit")) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
service.storeResults(question, answer); // This stores given inputs on db
}
This doesn't work. How can I validate each user input?
The problem is that nextLine() "Advances this scanner past the current line". So when you call nextLine() in the while condition, and don't save the return value, you've lost that line of the user's input. The call to nextLine() on line 3 returns a different line.
You can try something like this
Scanner scanner=new Scanner(System.in);
while (true) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
if(question.equals("quit")){
break;
}
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
if(answer.equals("quit")){
break;
}
service.storeResults(question, answer);
}
Try:
while (scanner.hasNextLine()) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
if(question.equals("quit")){
break;
}
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
service.storeResults(question, answer); // This stores given inputs on db
}
always check if scanner.nextLine is not "quit"
while (!scanner.nextLine().equals("quit")) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
if(question.equals("quit"))
break;
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
if(answer.equals("quit"))
break;
service.storeResults(question, answer); // This stores given inputs on db
}

How do I add all of a scanner's input to a string?

What I want is to be able to enter a string into the console and have it inputted into a string (an array would be fine, I just want to store it). Here's my example code:
System.out.println("What is your opinion?");
while(user_input.hasNext()){
choice = choice + " " + user_input.next();
}
this.opinion = choice;
All this does is ask for user input repeatedly. Thank you in advance for any input!
Looks like you want nextLine():
System.out.println("What is your opinion?");
this.opinion = user_input.nextLine();
Note that you should follow Java naming conventions as well: userInput.
System.out.println("What is your opinion?");
Scanner user_input = new Scanner(System.in);
String line = user_input.nextLine();
String choice = "";
while (!line.equalsIgnoreCase("-1")){ // here you can have terminate string. I used -1
choice += "," + line;
line = user_input.nextLine();
}
System.out.println(choice);
Then you can get a string array by
String[] words = choice.split(",");

Categories

Resources