2

I'm trying to convert my String ArrayList into a Double ArrayList and for some reason it is not converting right.

My input:

1 2 3 4

My output:

[1.0]
[1.0, 1.0, 2.0]
[1.0, 1.0, 2.0, 1.0, 2.0, 3.0]
[1.0, 1.0, 2.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0]

Expected Output:

[1.0] [1.0, 2.0] [1.0, 2.0, 3.0] [1.0, 2.0, 3.0, 4.0]

My Code

String inputValue;


List<String> input = new ArrayList<String>();
List<Double> numbers = new ArrayList<Double>();



while((inputValue = stdin.readLine()) != null) {

   input.add(inputValue);

    for(int i = 0; i < input.size(); i++) {
        numbers.add (Double.parseDouble(input.get(i)));
    }
    System.out.println(numbers);
}
5
  • My input: 1 2 3 4 My output: [1.0] [1.0, 1.0, 2.0] [1.0, 1.0, 2.0, 1.0, 2.0, 3.0] [1.0, 1.0, 2.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 4.0] Expected Output: [1.0] [1.0, 2.0] [1.0, 2.0, 3.0] [1.0, 2.0, 3.0, 4.0] Commented Mar 9, 2016 at 6:53
  • 1
    this comment should be in the qustion itself, so we can see it faster and directly... Commented Mar 9, 2016 at 6:54
  • I know but for some reason every time I had it the website thinks its code Commented Mar 9, 2016 at 6:55
  • Shouldn't numbers be a list of lists? Commented Mar 9, 2016 at 6:58
  • Thank you Ori for editing it Commented Mar 9, 2016 at 6:58

1 Answer 1

4

You don't need two loops - you can convert the String to double when you read the input from stdin. Beside that, the output should be printed after the loop in done, once all the numbers are in the lists :

    while((inputValue = stdin.readLine()) != null){
       input.add(inputValue);
       numbers.add (Double.parseDouble(inputValue));
    } 
    System.out.println(numbers);

Actually, I'm not sure you even need the input list.

EDIT :

If you want to print the input as it is added to the Lists, and handle bad input, as well as allow the user to quit the loop by typing enter (i.e. an empty line) :

    while((inputValue = stdin.readLine()) != null && !inputValue.isEmpty()) {
       input.add(inputValue);
       try {
           numbers.add (Double.parseDouble(inputValue));
           System.out.println(numbers);
       }
       catch (NumberFormatException numEx) {
           System.out.println(inputValue + " is not a double");
       }
    } 
Sign up to request clarification or add additional context in comments.

8 Comments

yeah but I need it to print so I know the numbers are going in and even if I enter null or nothing the loops doesn't stop
@swaguire in that case, you simply need to wrap the parseDouble call with a try catch block, to catch NumberFormatException.
Well is there a way to make it so the while loop stops when null is entered?
@swaguire I misunderstood your previous comment. Since you are reading the input from stdin (as opposed to reading it from a file), the program will always with for new input when you call readLine(). Therefore, you should choose some String that indicates the end of the input, such as "q", and leave the loop when that String is entered.
Well for my assignment for this class I need the while loop to stop when null or nothing is entered on the next line. So would I create a string that equals null and then make a parameter saying when something that is equal to that string is entered to stop?
|

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.