While infinite (Restriction) loop is not working properly [closed] - java

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

Related

Why am I not getting Output? Can anyone help me? [closed]

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 have declared a variable of type long and I want to print the statements if the given variable lies between the range specified -
class Sample {
public static void main(String[] args) {
long n = 3454546L;
if (-2147483648L<=n && n>=2147483647L) {
System.out.println("* int");
System.out.println("* long");
}
}
}
This is the code.
Because your logic is is n >= -2147483648L true and n >= 2147483647L false so overall false?
Think you wanted
-2147483648L<= n && n<=2147483647L
Your second part being n>=2147483647L is not matching, thus you have false and will not go into the statements, i guess it should be n<=2147483647L?

I want my loop to end at the given number in java [closed]

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.

Problems with a string loop [closed]

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
}

I am getting a "not a statement" error on a for each loop [closed]

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 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)

Why am I getting cannot find symbol: in.nextInt error? [closed]

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 8 years ago.
Improve this question
I'm trying to write a little game in Java and it keeps popping up error:
cannot find symbol
on in.nextInt. I've got other in.nextInts in my code that work fine so I'm a little stumped.
import java.util.Scanner;
public class Player
{
int difficult = 3;
public Player()
{
}
public void SetDifficult()
{
Scanner in = new Scanner(System.in);
System.out.println("choose your difficulty:\n1. simple\n2. easy\n3. normal\n4. hard\n5. impossible");
difficult = in.nextint();
...
}
What is the reason for getting this error ?
you have:
in.nextint()
you want:
in.nextInt()
with a capital I

Categories

Resources