0

Hey guys I was working on making a number averaging program and I wanted to insert this function that allows the user to enter the letter "y" to run again and do another computation, however, my program shows the terminated message (I'm using Eclipse) after the first computation, even though I want the user to be able to enter the input. Here is the part of the source code that puzzles me:

public static void main(String[] args)
{
    Scanner input= new Scanner(System.in);

    System.out.print("This is an averaging program. How many numbers would you like to average together?");
    int total=input.nextInt();
    int i;
    float sum=0;
    for(i=0; i<total; i++)
    {
        System.out.print("Enter your numbers: ");
        sum += input.nextFloat();
    }
    String y;

    System.out.print("Your average is: " + sum/total + "\n");
    System.out.print("Would you like to do another computation? Type y for yes, or something else for no.");
    y=input.nextLine();
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
Kevin Ossia
  • 179
  • 1
  • 3
  • 9
  • Well, if that's the end of the program, then there's nothing telling it to start over after you answer the prompt. – Ryan Stewart Feb 14 '13 at 02:48
  • It isn't the end of the program, it's part of it. I have it written so that main() says that if the user inputs "y", another function which is identical starts to run, and if the user wants to enter ANOTHER computation, then that function calls itself. – Kevin Ossia Feb 14 '13 at 02:54
  • Ah, so two things: 1) That kind of duplication is terrible. Don't do it. 2) [user000001's answer](http://stackoverflow.com/a/14866999/839646) is probably what you're looking for. – Ryan Stewart Feb 14 '13 at 02:56
  • Just out of curiosity, why is that duplication terrible? – Kevin Ossia Feb 14 '13 at 02:59
  • My answer of course is relevant if OP presses `enter` after entering each number. – user000001 Feb 14 '13 at 02:59
  • Also, I keep getting an error saying that readLine() isn't part of the Scanner class. – Kevin Ossia Feb 14 '13 at 03:00
  • yes it is `nextLine()`. I fixed the typo. – user000001 Feb 14 '13 at 03:01

1 Answers1

3

try this:

change

sum += input.nextFloat();

to

sum += input.nextFloat();
input.nextLine();

And

int total=input.nextInt();

to

int total=input.nextInt();
input.nextLine();

Explanation. You should manually read the newline character \n after reading a number form Scanner

Of course you should also add the relevant part of the program in a do while loop in order to execute repeatedly, but you probably know that.

user000001
  • 32,226
  • 12
  • 81
  • 108
  • How does this help run the program again if the user answers "y"? – Ryan Stewart Feb 14 '13 at 02:49
  • It doesn't but it does solve the problem of the program prematurely ending instead of waiting for an input. – Sinkingpoint Feb 14 '13 at 02:51
  • The do-while loop....why didn't I think of that! My dad always kept saying use a loop not recursive function but I could never figure it out, I might be able to now. Thanks! – Kevin Ossia Feb 14 '13 at 02:58