Input and store an arbitrarily long sequence of numbers

Viewed 146

So, this is basically what I have to do:

Ask the user to enter as many ints as they want, and tell them to type “done” when they are done entering the numbers.
Store these numbers in a data structure. (Think carefully about which one to use.)
Write a method that computes the sum of all the elements in the data structure.
Write a method that finds the smallest positive number in the data structure (return 9999 if there aren’t any positive numbers).
Call these methods on the data structure with the user’s numbers.

I have already written some of the code, but it's taking me no where.

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            if (in.hasNextInt())
                System.out.println(in.nextInt());
            else
                in.next();
        }
    }
}
2 Answers

Here is a change that will lead you into the right direction:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNext()) {
        if (in.hasNextInt()) {
            addNumber(in.nextInt());
        } else {
            final String string = in.next();
            if ("done".equals(string.toLowerCase())) {
                compute();
            } else {
                System.err.println("Invalid input " + string + ". Aborting...");
            }
            return;
        }
    }
}

private static void compute() {
    // Do your magic here
}

private static void addNumber(int number) {
    // Change this to code that stores the number
    System.out.println(number);
}

EDIT: Some insight in what has changed. The very basic structure you had was alright. I mean, you got the right idea. I added the addNumber() method that will be called every time you get a number in the input. Now, inside that method you can write code for storing the value in a set or list maybe. Then, if the input doesn't get a integer we just get whatever it is as a string a compare to "done". If it's equal we call compute() method. Inside that method you should write code that computed whatever calculation you are requested to do. Using the values in the set/list. If the string isn't "done" we consider that an error.

By using the Java8 stream we could write this more efficiently and in a fewer lines of code than using the equivalent for loop.

import java.util.*;

class Main {
    // Returns an array containing all inputted integers.
    public static int[] getInts() {
        List<Integer> list = new ArrayList<Integer>();
        System.out.println("Enter integers or done to exit and press enter: ");
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            if (in.hasNextInt())
                list.add(in.nextInt());
            else if (in.next().equals("done"))
                break;
        }
        return list.stream().mapToInt(i->i).toArray();
    }

    // Returns the sum of the integers, if the array is empty it returns 0.
    public static int sum(int[] arr) {
        return Arrays.stream(arr).sum();
    }

    // Returns 9999 if the array is empty or only has negative integers.
    public static int min(int[] arr) {
        return Arrays.stream(arr).filter(i -> i > 0).min().orElse(9999);
    }

    public static void main(String[] args) {
        int[] arr = getInts();
        System.out.println(Arrays.toString(arr));
        System.out.println(sum(arr));
        System.out.println(min(arr));
    }
}

Output:

Enter integers or done to exit and press enter: 
5 3 1 done // Our Input.
[5, 3, 1]
9
1
Related