0

So i wanted to produce a program in which the user is prompted to input as many Double values as they want, and the program stores these values in an array until the EOF (when the user presses ctl + d), and then prints the sum of these numbers to output. This is what i have so far:

import java.io.*;
import java.util.*;

public class ArrayExample {

 public static void main(String[] args) {

  Scanner keyboard = new Scanner(System.in);
  System.out.print("Enter line: ");
  ArrayList<Double> nums = new ArrayList<Double>();
  double input = keyboard.nextDouble();

  while (input.hasNextLine()) {
   input = keyboard.nextDouble();
  }
  System.out.print(nums);
  double sum = 0;
  for (int i = 0; i < nums.size(); i++) {
   sum+=Integer.parseInt(nums.get(i));
  System.out.println(sum);
  }
 }
}

My questions were how would i use a loop to keep reading input doubles until EOF, is the ArrayList the best way to store the users inputs and is that the most correct way to get the sum? Thank you!

8
  • in your loop do... nums.add(keyboard.nextDouble()); but get rid of the whole line... input = keyboard.nextDouble(); and also you might just want to do keyboard.hasNextDouble(); as your while loop condition... as of right now though your code does not compile because you do.. while(input.hasNextLine()) Also... no need for the sum += Integer.parseInt(nums.get(i)); just simply do... sum += nums.get(i); Commented Apr 16, 2016 at 4:50
  • @3kings It compiles with hasNextLine(), it just won't work at runtime. Commented Apr 16, 2016 at 4:52
  • 2
    @Andreas no... he has... input.hasNextLine() that doesn't make any sense... considering input is a double Commented Apr 16, 2016 at 4:53
  • 1
    @3kings You're right, I just thought you were correcting the hasNextLine() to hasNextDouble(), and didn't notice that you also corrected input to keyboard. Commented Apr 16, 2016 at 4:55
  • Why store the values the user is entering? Your described requirement is for the sum, and you can calculate that as the user enters values. And, Integer.parseInt(nums.get(i)) is not what I would call a best practice. Commented Apr 16, 2016 at 4:57

1 Answer 1

1

Modify the while and for loops as per the snippet below. In the while loop, just use Scanner's hasNextDouble() method directly and add values to your Arraylist as they are entered. In the for loop, you don't need to parse the values to integers since sum is a double which is the datatype of your Arraylist. Arraylist is right for your case since you are not sure of how many values you will get from the user; Arraylists are resizable.

  while (keyboard.hasNextDouble()) {
      nums.add(keyboard.nextDouble());
  }
  keyboard.close();
  System.out.println(nums);
  double sum = 0;
  for (int i = 0; i < nums.size(); i++) {
   sum += nums.get(i);
  }
  System.out.println(sum);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.