Hi- I want to use a sentinel value (-1) to end the loop of a program after the user has entered all of their data. However, how can I output inbetween each instance of the loop? Then, at the end, how can I will have an average of all entered data?

Thanks

Recommended Answers

All 11 Replies

are you talking about something like this?

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x;
int count = 0;
int i;

do
{
   System.out.println("enter a number");
   i = Integer.parseInt(br.readLine());
   x = x + i;
   count ++;
 }
  while(i != -1);
  int b = x / count;
  System.out.println("Average is: " + b);

Don't exactly take my word for it, I'm doing this off the top of my head.

Sorry, I'm new to java. Let me make it a little more clear.

How can I have a user input 2 numbers. The first number entered will be divided by the first to find the average for those two numbers.

After the user enters a -1 it will end and display the Complete average for all the numbers. ie (average1 + average2 + average3 / 3) <--depending on how many numbers they enter.

Hope this is more clear.

So, you've laid out your entire algorithm.

Now all YOU have to do is translate that to Java. Noone here is going to do your homework for you, but if you get stuck after making a good start we'll be happy to give you a hand in completing it.

All you would have to do is change the code I gave you little bit...

Well I spent a few hours on it and got it to compile and ask for the correct inputs. However, the program asks for miles driven and gallons used and it should display the miles per gallon. But it keeps displaying 0 as the result, unless the numbered entered is the same for both, ie 5miles, 5gallons, the result displayed is 1. Here is a part of the code where I think the problem is. If you could point me in the right direction I'd appreciate it. I don't expect anyone to do my homework for me.

//loop
	while (tankCounter <= numOftanks)
	{

	// first user input
	milesdrivenstring = JOptionPane.showInputDialog( "Enter the amount of miles driven" );

	milesdriven = Integer.parseInt( milesdrivenstring );

	// second user input
	gallonsusedstring = JOptionPane.showInputDialog( "Enter the gallons of gas used" );
	
	gallonsused = Integer.parseInt( gallonsusedstring );
	
	individualmpg = gallonsused / milesdriven;     

	// display output

	JOptionPane.showMessageDialog( null, "The miles per gallon for this tank is " + individualmpg,
	"Results", JOptionPane.PLAIN_MESSAGE );


	gallonstotal = gallonstotal + gallonsused; 
	milestotal = milestotal + milesdriven; // add milesto total
	tankCounter = tankCounter + 1; //increment counter

	
	}

So, you've laid out your entire algorithm.

Now all YOU have to do is translate that to Java. Noone here is going to do your homework for you, but if you get stuck after making a good start we'll be happy to give you a hand in completing it.

I haven't looked too much at the code, but I did notice that you used integers..This means there will be no decimal places, and it's possible that your results will turn out to be 0 because of that.

I haven't looked too much at the code, but I did notice that you used integers..This means there will be no decimal places, and it's possible that your results will turn out to be 0 because of that.

Thanks. I found out what the problem was. I had the division backwards. I corrected it by changing it to:

individualmpg = milesdriven / gallonsused;

Very simple mistake, that cause a big problem. I just figured out how to format the output to two decimal places so everything is working perfect!

Thanks for the help.

ok in good faith, i've done everything i can think of but can't figure this out. my objective is to use an infinite loop terminated by a sentinel value. then collect a minimum, maximum, and an average, of the values entered. my test batch of 12, 55, 62, 98 produces a min of 12, max of 98 and average of 65, when the calculated average is 56.75, i have to use int variables as part of the assignment, so collecting an answer of 56 OR 57 is fine. if my test batch is 12, 55, 62, 98, 56, my min is 12, max is 98 and average is 56!? i've repeated this with 12 and 24 in batches of 12. 24, result is 20. 12, 24, 18 gives me 18. as a last run, i ran 12, 55, 62, 98, 56, 57, gives me 56. these results are all over the board, any help?

code as follows

    import java.util.Scanner;

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


            int x = 0;
            int count = 0;
            int i;
            int min = 999999;
            int max = 0;
            int value = 0;

        do
        {

            System.out.println( "Enter value" );
            i = input.nextInt();


            if ( i != -888 )
            {
                value = i;
            }

            if ( value < min )
            {
                min = value;
            }

            if ( value > max )
            {
                max = value;
            }

            x = x + value;          

            count ++;
        }

            while( i != -888 );

            int b = x / count;
            System.out.println( "The average input is: " + b );
            System.out.println( "The max is: " + max );
            System.out.println( "The min is: " + min );



    }
}

its really great posting. its so useful for me. thanks for this.

commented: Spamming thanks will not get you visitors to your website... You helping others is what may tempt people to go and check out them. -2

its really great posting. its so useful for me. thanks for this.

umm i hope you noticed, it does not work, if you know how to fix it that would be really helpful. I know mr mugler would be happy if it worked right.

Sorry, I'm new to java. Let me make it a little more clear.

How can I have a user input 2 numbers. The first number entered will be divided by the first to find the average for those two numbers.

After the user enters a -1 it will end and display the Complete average for all the numbers. ie (average1 + average2 + average3 / 3) <--depending on how many numbers they enter.

Hope this is more clear.

Hey are you in my class? Haha every time I look something up on the internet I see people asking questions to the same homework problems I have :) I'm in libe50a with Henry Leitner... and I want to know how to use sentinels too! I know how to do the while loop and say while whatever != -1 but you and me know that that's not what we're looking for...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.