0

I was going all over the internet looking for something to help me with this problem. I learned how to make an arrayList, but i want to know how to make it acceptable user input. What i mean by that is that i want the user to input his number. Here is what i have:

public class MyClass {    
    public static void main(String[] args) {    
        Scanner input=new Scanner(System.in);    
        ArrayList<Integer> myList=new ArrayList<Integer>(10);    
        System.out.println("Enter your number:");    
        myList.add(416355);    
        myList.add(21212);    
        for(int x : myList)    
            System.out.println(x);    
        System.out.println("Size="+myList.size());    

    }
  }

What i have now is the numbers i have put in there. Any help is greatly appreciated.

Thanks in advance.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
  • 1
    Possible duplicate of [How can I get the user input in Java?](https://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java) – Yahya Jun 11 '17 at 19:46

5 Answers5

0
public class MyClass {    
    public static void main(String[] args) {    
        Scanner input=new Scanner(System.in);    
        ArrayList<Integer> myList=new ArrayList<Integer>(10);    
        System.out.println("Enter your number:");  
        int totalNumbers = 2;
        for (int i = 0; i < totalNumber; i++) {
            myList.add(input.nextInt());
        }
        for(int x : myList)    
            System.out.println(x);    
        System.out.println("Size="+myList.size());    

    }
  }
sumitb.mdi
  • 1,010
  • 14
  • 17
0

These two lines of code are necessary to get the user input into the ArrayList:

int n = input.nextInt();
myList.add(n);

So the full code would read:

public class MyClass {     
    public static void main(String[] args) {    
        Scanner input=new Scanner(System.in);    
        ArrayList<Integer> myList=new ArrayList<Integer>(10);    
        System.out.println("Enter your number:");
        int n = input.nextInt();
        myList.add(n);
        myList.add(416355);    
        myList.add(21212);    
        for(int x : myList)    
            System.out.println(x);    
        System.out.println("Size="+myList.size());    

    } 
  }

You might find this helpful: How can I get the user input in Java?

Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
0
    import java.util.ArrayList;
    import java.util.Scanner;

    public class MyClass {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            ArrayList<Integer> myList = new ArrayList<Integer>(10);

            int totalNumbers = 10;
            for (int i = 0; i < totalNumbers; i++) {
                System.out.println("ENTER Element to add into list");
                Scanner element = new Scanner(System.in);
                int ele = (element .nextInt());
                myList.add(ele);
            }
            for (int x : myList)
                System.out.println(x);
            System.out.println("Size=" + myList.size());

        }
    }
So the full code would read:
Akash
  • 587
  • 5
  • 12
0

As I understood you want to capture user data and save it into an array;for each input element you need to create a variable:

import java.util.ArrayList;
import java.util.Scanner;

public class MyClass {    
    public static void main(String[] args) {        
        ArrayList<Integer> myList=new ArrayList<Integer>(10);    enter code here
        System.out.println("Enter your number:");   
        Scanner input=new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        myList.add(a);
        myList.add(b);
        for(int x : myList)    
            System.out.println(x);    
        System.out.println("Size="+myList.size());    

    }
  }
ffer
  • 38
  • 6
0
import java.util.ArrayList;
import java.util.Scanner;

public class Average
{

   public static void main(String[] args)
   {
      ArrayList<Double> numbers = new ArrayList<Double>();

      Scanner in = new Scanner(System.in);
      System.out.println("Please enter a list of numbers: ");

      while (in.hasNextDouble())
      {
         double input = in.nextDouble();
         numbers.add(input);
      }

      if (numbers.size() == 0)
      {
         System.out.println("No average.");

      } 

      else
      {
         double total = 0;
         for (double element : numbers)
         {
            total = total + element;
         }
         double average = total / numbers.size();
         System.out.println("The average is: " + average);

      }
   }
Saubhagya
  • 155
  • 12