Min and Max math methods

Trouble using the min and max methods

Page 1 of 1

11 Replies - 10838 Views - Last Post: 17 June 2009 - 02:58 PM Rate Topic: -----

#1 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Min and Max math methods

Post icon  Posted 16 June 2009 - 05:38 PM

Hello all,
I haven't posted since I finished my C++ class. Now on to Java. Seems a bit easier...
Well here is my problem. I am trying to use the max and min method to determine the max and min number enterd by the user into an array. I could type out all the code to make a class, but want to keep it simple. The errors I get are:

FindAverage.java:31: cannot find symbol
symbol : method max(int)
location: class java.lang.Math
int maxNums = Math.max(0, Math.max( array[counter]));
^
FindAverage.java:32: cannot find symbol
symbol : method min(int)
location: class java.lang.Math
int minNums = Math.min(0, Math.min( array[counter]));
^
2 errors

Here is the code: any help is appreciated.
   import javax.swing.JOptionPane;
	public class FindAverage
	/* Main method */
   {
	   public static void main(String args[])
	  {
		 String strPrompt = "Enter integer values, enter 0 to mark end of file:";
		 int[] array = new int[10];//Amount entered from keyboard
		 int nums = 0;
		 int sum = 0;
		 int counter = 0;
		 float avg = 0;	
	  //Receive amount entered from keyboard
		 do
		 {
			String strData = JOptionPane.showInputDialog(strPrompt);
			nums = Integer.parseInt(strData);
			array[counter] = nums;
			sum += nums;
			counter++;
		 }
		 while (nums != 0);
		 counter -= 1;
		 avg = sum / counter;
		 int maxNums = Math.max(0, Math.max( array[counter]));
		 int minNums = Math.min(0, Math.min( array[counter]));
		 JOptionPane.showMessageDialog(null,
			"You entered " + counter + " numbers:\n" +
			" The sum is: " + sum + 
			"\n The avg is: " + avg + 
			"\n The largest value is: " + maxNums +
			"\n The smallest value is: " + minNums );
	  }
   }


Is This A Good Question/Topic? 0
  • +

Replies To: Min and Max math methods

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Min and Max math methods

Posted 16 June 2009 - 05:45 PM

Edited to reduce confusion.

This post has been edited by xclite: 17 June 2009 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Min and Max math methods

Posted 16 June 2009 - 05:51 PM

Hello there. :)
First of all,

int maxNums = Math.max(0, Math.max( array[counter]));


In the nested call of the function math u use a version of max that doesnt exist.
You call the function with one parameter.

Secondly, isnt the logical thing to do a loop to find the max value and the min value? :)
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Min and Max math methods

Posted 16 June 2009 - 05:52 PM

View Postxclite, on 16 Jun, 2009 - 04:45 PM, said:

import java.lang.Math;


At the top of your program.

No the method is fully qualified: Math.max()
so the problem is not there :)

There are methods to return the max between 2 numbers:
Math.max(int, int);
Math.max(float, float);
Math.max(double, double);
....
but there is no method Max() with a single element so it is

Math.max(0, array[counter]);
not
Math.max(0, Math.max(array[counter]));
Was This Post Helpful? 0
  • +
  • -

#5 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Min and Max math methods

Posted 16 June 2009 - 05:55 PM

View Postxclite, on 16 Jun, 2009 - 04:45 PM, said:

import java.lang.Math;


At the top of your program.


All classes in the java.lang package are visible without an import.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Min and Max math methods

Posted 16 June 2009 - 06:05 PM

Ug you are, of course, correct. That's what I get for working in other languages all week.
Was This Post Helpful? 0
  • +
  • -

#7 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Min and Max math methods

Posted 16 June 2009 - 06:56 PM

Thanks that did solve my compile errors. I don't get the max or min to come out right though. I get a "0" for both. Do you think that subtracting the last entry from the counter is doing that?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Min and Max math methods

Posted 16 June 2009 - 07:00 PM

This will mnake you exit after the first input

while (nums != 0);

you probably mean

while(counter < array.length);
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Min and Max math methods

Posted 16 June 2009 - 07:02 PM

Your max will be the last value you check, and min will always be 0. You dont compare the two as an "updated" variable. Something like what follows may be better:

int maxNums = 0;
int minNums = array[0]; // after the while loop
//following can replace what you have
maxNums = Math.max(maxNums, array[counter]);
minNums = Math.min(minNums, array[counter]);


This post has been edited by Fuzzyness: 16 June 2009 - 07:04 PM

Was This Post Helpful? 1
  • +
  • -

#10 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Min and Max math methods

Posted 16 June 2009 - 07:10 PM

That does change it, now I get 2 for both the max and the min. ??
I have the while statement because I am forced to use "0" as a setinal to stop the loop.
Was This Post Helpful? 0
  • +
  • -

#11 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Min and Max math methods

Posted 16 June 2009 - 08:00 PM

Here is some code snippets to help...maybe (hope) you can put it together...

Entering values into an array:
Scanner keyboard = new Scanner(System.in)

System.out.println("How may values to input into the array/ ");
int values = keyboard.nextInt();

int[] tests = new int[values];

for (int i = 0; i < tests.length; i++)   //could use a while loop here....
   {

	  System.out.print("Enter test score " + (index + 1) );
	 tests[i] = keyboard.nextInt();

}


To find min and max array values:

int highest = tests[0];
for (int j = 1; index < tests.length; j++)
{   if (tests[j] > highest)
	  highest = tests[j];
}


The lowest is similar in scope..now go do it... :D
Was This Post Helpful? 1
  • +
  • -

#12 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Min and Max math methods

Posted 17 June 2009 - 02:58 PM

Works great thanks allot for the help! This lang is so much easier than C++ was...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1