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
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 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 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
}
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 ;-)