Scanner scan = new Scanner(System.in); double numbers = scan.nextDouble(); double[] avg =..????
For int array you can try: Scanner scan = new Scanner(System.in); int[] arr = Arrays. stream(scan. nextLine() .
To read a single character, we use next(). charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string. In this case, the scanner object will read the entire line and divides the string into tokens: “How”, “are” and “you”.
You could try something like this:
public static void main (String[] args) { Scanner input = new Scanner(System.in); double[] numbers = new double[5]; for (int i = 0; i < numbers.length; i++) { System.out.println("Please enter number"); numbers[i] = input.nextDouble(); } }
It seems pretty basic stuff unless I am misunderstanding you
You can get all the doubles with this code:
List<Double> numbers = new ArrayList<Double>(); while (scan.hasNextDouble()) { numbers.add(scan.nextDouble()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With