Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to build a loop where the console keeps asking me for a string until I write the word "out". This is what I have till now, but it doesn't loop and I can't figure out why. Also I really want to use while or do while if possible.
Scanner input = new Scanner(System.in);
String name = input.nextLine();
while (!name.equals("out")) {
System.out.println(name);
break;
}
You're not updating name inside the loop and remove the break otherwise you're jumping out of the loop in the first iteration (which is not what you want)
while (!name.equals("out")) {
System.out.println(name);
name = input.nextLine(); // I've added it here for you
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to key in a value(int) as a parameter and make a loop from zero until it reaches that number.
Scanner s = new Scanner(System.in);
int m=0;
int a;
a=s.nextInt();
while(a<=10) {
System.out.println(m); m++;
}
while loop must be like this:
while(m<=a) {
...
}
In while condition you need to add m<=10 not a.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
as one of my first java projects, I want to build a mortgage calc.
I was trying to restrict the duration that the user can pick using this code
Scanner numOfPayments = new Scanner(System.in);
while (true) {
System.out.print("Enter number of payments (the number of Years you will be paying the loan):");
short numOfPayments2 = numOfPayments.nextShort();
if (numOfPayments2 >= 1 && numOfPayments3 <= 30) {
numOfPayments3 = (short) (numOfPayments2 * 12);
break;
}
System.out.println("Enter a valid number of years(Between 1 and 30)");
}
It worked with restricting other values, but not for this one.
Thanks in advance
Thanks for #azro for the answer
I should just change numOfPayments3 to numOfPayments2 inside the If statement
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to find the palindrome of a string.
public static void main(String[] args)
{
String s1 = "eye",s2="";
for(int i = s1.length()-1;i<=0;--i)
{
s2 =s2+String.valueOf(s1.charAt(i));
}
System.out.println(s1);
System.out.println(s2);
}
}
I expected the output
eye
eye
but, s2 isn't printing.
You have the wrong condition in the for loop. The correct condition should be i>=0.
Remember that as long as this condition is true, the for loop will run. Your original condition, i<=0 is false at the very beginning, when i is 2, so the for loop never starts.
A less important problem is that you should not concatenate strings in a for loop, and should use a StringBuilder instead. See this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
In this code, the user types in yes if they would like to play again
I have to use functions for this code, I cannot simply state while(again.equals("yes"));.
Part of code:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
My question is why is }while(running==true); a syntax error? I declared the variable above it, shouldn't that allow it to run?
You need a "do" statement first, i.e. do { statements; } while (condition). Also
you don't need to save the return from playAgain() into a variable, you can call it directly from the while().
There are many Java tutorials out there, try the official Oracle ones from starters: The while and do-while Statements
Update
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
how can I ask the same question after I use break?
System.out.print("Employee Name: ");
sEmployee = input.nextLine();
boolean bInputOk = checkValue(sEmployee,0);
while(!bInputOk)
{
bInputOk = checkValue(sEmployee,0);
System.out.print("Enter a valid name");
break;
}
break breaks out of the loop. You want to only leave the loop if bInputOk == true. You already have that in your loop condition, and you can just drop the break. Also, you're going to want to actually prompt for input again, otherwise you'll just keep reusing the same invalid input (as Aduait Pokhriyal points out in question comments), e.g.:
while(!bInputOk) {
sEmployee = input.nextLine();
bInputOk = checkValue(sEmployee,0);
System.out.print("Enter a valid name");
}
By the way, you probably want to reverse the order of the print and the prompt there, otherwise your input and error messages will get a little confusing:
while(!bInputOk) {
System.out.print("Enter a valid name");
sEmployee = input.nextLine();
bInputOk = checkValue(sEmployee,0);
}
In general, your program will do exactly what you tell it to do. If you tell it to break out of the loop, it will do just that. The reason your original attempt behaved the way it did is because you told it to behave that way.