1

I'm not sure what to title this question(if anyone has input on what to name the question, please let me know). My program asks the user for 5 int and 5 doubles. Then those numbers are put in an array and passes it to a method to get the average. My question is if I separate the user input by spaces and press enter(like so, 5 space 6...enter; it allows me to enter more than what is allowed in the array index. Why doesn't it give you a error? and how do you prevent this? Also any advice on how I write code would be helpful too!

Here is the code.

import java.util.*;

public class Lab7A_BRC{
 public static void main(String[] args) {
    System.out.println("\t\t Average arrays ");
    Scanner input = new Scanner(System.in);

    //array count varriable
    int n = 5;

    //array declaration
    int [] list1 = new int[n];
    double [] list2 = new double[n];

    System.out.print("Enter 5 integer values. ");
    for(int i = 0; i < n; i++) {
            list1[i]= input.nextInt();


         if(i == (n - 1)){

            System.out.println("\t The average of the 5 integers is "
            + average(list1, n));

            }
        }

    System.out.println("Enter 5 double values. ");
    for (int i = 0; i < n; i++){
         list2[i]= input.nextDouble();

         if(i == (n-1)){
             System.out.println("\t The average of the 5 doubles is "
             + average(list2, n));
         }

        }
    }
    public static int average(int[] array, int n){
        int sum = 0;
        for(int i = 0; i < n; i++){
            int holdNumber = array[i];
            sum += holdNumber;
            }

        int average = sum / n;
        return average;

    }

    public static double average(double[] array, int n){
        double sum = 0;
        for(int i = 0; i < n ; i++){
            double holdNumber = array[i];
            sum += holdNumber;
            }
        double average = sum / n;
        return average;
        }
}
DarkSuniuM
  • 2,523
  • 2
  • 26
  • 43

5 Answers5

0

I am a beginner, so my answer might be wrong:), sorry for that. This code is working for me. Like @iajrz mentioned, you can do spaece or newline when try to use system.in.

ccc
  • 55
  • 7
  • Not exactly; the input scanner reads from `System.in`'s buffer. The scanner uses whitespace as a default separator of data, so a new line or space or tab works - and when you reuse the scanner it reads the next available piece of data from System.in - your input is not discarded. You can even do 12345 – iajrz Oct 21 '18 at 23:08
  • @ iajrz Oh I understand. You are right ! I don't know that space and separate input of data. Thank you. – ccc Oct 21 '18 at 23:13
0

I think you're confusing two different concepts.

One is the input, and another one is your variable.

Input is a buffer (read: block of data) managed by the shell and the Scanner. It can contain an arbitrary amount of data, you have nothing to do with it.

What happens in your code is that the scanner takes the buffer and parses (read: interprets) the next valid value from the buffer and transforms it into the right data type - until the "nth" element. So, because you're taking "n" elements (controlled by the for), it doesn't matter how much data is available in the input buffer, you always read a finite amount.

The only way the amount of data matters is when there's no more input for the scanner to read from, in which case it asks for more input.

iajrz
  • 749
  • 7
  • 16
  • Ah I understand, it doesn't matter how many spaces I put in there; it still gives you the right answer. the buffer only reads the first 5 in the input. Thank you. – Silent Serenity Oct 21 '18 at 23:21
0

The reason is that you are iterating till the n number that you defined in the beginning.

for(int i = 0; i < n; i++) {
        list1[i]= input.nextInt();

So if you try to enter 1 1 1 1 1 124124 1241 you will see that the average is 1 because the rest is ignored and not added to the list. Because it doest not try nextInt() more than n given.

Bleach
  • 309
  • 3
  • 18
0

It doesn't give you an error because you only read the first 5 values, as stated in your for loop.

The first thing is you should decouple your input logic from your output logic, so you know for sure you're in your 5th number when you exit the for loop.

Then you can check if there's anything else than a blank string left, if there is then you can throw an exception stating it has too many numbers.

I've adapted the integer part, you can easily adapt the doubles logic.

Feel free to ask if you have any doubts.

The adapted code:

import java.util.Scanner;

public class Lab7A_BRC {

    public static void main(String[] args) {
        System.out.println("\t\t Average arrays ");
        Scanner input = new Scanner(System.in);

        //array count varriable
        int n = 5;

        //array declaration
        int[] list1 = new int[n];
        double[] list2 = new double[n];

        System.out.print("Enter 5 integer values. ");
        for (int i = 0; i < n; i++) {
            list1[i] = input.nextInt();
        }

        if (!input.nextLine().equals("")) {
            throw new RuntimeException("Too many numbers entered!");
        }

            System.out.println("\t The average of the 5 integers is "
                    + average(list1, n));


        System.out.println("Enter 5 double values. ");
        for (int i = 0; i < n; i++) {
            list2[i] = input.nextDouble();

            if (i == (n - 1)) {
                System.out.println("\t The average of the 5 doubles is "
                        + average(list2, n));
            }

        }
    }

    public static int average(int[] array, int n) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int holdNumber = array[i];
            sum += holdNumber;
        }

        int average = sum / n;
        return average;

    }

    public static double average(double[] array, int n) {
        double sum = 0;
        for (int i = 0; i < n; i++) {
            double holdNumber = array[i];
            sum += holdNumber;
        }
        double average = sum / n;
        return average;
    }
}
Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18
0

Because the for loop does n iterations so you pick up only the first n integers of the input. If your input is 1 2 3 4 5 6 7 8 it will select only 1 2 3 4 5 (because in your code n=5). You can also insert multiple digits number separated by spaces, so input 15 0 00 0010 0 has average=5

Wippo
  • 853
  • 8
  • 22