Author Topic: For loop  (Read 2783 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
For loop
« on: 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:

Code: (java) [Select]
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?
Vae Victis - suffering to the conquered

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: For loop
« Reply #1 on: 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.
« Last Edit: April 11, 2014, 11:55:30 am by Kulverstukas »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: For loop
« Reply #2 on: 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:

Code: (java) [Select]
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]);
}
}
}
Vae Victis - suffering to the conquered

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: For loop
« Reply #3 on: April 11, 2014, 01:27:23 pm »
That's error I get. If that's what you asked for =)
Vae Victis - suffering to the conquered

Offline sherlock

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 2
    • View Profile
Re: For loop
« Reply #4 on: 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

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: For loop
« Reply #5 on: 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
« Last Edit: April 11, 2014, 03:51:16 pm by Deque »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: For loop
« Reply #6 on: 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.

Vae Victis - suffering to the conquered

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: For loop
« Reply #7 on: April 11, 2014, 08:29:52 pm »
Obviously you are not using the good version of Java. The error is pretty clear.
Satan911
Evilzone Network Administrator

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: For loop
« Reply #8 on: 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.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: For loop
« Reply #9 on: 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
Vae Victis - suffering to the conquered

Offline sherlock

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 2
    • View Profile
Re: For loop
« Reply #10 on: 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

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: For loop
« Reply #11 on: 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.
« Last Edit: April 13, 2014, 08:25:23 am by Deque »

Offline sherlock

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 2
    • View Profile
Re: For loop
« Reply #12 on: 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 :)