-1
int oct1 = scan.nextInt();
int oct2 = scan.nextInt();
int oct3 = scan.nextInt();
int oct4 = scan.nextInt();

for(int i=1; i<5; i++){

  System.out.println(oct+i);
}

This is my code and basically, I'm just wondering how I would go about looping through each of the oct variables.

I obviously know that the current print wouldn't work, but is there a way to make it so that a for loop can print each variable without just using four lines to print them all?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 2
    Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Nick Feb 01 '21 at 23:38

6 Answers6

1

Ideally to handle many values and iterate them you should use a List implementation, an ArrayList is one of them. This could be an alternative way, more scalable and efficient.

List<Integer> octList = new ArrayList<Integer>();

octList.add(scan.nextInt());
octList.add(scan.nextInt());
octList.add(scan.nextInt());
octList.add(scan.nextInt());

for (int i = 0; i < octList.size(); i++) {
  System.out.println(octList.get(i));
}
Oleg Valter
  • 6,098
  • 6
  • 22
  • 37
E. Mancebo
  • 443
  • 2
  • 9
0

You can store them in an array, and then reference them based on index:

int[] arr = new int[]{scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()};
for(int i = 0; i < arr.length; i++){
    System.out.println(arr[i]);
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Spectric
  • 5,761
  • 2
  • 6
  • 27
0

Oracle have a very good introductory tutorial on arrays. Oracle Tutorial On Arrays

There are two ways using standard Arrays. In my first solution, I assume the values oct1 ... oct4 have been populated. However, after the following example is another solution that does not assume this and also takes input using Scanner.

    int[] values ={oct1, oct2, oct3, oct4};
    // Two examples:

    // Using a 'for each' loop to display values
    for(int value : values){
        System.out.println(value);
    }

    // Using a standard 'for' loop to display values
    for(int i; i < values.length; i++){
      System.out.println(values[i]);
    }

This solution uses an array and reads into it using the Scanner, but you must know how many values are to be read in advance or else you will need to learn lists.

   int count = 4;
   int[] values = new int[count];
   Scanner scan = new Scanner(System.in);

   // Using a standard for loop to read in values
   for(int i; i < values.length; i++){
     System.out.println("Enter a number");
     values[i] = scan.nextInt();
   }

   // Using a standard 'for' loop. Assume this
   // would come later in the program because
   // you would otherwise read in and display 
   // the values in a single loop.
   for(int i; i < values.length;i++){
     System.out.println(values[i]);
   }
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Andrew S
  • 2,390
  • 2
  • 27
  • 44
0

For local variables and method parameter it is not possible. You can use an Array or a List to store the values and iterate.

For instance, using array,

// Declare an array of size 4
int oct[] = new int[4];

// Take input from user 4 times
for(int i = 0; i < 4; i++){
    oct[i] = scan.nextInt();
}

// Print the array
for(int i = 0; i < 4; i++){
    System.out.println(oct[i]);
}

Also check Is there any way to loop though variable names?.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
XO56
  • 307
  • 1
  • 10
0

There are two ways using standard Arrays. In my first solution, I assume the values oct1 ... oct4 have been populated. However, below is a solution that does not assume this.

    int[] values ={oct1, oct2, oct3, oct4};
    //Two Examples:

    //Using a for each loop
    for(int value : values){
        System.out.println(value);
    }
    
    //Using a standard for loop 
    for(int i; i < values.length;i++){
      System.out.println(values[i]);
    }

This solution assumes no array, but you must know how many values are to be read in advance or else you will need to learn lists. Remember to import scanner with import java,util.Scanner; at the top of the class file you are using this code.

   int count = 4;
   int[] values = new[count];
   Scanner scan = new Scanner(System.in);
        
    
   //Using a standard for loop
   //Assume this would go later in the
   //program or else use a single loop
   //to read in and display the values.
    for(int i; i < values.length;i++){
      System.out.println("Enter a number");
      values[i] = scan.nextInt();
    }
  
    //Using a standard for loop
    for(int i; i < values.length;i++){
      System.out.println(values[i]);
    }
Andrew S
  • 2,390
  • 2
  • 27
  • 44
0

Use a map to name the values:

Map<String, Integer> values = new LinkedHashMap<>();
for (int i = 1; i < 5; i++) {
    values.put("oct" + i, scan.nextInt());
}

for (Map.Entry<String, Integer> value : values) {
   System.out.println(value.getValue());
}

Or to output their name too:

for (Map.Entry<String, Integer> value : values) {
   System.out.println(value.getKey() + " = " + value.getValue());
}

And to get them:

int v = values.get("oct2");
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Bohemian
  • 365,064
  • 84
  • 522
  • 658