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");
}
}
}
Answer from vish4071 on Stack OverflowUse 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
Use for loop to get user input and keep prompting for input if the wrong number was entered.
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.comjava - Loop user input until conditions met - Stack Overflow
java - How to end a while Loop via user input - Stack Overflow
Videos
Scanner user_in= new Scanner(System.in);
System.out.print("Enter an integer between 1-10 : ");
int n = user_in.nextInt();
for(int i=0; i<1; i++) {
while(true || !user_in.hasNext()) {
if(n>=0 && n<=10) {
System.out.println("You entered : " + n);
break;
}
else {
System.out.println("Try again!");
user_in.next();
}
}
}So I've trying now to use a for loop to get the user input and keep prompting an input if they enter a number outside the range 1-10. This has been my attempt so far and I keep on failing no matter what I change. Thank you for the help!
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
}
}
Easy with do-while:
Scanner keyboard = new Scanner(System.in);
int startr, endr;
boolean good = false;
do
{
System.out.println("Enter the Starting Number of the Range: ");
startr = keyboard.nextInt();
if(startr % 10 == 0 && startr >= 0)
good = true;
else
System.out.println("Numbers is not divisible by 10");
}
while (!good);
good = false;
do
{
System.out.println("Enter the Ending Number of the Range: ");
endr = keyboard.nextInt();
if(endr % 10 == 0 && endr <= 1000)
good = true;
else
System.out.println("Numbers is not divisible by 10");
}
while (!good);
// do stuff
You need to use a while, something like:
while conditionsMet is false
// gather input and verify
if user input valid then
conditionsMet = true;
end loop
should do it.
You have to assign repeat in your while-loop so it becomes false if the user says yes:
repeat = !input.equalsIgnoreCase("yes");
You just need to set repeat to true or false based on user input. So in the end, compare input with yes or no. Something like this would work for you :
if ("yes".equals(input))
repeat = true; // This would continue the loop
else
repeat = false; // This would break the infinite while loop