0

I have a programming assignment that's asking me to have the user input 10 (or less) integers and put them in an array, then take the average of them and output it. If they input a period, the program should stop asking for integers and do the averaging.

My problem is that whenever the user inputs an integer, the for loop executes more than once.

My code is below. Any ideas on how to fix this?

    int[] intArr = new int[10];

    int entered;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    for(entered = 0; entered < 10; entered++){
        System.out.println("Please enter an integer: ");

        int input = br.read();

        if(input == '.') break;

        else{
            intArr[entered] = input;
        }
    }

    int total = 0;
    for(int i : intArr){
        total += i;
    }

    System.out.println("Average: " + total/entered);
    System.out.println("Entered: " + entered);
devnull
  • 118,548
  • 33
  • 236
  • 227
Daniel
  • 84
  • 2
  • 8

3 Answers3

0

Use String input = br.readLine() to read an entire line.

To check for ".", use if (input.equals(".")) { ... }. (check out this if you want to know why you have to use .equals() instead of == for Strings)

Finally, to convert the input to an integer, see here.

Community
  • 1
  • 1
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
  • Yup, I know about the equals(); Andrey helped me solve it above. I'll mark this as my answer, anyways. Thanks! – Daniel Nov 12 '13 at 19:01
0
for (entered = 0; entered < 10; entered++) {
    System.out.println("Please enter an integer: ");
    String str = br.readLine();
    if (".".equals(str)) {
        break;
    }
    int input = Integer.valueOf(str);
    intArr[entered] = input;
}
Stanley
  • 11
  • 1
0

Ok Its Really Simple First let Me Explain You Why Its Happening Ok the read() function reads first char of the input value and rest of line is stored in buffer so when you enter any integer for example: 1 1 is stored in variable and '\n'which java by defaults adds to a input value gets stored in buffer so in next iteration of loop it reads the char '\n' from buffer as input value and moves to next iteration

EXAMPLE 2: If In Your Program We Enter Input As 12 It Skips Two Iterations Coz Firstly It Stores 1 At The Time Of Input In Next Iteration It Takes value 2 of previous input as input for this time In Further Next Iteration It Takes '\n' and then moves to next iteration at which their is no character left in memory so it asks you to input

Note::: read() functions return character so even if user enters 5 while calculation ASCII Code Of 10 will be used that is 53 not one creating problems

FIX:::

int[] intArr = new int[10];
int entered;
BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
for (entered = 0; entered < 10; entered++) {
    System.out.println("Please enter an integer: ");

    String input = br.readLine();

    if (input.equals(".")) {
        break;
    } else {
        intArr[entered] = Integer.parseInt(input);
    }
}
int total = 0;
for (int i: intArr) {
    total += i;
}
System.out.println("Average: " + total / entered);
System.out.println("Entered: " + entered);