1

Why does this statement give me an error in the program mentioned below? for(int y=0, int z=0; z

The program I used it in is ::

class Testloops
{
     public static void main(String[] args)
     {
         int[] x={ 7,6,5,2,8,9,3};
          for(int y=0, int z=0; z<x.length;z++)
          {
              y= x[z];
              System.out.println(y+ " ");
          }
     }
}
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
sasu
  • 411
  • 1
  • 4
  • 4

4 Answers4

2

Get rid of the second int declaration.

for (int y = 0, z = 0; z < x.length; z++) {
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

You are almost there, remove the int type before z=0, then it will work!

for(int y=0, z=0; z < x.length ;z++)

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0

You only need the first int, see a working example here:

http://ideone.com/wGDCIE

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

You can do this

  • Declare Y outside the for statement

     int y = 0; 
     int[] x={ 7,6,5,2,8,9,3};
    
     for (int z=0;z<x.length;z++)
     {
          y= x[z];
          System.out.println(y+ " ");
      }
    

Not sure why you are declaring y inside the for statement, as its not being iterated?

ssayyed
  • 766
  • 1
  • 8
  • 13