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?
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 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 6 years ago.
Improve this question
When I fill an array of characters I get an error.
List<Character> letters = new ArrayList<>();
int i = 0;
if (i == 0) {
Character str = 'a';
letters.add(str);
} else if (i == 1)
Character str = 'b'; //Variable declaration not allowed here
letters.add(str);
}
Why does this error message appear? Cases i==0 and i==1 are mutually
exclusive. Why compiler doesn't allow me to decare str the second time?
Scope in not overlapping, this is correct. You are forgetting an opening curly bracket at else if line. This is just a typo that looks like
} else if (i == 1){ fixes a problem.
Your situation is distinctly different from JAVA Variable declaration not allowed here
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 ;-)