# Prompting user to enter double values into array.

Page 1 of 1

## 5 Replies - 3525 Views - Last Post: 29 November 2011 - 07:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257732&amp;s=48d67c82e532c59dd31d5d8ffcd99faa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Randal McSandal

Reputation: 0
• Posts: 9
• Joined: 25-September 11

# Prompting user to enter double values into array.

Posted 29 November 2011 - 06:54 PM

Basically I need to have a 'test' program that asks the user to enter 10 double values, that will then put those into an array. (list1, list2).

Here's my test program:

```
public static void main(String[] args) {
int[] list1 = {1, 2, 3, 4, 5, 6};
double[] list2 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

System.out.println(SixEightMeth.average(list1));
System.out.println(SixEightMeth.average(list2));
}
}

```

Here's my averaging methods:
```  public static int average(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++)
sum += array[i];
return sum / array.length;
}

public static double average(double[] array) {
double sum = 0;
for (int i = 0; i < array.length; i++)
sum += array[i];
return sum / array.length;
}
}

```

It outputs:
3
3.5

Right now, which is correct as the arrays only contain 1-6. My question is how do I get the user to input their own 10 numbers to list1 & list2.

Is This A Good Question/Topic? 0

## Replies To: Prompting user to enter double values into array.

### #2 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Prompting user to enter double values into array.

Posted 29 November 2011 - 06:58 PM

You will need a Scanner object to do the job(nextInt() and nextDouble() will be your need).

Just create your two empty array and ask user to enter those values and store them in array. Probably you will need loop to iterate in your array indexes.

### #3 CasiOo

• D.I.C Lover

Reputation: 1514
• Posts: 3,375
• Joined: 05-April 11

## Re: Prompting user to enter double values into array.

Posted 29 November 2011 - 07:03 PM

The easiest for beginners is to use the Scanner class.
It parses the user's input for you.
http://docs.oracle.c...il/Scanner.html

You can either use Scanner.nextInt() which will parse the next token as an int and give it to you, or you could just read in a whole String (with all the numbers) and parse the String yourself.

```Scanner scanner = new Scanner(System.in); //System.in is out inputstream

System.out.println("Input 6 numbers separated by space");

//Blocks the execution until the makes input
//It reads until an return or new line character has been found
String input = scanner.nextLine();

String[] numbers = input.split(" "); //Split the String at every space

int[] list1 = new int[6];

//Put the values into the array and parse the input to int
for (int i=0; i<numbers.length; i++) {
list1[i] = Integer.parse(numbers[i]);
}

```

This post has been edited by CasiOo: 29 November 2011 - 07:19 PM

### #4 Randal McSandal

Reputation: 0
• Posts: 9
• Joined: 25-September 11

## Re: Prompting user to enter double values into array.

Posted 29 November 2011 - 07:17 PM

Thank you both for your help.

```import java.util.Scanner;
public class SixEight {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Input 10 numbers separated by space: ");

String input = scanner.nextLine();

String[] numbers = input.split(" ");

int[] list1 = new int[10];
double[] list2 = new double[10];

//Put the values into the array and parse the input to String
for (int i=0; i<numbers.length; i++) {
list1[i] = Integer.parseInt(numbers[i]);
list2[i] = Integer.parseInt(numbers[i]);
}

System.out.println(SixEightMeth.average(list1));
System.out.println(SixEightMeth.average(list2));
}
}

```

That is now my test program and it works!

Anything else I should touch up?

### #5 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Prompting user to enter double values into array.

Posted 29 November 2011 - 07:20 PM

Wait, you ask user for 10 numbers and then you put the same numbers in both arrays:
```list1[i] = Integer.parseInt(numbers[i]);
list2[i] = Integer.parseInt(numbers[i]);
```

Did you mean it?

### #6 CasiOo

• D.I.C Lover

Reputation: 1514
• Posts: 3,375
• Joined: 05-April 11

## Re: Prompting user to enter double values into array.

Posted 29 November 2011 - 07:22 PM

it should say parse the input to int and not String

Remember you can get exceptions.
If the user input double values or non number characters, then you will get a InputMismatchException