Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible for this to be put into a for loop while still returning the variables so that the variables could be put into the array in java

This is what I got so far, I’m trying to loop the user input and have the variables from the user input added to the list below. I tried to put the user input part into a loop but was not able to transfer the variables into the array

// inport
import java.util.*;

// Main program
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in); //Scans the user inputs and puts it into array
    System.out.print("Enter a number- ");
    int a = sc.nextInt();
    System.out.print("Enter a number- ");
    int b = sc.nextInt();
    System.out.print("Enter a number- ");
    int c = sc.nextInt();
    System.out.print("Enter a number- ");
    int d = sc.nextInt();
    System.out.print("Enter a number- ");
    int e = sc.nextInt();
    System.out.print("Enter a number- ");
    int f = sc.nextInt();
    System.out.print("Enter a number- ");
    int g = sc.nextInt();
    System.out.print("Enter a number- ");
    int h = sc.nextInt();
    System.out.print("Enter a number- ");
    int i = sc.nextInt();
    System.out.print("Enter a number- ");
    int j = sc.nextInt();

    int[] array = { a, b, c, d, e, f, g, h, i, j };
    
    
  }
}

This is what I tried:

import java.util.*;

class Main {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      Scanner sc = new Scanner(System.in); 
      System.out.print("Enter in a number- ");
      int a = sc.nextInt();
      return a;
    }
    int[] ary = {a, a, a, a, a, a, a, a, a, a};
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The trick is to initialize the array with the number of elements you want to store before collecting the user input.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = new int[10];
        
        int i = 0;
        while (i < array.length) {
            System.out.print("Enter a valid number: ");
            try {
                array[i] = sc.nextInt();
                i++;
            } catch (Exception ex) {
              System.out.println("Error: please provide a valid number");
              sc.nextLine();
            }
        }
        
        // Print out your elements
        for (int element : array) {
            System.out.print(element + " ");
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading