0

Is there a way to simplify this code? I tried iterating around just one for loop, converting the loop variable into a String and concatenating it with the variables arreglo2, 3 and 4; but it doesn't work. It doesn't even allow me to concatenate the variables ...

Indice = 0;
System.out.println("\nAge Range from 20 to 29\n");
for ( x = 0 ; x < arreglo2.length ; x++)
{
  indice = x + 1;
  System.out.println(indice + ". " + arreglo2[ x ]);
}
indice = 0;
System.out.println("\nAge Range from 30 to 39\n");
for ( x = 0 ; x < arreglo3.length ; x++)
{
  indice = x + 1;
  System.out.println(indice + ". " + arreglo3[ x ]);
}
indice = 0;
System.out.println("\nAge Range from 40 to 49\n");
for ( x = 0 ; x < arreglo4.length ; x++)
{
  indice = x + 1;
  System.out.println(indice + ". " + arreglo4[ x ]);
}

Thanks a bunch...

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jwenting
0
int indice = 0;
System.out.println("\nAge Range from 20 to 29\n");
for (String x: arreglo) { System.out.println(++indice + "." + x); }

And similarly for the other loops.
Turn that into a method and call it several times instead of writing it out several times.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.