Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a Scanner input into an array... for example a couple of numbers

Tags:

Scanner scan = new Scanner(System.in); double numbers = scan.nextDouble(); double[] avg =..???? 
like image 692
wam090 Avatar asked May 08 '10 19:05

wam090


People also ask

How do I scan inputs into an array?

For int array you can try: Scanner scan = new Scanner(System.in); int[] arr = Arrays. stream(scan. nextLine() .

How do you use Scanner to input?

To read a single character, we use next(). charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string. In this case, the scanner object will read the entire line and divides the string into tokens: “How”, “are” and “you”.


2 Answers

You could try something like this:

public static void main (String[] args) {     Scanner input = new Scanner(System.in);     double[] numbers = new double[5];      for (int i = 0; i < numbers.length; i++)     {         System.out.println("Please enter number");         numbers[i] = input.nextDouble();     } } 

It seems pretty basic stuff unless I am misunderstanding you

like image 157
npinti Avatar answered Sep 29 '22 21:09

npinti


You can get all the doubles with this code:

List<Double> numbers = new ArrayList<Double>(); while (scan.hasNextDouble()) {     numbers.add(scan.nextDouble()); } 
like image 40
Felipe Cypriano Avatar answered Sep 29 '22 22:09

Felipe Cypriano