Results 1 to 13 of 13
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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.

    Please help.

    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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #4
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #6
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #8
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Average, Max, Min & Range

    Quote Originally Posted by jmscarlet9 View Post
    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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. #11
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Average, Max, Min & Range

    Quote Originally Posted by jmscarlet9 View Post
    ...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. #13
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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

Similar Threads

  1. a range of numbers
    By sh4rif in forum New To Java
    Replies: 2
    Last Post: 12-07-2011, 04:13 PM
  2. name in a range of names
    By Evii0 in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 06:35 AM
  3. pls help in understanding the range of byte
    By shivamskn in forum New To Java
    Replies: 2
    Last Post: 06-24-2010, 12:07 PM
  4. In-Range adder
    By filem_funk in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 01:06 AM
  5. Range within an Array
    By End in forum New To Java
    Replies: 6
    Last Post: 04-18-2009, 06:53 PM

Posting Permissions

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