-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?

1

6 Answers 6

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

List<Object> not List<primitive>
Also, see this Q&A for details on the above.
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]);
}

Comments

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?.

Comments

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]);
   }

Comments

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");

Comments

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]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.