5 Replies - 2519 Views - Last Post: 29 November 2011 - 07:22 PM Rate Topic: -----

#1 Randal McSandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

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

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 Randal McSandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • 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 :P

Remember you can get exceptions.
If the user input double values or non number characters, then you will get a InputMismatchException
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1