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
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 3 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a function that removes all spaces in the given string. This is my code:
fun nonSpaceString(s: String): String {
var result = " "
for (index in 0..s.length-1) {
if (s[index] != ' ') {
result += s[index]
}
}
return result
}
fun main(){
nonSpaceString("abc d e")
}
But the result is holding "a" and then holding "ab" but the end result holds nothing. Does anyone know what I'm doing wrong here?
You have initialized the result with a space character. So the result starts with a space that may confuse you. Apart from that, your function is working fine.
Additionally, you can use the replace function to remove all spaces from a string
"abc d e".replace("\\s+".toRegex(), "")
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?
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 7 years ago.
Improve this question
I'm writing a little application that receives instructions in a StringBuilder filled with "(" and ")", for every ( the application sums 1, for every ( it decreases 1. I know it's a bit weird but it's a puzzle and it had to be done like that.
I'm having problems reading the content from the stringbuilder, Eclipse is complaining that it cannot access the specified index with charAt, what am I doing wrong?
I already checked and StringBuilder instructions is valid and properly filled with ( and ), no nulls or other symbols.
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==")"){
//do something
}
else(){
//do other thing
}
}
To use a Char in Java you use ' not "
so it should be :
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==')'){ //<-- note the change of " to '
//do something
}
else{
//do other thing
}
}
}
Edit 1:
The above solves the complication problem however
as #cricket noted, it should be charAt(i) for correctness of the code
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 ;-)