# Thread: Average, Max, Min & Range

1. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Average, Max, Min & Range

Hey everyone,
Completely new to java unless you count the 6 weeks of class I've already gotten through. I'm completely stumped on this program which (of course) is due tonight. Right now, it compiles fine but after I enter 2 sets of digits, even if I then enter "q", the run goes into an infinite loop.

I know my range is probably in the wrong place, but wanted to at least have it in the code while I'm trying to figure it out.

Java Code:
```import java.util.Scanner;

/**
Write a program that reads a set of floating-point values. Ask the user to enter the values, then print:
-the average of the values
-the smallest of the values
-the largest of the values
-the range, that is the difference between the smallest and largest.
Only prompt for the values once.
*/
public class AvgSmLgRg
{
public static void main (String [] args)
{
double sum = 0;
int count = 0;
double largest = 0;
double smallest = 0;
double range = 0;

Scanner in = new Scanner(System.in);
//Process values when sentinel is entered.
System.out.println("Enter values. Use Q to finish.");
while (in.hasNextDouble())
{
double value = in.nextDouble();
{
//Find average
sum = sum + value;
count++;
double average = sum/count;
if (value > largest);
{
largest = value;
}
if (value < smallest);
{
smallest = value;
range = largest - smallest;
}
System.out.println("The average is: " + average);
System.out.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
System.out.println("The range is: " + range);
}

}
}
}```

2. ## Re: Average, Max, Min & Range

Big warning. This:

Java Code:
```                if (value > largest);
{
largest = value;
}```
has a wayward semicolon at the end of the if boolean condition. The compiler will interpret this as if you mean to do this:

Java Code:
```            if (value > largest) {
; // semicolon becomes an empty statement
}

// do the following *always* regardless of whether value is > largest
// or not.
{
largest = value;
}```
Solution: get rid of that semicolon and re-write all of your if blocks in a similar fashion:

Java Code:
```            if (value > largest) {
largest = value;
}```

3. ## Re: Average, Max, Min & Range

Also, why the curly brace here?

Java Code:
```         double value = in.nextDouble();
{ // *** why this curly brace?```
And you'll want to get all of your println statements out of the while loop, unless they're in there for testing purposes only and will be moved for your final program.

4. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Average, Max, Min & Range

Fubarable,
When I move the printlns out of the loop, I get the following error:

----jGRASP exec: javac -g AvgSmLgRg.java

AvgSmLgRg.java:48: error: cannot find symbol
System.out.println("The average is: " + average);
^
symbol: variable average
location: class AvgSmLgRg
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Based off of both replies so far, I've updated the code like this:

Java Code:
```import java.util.Scanner;

/**
Write a program that reads a set of floating-point values. Ask the user to enter the values, then print:
-the average of the values
-the smallest of the values
-the largest of the values
-the range, that is the difference between the smallest and largest.
Only prompt for the values once.
*/

public class AvgSmLgRg
{
public static void main (String [] args)
{
double sum = 0;
int count = 0;
double largest = 0;
double smallest = 0;
double range = 0;

Scanner in = new Scanner(System.in);
//Process values when sentinel is entered.
System.out.println("Enter values. Use Q to finish.");
while (in.hasNextDouble())
{
double value = in.nextDouble();
//Find average
sum = sum + value;
count++;
double average = sum/count;
if (value > largest)
{
largest = value;
}
if (value < smallest)
{
smallest = value;
range = largest - smallest;
}
}
System.out.println("The average is: " + average);
System.out.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
System.out.println("The range is: " + range);
}
}```

5. ## Re: Average, Max, Min & Range

Well the error makes sense. You declare the average variable inside of the while loop, and so it is only visible inside of this block of code (the while loop). To allow it to be visible throughout the main method, you'll want to declare it in the main method, above the while loop. This is called a scoping problem since the scope of the average variable is currently limited to the while loop. After this loop, the variable goes "out of scope" and is no longer available.

6. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Average, Max, Min & Range

That makes sense...even though "scoping" is in the next chapter of our book. GRR.

Would I need to declare it with the rest of the declarations before the "Scanner in" line?

I guess what doesn't make sense to me is the example our book has shows

while (in.hasNextDouble());
{
value = in.nextDouble();
Process value.
}

This made me assume everything had to be after the value statement.

7. ## Re: Average, Max, Min & Range

Your instructions make sense because where you declare the variable is not necessarily the same as where you use the variable. So again declare it before the while loop, but use it inside the while loop, and after the while loop.

8. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Average, Max, Min & Range

Okay. I found another site that helped and I switched around the order of some things. However, the smallest isn't showing up when compiled and ran. Everything else seems to be working....so I don't get why the smallest isn't.

Here's my updated code:

Java Code:
```import java.util.Scanner;

/**
Write a program that reads a set of floating-point values. Ask the user to enter the values, then print:
-the average of the values
-the smallest of the values
-the largest of the values
-the range, that is the difference between the smallest and largest.
Only prompt for the values once.
*/

public class AvgSmLgRg2
{
public static void main (String [] args)
{
double smallest = 0;		//smallest number
double largest = 0;		//largest number

int count = 0;				//Total # of values
double sum = 0;			//Sum of all values

Scanner in = new Scanner(System.in);
//Process values when sentinel is entered.
System.out.println("Enter values. Use Q to finish.");
while (in.hasNextDouble())
{
double value = in.nextDouble();
//Find maximum
if (value > largest)
{
largest = value;
}
//Find minimum
if (value < smallest)
{
smallest = value;
}
//Find average
count++;				//Counts values
sum += value;		//Keeps running total
}
if (count > 0)
{
double average = sum / count;
//Find range
double range = largest - smallest;
System.out.println("The average is: " + average);
System.out.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
System.out.println("The range is: " + range);
}
else
{
System.out.println("You didn't input enough data");
}
}
}```
PS I'm SO glad I found this forum! Thank you!!!
Last edited by jmscarlet9; 02-29-2012 at 01:46 AM. Reason: Updated code again

9. ## Re: Average, Max, Min & Range

Originally Posted by jmscarlet9
Okay. I found another site that helped ...
Which site did you use?

PS I'm SO glad I found this forum! Thank you!!!
You're welcome.

10. ## Re: Average, Max, Min & Range

A trick when searching for large and/or small values is to set the starting value at the opposite limit.
For largest, set its initial value to the smallest possible number.
For smallest ,set initial value to the largest possible number.

11. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Average, Max, Min & Range

This just has an example of what I'm trying to do (minus the range part). - Java: Example: Console: Min, max, average

I think I figured out why the smallest isn't working. When I did a test input of negative numbers, the "largest" appeared not to work. However, I think it's because I set it to initialize at 0. When I tried what this site suggested, e.g.,

Java Code:
```double smallest = Double.MIN_VALUE;		//smallest number
double largest = Double.MAX_VALUE;		//largest number```
I got the following as my output.

----jGRASP exec: java AvgSmLgRg2

Enter values. Use Q to finish.
2 3 4 q
The average is: 3.0
The largest is: 1.7976931348623157E308
The smallest is: 4.9E-324
The range is: 1.7976931348623157E308

----jGRASP: operation complete.

Any help on this last piece would be GREATLY appreciated.

12. ## Re: Average, Max, Min & Range

Originally Posted by jmscarlet9
...When I tried what this site suggested, e.g.,

Java Code:
```double smallest = Double.MIN_VALUE;		//smallest number
double largest = Double.MAX_VALUE;		//largest number```
I got the following as my output.
You're starting out with largest being the largest double that java will allow and the converse form smallest. Will largest ever be hold a greater value? Will smallest ever hold a smaller value? Perhaps you want to do this slightly differently? You're close.

13. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Average, Max, Min & Range

I typed it wrong. YOU ARE THE BEST. Any chance you can have some plastic surgery to look like me for the final in May????? :-D

Until next week's homework (hopefully before the night it's due....)
~Jaime

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•