Change it to
...
String input;
do
{
input = scan.next();
if(input.equals("enter") || input.equals("Enter"))
{
System.out.println("You have entered the house.");
}
...
Answer from Dacaspex on Stack OverflowChange it to
...
String input;
do
{
input = scan.next();
if(input.equals("enter") || input.equals("Enter"))
{
System.out.println("You have entered the house.");
}
...
Just FYI, instead of:
if(input.equals("enter") || input.equals("Enter"))
use
input.equalsIgnoreCase("enter")
It will eliminate your "or" clauses and shorten your code a little bit
java - Do-While loop with user input - Stack Overflow
Pausing a while loop to get user input, help
Please ensure that:
-
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
-
You include any and all error messages in full
-
You ask clear questions
-
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
More on reddit.comDo while loop skips the last step to get user input
Breaking from while loop when user enters nothing
Videos
Use a while loop above input line as:
while(true)
And, use if condition to break.
if(year == 0)
break;
Also, condition for leap year is wrong in your code. It should be:
if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
//its a leap year
else
//its not
PS: As in comments, I'll give a complete code:
import java.util.*;
public class Task10 {
public static void main(String[] args) {
System.out.println("Enter a year to check if it is a leap year");
while(true){
Scanner input = new Scanner(System.in);
int year = input.nextInt();
if(year == 0)
break;
if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
}
}
}
You need to do something to keep you input loop running until a stopping condition is encountered (which in your case is that when the user inputs 0)
// First get the scanner object with the input stream
Scanner sc = new Scanner(System.in);
// Just using do-while here for no reason, you can use a simple while(true) as well
do{
int input = sc.nextInt(); // read the next input
if (int == 0) { // check if we need to exit out
// break only if 0 is entered, this means we don't want to run the loop anymore
break;
} else {
// otherwise, do something with the input
}
} while(true); // and keep repeating
Hello reddit! Here is my code for my assignment for my compsci class in college. I am a beginner but have the task of making a user-input binary string that updates after every user input. I have nested the program in a while loop so it reprompts the user every time to get a new input for the user. However, it infinitely uses the user's first input each time, with no way to pause the loop to wait for the user's input again. Is there a fix for this? Any help is greatly appreciated. Thank you all for your time! :)
import java.util.Scanner;
public class Program05a {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner stdIn = new Scanner (System.in);
String bitString = "";
int resultSoFar = 0;
int userInput = 3;
while (userInput != 2)
{
System.out.println("0 -> Shift Left, Add 0");
System.out.println("0 -> Shift Left, Add 1");
System.out.println("2 -> Exit, Evaluate");
while ((userInput != 1) && (userInput != 2) && (userInput != 0))
{
System.out.print("Choose an operator from the menu above : ");
userInput = stdIn.nextInt();
}
if (userInput != 2)
{
if (userInput == 0)
{
bitString = bitString + "0";
System.out.println(bitString);
resultSoFar = 2*resultSoFar + 0;
}
else if (userInput == 1)
{
bitString = bitString + "1";
System.out.println(bitString);
resultSoFar = 2*resultSoFar + 1;
}
}
}
System.out.println("\nThe value of the bit is: " + resultSoFar);
stdIn.close();
}}
Please ensure that:
-
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
-
You include any and all error messages in full
-
You ask clear questions
-
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Step through your code line by line (manually don't actually run it) and see how userInput changes as you loop though the program.
Especially look at this line:
userInput = stdIn.nextInt();
As it's the only line that changes the value of userInput.
Example:
for(int i = 0; i < 4; i++) {
// i == 0, 1, 2, 3
if(i % 2 == 0) { // true for 0 and 2
print(i); // prints 02
}
}