x cannot be resolved as a variable [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 7 years ago.
Improve this question
What is wrong with this loop?
int index = 0;
for(int x = 0; x < winDate.length;x++);
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
I've used x several times before in comparing values.

You have got a ";" behind the loop!
In fact this loop does nothing instead of counting.
The part below the loop is getting to initialized statically.
int index = 0;
for(int x = 0; x < winDate.length;x++)-->;<---
{
if(userDate == winDate[x])
{
index = x;
break;
}
}

Related

Why does it print 12345 not 01234? [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 months ago.
Improve this question
It has to be output like this 01234
for(int i = 0;i < 5; i++, System.out.print(i));
or
for(int i = 0;i < 5; System.out.print(i))
i++;
Output: 12345
for(int i = 0;i < 5; System.out.print(i))
i++;
Is equivalent to the following while loop:
{
int i = 0;
while (i < 5) {
i += 1;
System.out.print(i);
}
}
i is always incremented before it is printed. Same goes for i++, System.out.print(i): i is already incremented before it is printed. And I don't need to mention that 0 + 1 = 1.
It's pretty easy, you just need to understand what you're writing.
It should go like:
for(int i=0;i<5;i++){ //"i" is increased at the end of the loop
System.out.print(i) //prints "i", which is zero and will increase up to 4
}
And that's it

Wanting to understand why this for loop will not work 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 5 years ago.
Improve this question
Was doing my assignment and tried doing a multi variable for loop where both value have the same increment. However, compiler is giving me an error saying im missing ) ; etc. I dont understand and hope someone can clarify for me. Thanks
for(int i = arr.length - 1; i >0; i--){
String temp = "";
for (int j =0 && int m = i; m < arr.length; j++, m ++){
temp = temp + arr[m][j];
}
diagonalArray.add(temp);
}
Tried changing the && to , but i get error: <identifier> expected
as my error.
int j =0 && int m = i
should be
int j = 0, m = i

Object not showing up anymore, I don't know why [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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.
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.
Improve this question
I had the enemy as a array[] but I recently changed it to an arrayList[] but for some reason nothing shows up anymore, any ideas?
ArrayList<Character> enemy = new ArrayList<Character>(20);
Character enty;
String[] eny = {"Eny_.png" , "eny2.png", "eny3.png"};
for (int i = 0; i < enemy.size(); i ++) {
int ranX = 700 + (int)(Math.random() * 2000);
int ranY = 100 + (int)(Math.random() * 360);
int randE = (int)(Math.random() * 2);
enty = new Character(ranX, ranY,0,0,100,25);
enty.setImage(eny[randE]);
enemy.add(i, enty);
}
for (int i = 0; i < enemy.size(); i ++) {
int ranSpeed = -1 + (int)(Math.random() * -2);
System.out.println(ranSpeed);
enemy.get(i).setVelX(ranSpeed);
enemy.get(i).getVelX();
repaint();
}
}
Because the 20 in
ArrayList<Character> enemy = new ArrayList<Character>(20)
Is the initial capacity, not the initial size. So,
for (int i = 0; i < enemy.size(); i ++) {
is not doing anything because enemy.size() is zero.
The constructor new ArrayList<Character>(20) does not create an ArrayList of size 20, it constructs an empty ArrayList with an initial capacity of 20. So, in your first loop, enemy.size() equals 0.
If you want to add 20 enemies, just use the constant 20 in your loop.

getting String out of bounds exception [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 6 years ago.
Improve this question
the code compiles and also works for any string that is passed but it doesn't work for checking the vowels. It throws a String out of bounds error and i have no idea why. the check for consonants is working.
Here is the code:
public String catchword(String word){
int x = 0;
for(x=0; x<word.length()+1; x++){
boolean v = Vowel(word.charAt(x));
boolean c = Consonant(word.charAt(x));
if (x<word.length()-1){
v = Vowel(word.charAt(x+1));
} else{
v = true;
}
if (c == true && v == true){
word = word.substring(0,x+1) + "op" + word.substring(x+1,word.length());
x = x+3;
}
}
System.out.print(word);
return word;
}
In for(x=0; x<word.length()+1; x++)
x<word.length()+1
Should be
x<word.length()-1
The max index of word is word.length()-1
You will also have problem in word.charAt(x+1). word.length() - 1 is the max index, when x will be equal to it you will get again IndexOutOfBoundsException.

Java loop can't iterate down string length [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
Here is some basic code:
public static void main(String[] args) {
String string = "Hello!";
System.out.println("First loop.");
for (int i = 0; i < string.length(); i++) {
System.out.println("g");
}
System.out.println("Second loop.");
for (int i = (string.length() - 1); i <= 0; i--) {
System.out.println("g");
}
}
For some reason, the program won't go through the second loop at all. This is somewhat strange. Can you explain this, and how to fix it?
Your second loop should be looping backwards, while the index is still greater than or equal to zero, not less than or equal to zero. With <= 0, i is greater than zero on the first evaluation and the loop never runs.
Try:
for (int i = (string.length() - 1); i >= 0; i--) {
Change the for condition,the i is initial with value greater than 0 (length-1) and there is condition i <= 0 which is true in case length is equal to 1.But the length of string is 6 so change the condition as below :
for (int i = (string.length() - 1); i >= 0; i--) {
System.out.println("g");
}
Your problem is the condition in the second for loop
i <= 0
never happens. I don't understand why you would want to check that.

Categories

Resources