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
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 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
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 am fairly new to Java, but my for loop is instantly skipping to the highest possible value in the following code:
System.out.println(i);
for(i = 0; i <= (difficulty - 2); i++);{
System.out.println(i);
nextMineX = (int) (10*Math.random());
nextMineY = (int) (10*Math.random());
for(y = 0; y <= 14; y++){
System.out.println(y);
if(nextMineX == minesX[y] && nextMineY == minesY[y]){
i = i-1;
} else{
minesX[i] = nextMineX;
minesY[i] = nextMineY;
}
}
}
The first for loop is screwing up, while the nested one is running fine. the variable i is initialized as 0, and difficulty is at 16. the output of this excerpt is as follows:
0
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
If anyone can help me with his that would be extremely appreciated. Since I'm new, it is probably something small and basic that I'm overlooking.
The problem is the semicolon at the end of the second line. It's valid to have a for loop with no body. It's also valid to have standalone a block of code inside brackets (this defines a scope for variables--if you defined a variable inside the brackets, it wouldn't be available outside). So Java is interpreting the beginning of your code like this:
for(i = 0; i <= (difficulty - 2); i++); // for loop is done, so i = difficulty - 2
{
System.out.println(i);
...
Your for loop statement ends with the semicolon you have
for(i = 0; i <= (difficulty - 2); i++); <- incorrect
for(i = 0; i <= (difficulty - 2); i++){ <- correct
//body
}
For loop is terminating cause of semicolon ;
for(i = 0; i <= (difficulty - 2); i++); //semicolon terminating loop
{...}
so you should use for loop like this
for(i = 0; i <= (difficulty - 2); i++) //remove semicolon prevent to terminate
{...}
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;
}
}
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 try to print the odd numbers in Java that are inside the array but this algorithm doesn't work ... May someone help me ?
The printing result is that :
"Exception in thread "main" .java.lang.ArrayIndexOutOfBoundsException: 7
at JavaArray.main(JavaArray.java:12)"
Code :
public class JavaArray {
public static void main(String[] args) {
int[] myArray = {1,3,4,5,8,9,10};
int i = 0;
for(i = 0; i < myArray.length; i++); {
if(myArray[i] % 2 == 1) {
System.out.println(myArray[i]);
}
}
}
}
Remove the semi-colon that is terminating your for loop
for (i = 0; i < myArray.length; i++);
^
Because you have placed semicolon after for loop, variable i increments till length of array(here 7). After that loop ends and you are trying to access myarray element through i which is 7 so it is giving out of bound exception.
Besides the extra ; you need to remove, you can consolidate by declaring the int in the loop declaration:
for (int i = 0; i < myArray.length; i++) {
.
.
.
}
Beside #Reimus point , you can also do it like below , sort the array if it's not sorted yet, in your case it is sorted . FYI, Instead of Collections.sort which is above O(N) complexity use a Hash Set.
public static void main(String[] args) {
int[] myArray={1,3,4,5,8,9,10};
Arrays.sort(str);
for (int i = 1; i < myArray.length; i++) {
if (str[i] == str[i - 1]) {
System.out.println("Dupe-num: " + str[i];
}
}
}
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.