EvilZone
Programming and Scripting => Java => : Code.Illusionist April 11, 2014, 11:48:30 AM
-
Attention: Noobish question.
So I was watching this tutorial here: http://www.tutorialspoint.com/java/java_loop_control.htm
and when I reached Enhanced for loop in Java , there is some problem. I do the same as they did but whenever I run application it seems there is error. Here is my code:
public class Apples {
public static void main(String args[]) {
int [] numbers = {1,2,3,4};
for(int number : numbers) {
System.out.println(number);
}
}
}
Does anyone have idea why this doesn't work?
-
System.out.println(); accepts a String as input, so just do System.out.println("" + number); to cast an int to a String.
-
I just tested it that way and still doesn't work. Btw, System.out.println can print integers because I can make application like this:
public class Apples {
public static void main(String args[]) {
int [] numbers = {1,2,3,4};
for(int x = 0; x <4;x++) {
System.out.println(numbers[x]);
}
}
}
-
That's error I get. If that's what you asked for =)
(http://www.dodaj.rs/f/22/Gd/N9G1Vxl/2014-04-11132601.jpg)
-
Rightclick the project > Properties > Java Build Path > Libraries > which version of the jdk are you using in this project?
=> Make sure using jdk.1.7.0_51
Also check properties > Java Compiler > which compiler compilance level did you choose?
=> Should be 1.7
-
System.out.println(); accepts a String as input, so just do System.out.println("" + number); to cast an int to a String.
See: http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println%28int%29
The println method is overloaded with all possible types.
@Code.Illusionist
This is indeed a problem of the Java compiler version used. The error message tells clearly that you need 1.5 or greater. However, I don't get why this is below 1.5 to begin with. SE 1.5 is 10 years old.
The newest version is 1.8
Edit: And please stop watching thenewboston
-
I already set right version. I downloaded Java SE 8 and set everything right. But this virus probably disable all that again. Reinstalling system again.
(http://www.dodaj.rs/f/E/yj/3YvIJMsC/2014-04-11162655.jpg)
-
Obviously you are not using the good version of Java. The error is pretty clear.
-
What exactly did you do?
Just downloading and installing a JDK is not enough. You need to change the settings in Eclipse.
Also: Eclipse ships with 1.7. There should be an option for a newer version already.
-
What exactly did you do?
Just downloading and installing a JDK is not enough. You need to change the settings in Eclipse.
Also: Eclipse ships with 1.7. There should be an option for a newer version already.
Thank you man. This the new boston tutorials are the worst as I can see now. So stupid. Now it works. Thanks +1
-
What exactly did you do?
Just downloading and installing a JDK is not enough. You need to change the settings in Eclipse.
Also: Eclipse ships with 1.7. There should be an option for a newer version already.
Thank you man. This the new boston tutorials are the worst as I can see now. So stupid. Now it works. Thanks +1
Actually there is no option for a newer version, until you install the plugin "Java 8 support for Eclipse Kepler SR2" in the Eclipse Marketplace
-
Actually there is no option for a newer version, until you install the plugin "Java 8 support for Eclipse Kepler SR2" in the Eclipse Marketplace
I was talking about Java 7. I meant "newer" compared to the version he was using before.
-
I was talking about Java 7. I meant "newer" compared to the version he was using before.
I see, makes sense now :)