My IntelliJ fails to give correct out put for the following Very Very Simple Program!!!!!!!!!!!!!!!!!!!!!!!!!!!

Answered

// i used the following code //

import java.util.Scanner;

class Main {
public static void main(String[] args) {
String[] guest = new String[3];
Scanner in = new Scanner(System.in);
guest[0] = in.next();
guest[1] = in.next();
guest[2] = in.next();


for(int i=2; i<=0; i--){
System.out.println(guest[i]);
}

}
}


it was intended to print values of defined array in reverse order but surprisingly
it did not give the desire out put !!!!!!!!
i verified my code in "Net beans".


i am adding the screen shot !!! Can any one tell why is this happening????????????????



1
3 comments

I believe you need to modify your 'for' condition to be i>=0 since you are iterating in the reverse direction. The loop should look like this:

for (int i = guest.length - 1; i >= 0; i--) {

}

See this thread for related discussion.

Tip: Typing myArray.forr and hitting Tab will generate the reverse 'for' loop for any of your arrays automatically.

0

// I TRIED IT BUT THE ERROR IS SAME//

0

How are you running it? Are there any other files in your project named 'Main.java'?

Try running the main method explicitly by clicking on the corresponding button in the editor gutter.

0

Please sign in to leave a comment.