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 Overflow Top answer 1 of 3
3
Change it to
...
String input;
do
{
input = scan.next();
if(input.equals("enter") || input.equals("Enter"))
{
System.out.println("You have entered the house.");
}
...
2 of 3
1
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
Videos
05:55
Java Do While Loop Example Where User Prompts to Start ...
08:08
Controlling Loops with User Input - YouTube
03:17
User Input Validation With A Do-While Loop | C Programming Example ...
03:10
Incorporate User Input into a For Loop in Java - YouTube
02:19
How to Input Validate Using While Loop in Java Tutorial - YouTube
Java EP.4: Accepting User Input using Scanner class (while ...
Top answer 1 of 4
9
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");
}
}
}
2 of 4
3
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
Programiz
programiz.com › java-programming › do-while-loop
Java while and do...while Loop
In the above program, we have used the Scanner class to take input from the user. Here, nextInt() takes integer input from the user. The while loop continues until the user enters a negative number.
W3Schools
w3schools.com › java › java_while_loop_do.asp
Java Do/While Loop
Summary: A do/while loop always runs at least once, even if the condition is false at the start. This is the key difference from a while loop, which would skip the code block completely in the same situation. This behavior makes do/while useful when you want something to happen at least once, such as showing a message or asking the user for input.
Programming Simplified
programmingsimplified.com › java › tutorial › java-while-loop
Java while loop | Programming Simplified
September 6, 2025 - We test a user input and if it's zero then we use "break" to exit or come out of the loop. ... class BreakWhileLoop { public static void main(String[] args) { int n; Scanner input = new Scanner(System.in); while (true) { // Condition in while loop is always true here System.out.println("Input ...
Coderanch
coderanch.com › t › 691813 › java › Loop-program-user-input
While-Loop for ending program from user input (Beginning Java forum at Coderanch)
March 17, 2018 - One often used approach where user input is required is to put the validation check and loop inside a method. Something like Then in the main body of your code you can call it like: And remove all your validation from your main body. Much cleaner. JavaRanch-FAQ HowToAskQuestionsOnJavaRanch ...
BeginwithJava
beginwithjava.com › java › loops › do-while-loop.html
Do While Loop in Java
The do while loop is similar to the while loop with an important difference: the do while loop performs a test after each execution of the loop body. import java.util.Scanner; // needed for Scanner Class /** * This program demonstrate do while loop. */ public class AddNumbers { public static void main(String[] args) { int value; // to hold data entered by the user int sum = 0; // initialize the sum char choice; // to hold 'y' or 'n' // Create a Scanner object for keyboard input...
W3Schools
w3schools.com › java › java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
YouTube
youtube.com › watch
Java Do While Loop Example Where User Prompts to Start Program Over - Appficial - YouTube
More videos coming soon, please SUBSCRIBE!A do-while loop is a post-test loop that executes its body of code at least once, and then repeats while the loop's...
Published October 7, 2017
CodersLegacy
coderslegacy.com › home › learn java › java do while loop
Java do while loop - CodersLegacy
April 28, 2020 - The above code is designed to keep taking input from the user, adding them up in the variable sum until a negative number is input to terminate the loop. The use of the do while loop is important here as the code must run once so that the value of x can be obtained to be matched against the condition. This is contrast to the while loop, which would first check the condition and throw an error since x has no value at that point. If you don’t understand how we’re taking input here, see the Java input guide.
Engineering LibreTexts
eng.libretexts.org › bookshelves › computer science › programming languages › think java - how to think like a computer scientist (downey) › 15: loops
15.6: The do-while Loop - Engineering LibreTexts
October 30, 2025 - We can use a do-while loop to keep ... in.nextDouble(); Although this code looks complicated, it is essentially only three steps: Display a prompt....
Coderanch
coderanch.com › t › 684625 › java › loop-user-input
While loop and user input (Beginning Java forum at Coderanch)
September 12, 2017 - Carey Brown wrote:You'll want to replace your a-- with edd.nextInt(). Every time through the loop you'll want to get another int. Also, variable names should begin with a lower case character, so it should be edd and not Edd. Alright but if i want to now use a text and number, and I want to print the text much as the number specifies, how do I do that? using While Thanks for your help! Hope you dont mind these noob questions. ... Mixing input as a number and as text potentially exposes you to one of the Scanner's gotchas.
Brainly
brainly.com › computers and technology › high school › challenge activity 3.18.1: basic do-while loop with user input.
complete the do-while loop to output 0 to the value of `countlimit` using `printval`. assume the user will only input a positive number. for example, if `countlimit` is 5, the expected output will be `0 1 2 3 4 5`.
```java
import java.util.scanner;
public class counttolimit {
public static void main(string[] args) {
scanner scnr = new scanner(system.in);
int countlimit;
int printval;
// get user input
countlimit = scnr.nextint();
printval = 0;
do {
system.out.print(printval + " ");
printval = printval + 1;
} while (printval
[FREE] CHALLENGE ACTIVITY 3.18.1: Basic do-while loop with user input. Complete the do-while loop to output 0 to - brainly.com
April 4, 2023 - When executed with an input of 5, the output will be '0 1 2 3 4 5'. To complete the do-while loop in the Java program, we want to output the numbers from 0 up to the value of countLimit provided by the user.