EvilZone

Programming and Scripting => Java => : Code.Illusionist April 11, 2014, 11:48:30 AM

: For loop
: 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:

: (java)
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?
: Re: For loop
: Kulverstukas April 11, 2014, 11:55:03 AM
System.out.println(); accepts a String as input, so just do System.out.println("" + number); to cast an int to a String.
: Re: For loop
: Code.Illusionist April 11, 2014, 01:18:27 PM
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:

: (java)
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]);
}
}
}
: Re: For loop
: Code.Illusionist April 11, 2014, 01:27:23 PM
That's error I get. If that's what you asked for =)
(http://www.dodaj.rs/f/22/Gd/N9G1Vxl/2014-04-11132601.jpg)
: Re: For loop
: sherlock April 11, 2014, 02:49:25 PM
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
: Re: For loop
: Deque April 11, 2014, 03:43:14 PM
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
: Re: For loop
: Code.Illusionist April 11, 2014, 04:29:54 PM
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)
: Re: For loop
: Satan911 April 11, 2014, 08:29:52 PM
Obviously you are not using the good version of Java. The error is pretty clear.
: Re: For loop
: Deque April 12, 2014, 11:03:45 AM
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.
: Re: For loop
: Code.Illusionist April 12, 2014, 12:43:34 PM
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
: Re: For loop
: sherlock April 12, 2014, 07:38:51 PM
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
: Re: For loop
: Deque April 13, 2014, 08:20:06 AM
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.
: Re: For loop
: sherlock April 13, 2014, 12:09:28 PM
I was talking about Java 7. I meant "newer" compared to the version he was using before.
I see, makes sense now :)