Dr Java does not recognize Math class
Page 1 of 114 Replies  1123 Views  Last Post: 09 March 2013  06:38 PM
#1
Dr Java does not recognize Math class
Posted 08 March 2013  12:46 PM
Replies To: Dr Java does not recognize Math class
#2
Re: Dr Java does not recognize Math class
Posted 08 March 2013  12:49 PM
Have you installed the JDK?
#3
Re: Dr Java does not recognize Math class
Posted 08 March 2013  12:52 PM
#4
Re: Dr Java does not recognize Math class
Posted 08 March 2013  12:56 PM
May be Dr Java install a JDK during it's installation process
Now when you say that it does not recognize it... what is there error message ? At which line ?
#5
Re: Dr Java does not recognize Math class
Posted 08 March 2013  01:07 PM
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum = 0; int average = 0; int maximum = 0; for(int counter = 0; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; average = (sum/5); maximum = Math.max(num); } JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); System.exit(0); } }
Line 23. error message reads: 1 error found:
File: /Users/jack/Java Programs/numberFacts.java [line: 23]
Error: /Users/jack/Java Programs/numberFacts.java:23: cannot find symbol
symbol : method max(int)
location: class Math
holy cow, I'm really botching this.... let's try one more time. Sorry about this.
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum = 0; int average = 0; int maximum = 0; for(int counter = 0; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; average = (sum/5); maximum = Math.max(num); } JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); System.exit(0); } }
line 23 is the error.
This post has been edited by pbl: 08 March 2013  01:05 PM
Reason for edit:: Fixed code tags
#6
Re: Dr Java does not recognize Math class
Posted 08 March 2013  01:09 PM
The max() method takes two (2) arguments and returns the largest one
The compiler (not Dr. Java) just does not find the max() method with one parameter located in the Math class
#7
Re: Dr Java does not recognize Math class
Posted 08 March 2013  01:11 PM
#8
Re: Dr Java does not recognize Math class
Posted 08 March 2013  01:22 PM
You need it fater the loop
int max = Integer.MIN_VALUE; for(.... { max = Math.max(max, num); } //max contains max of all the num average = .... should simply be calculated here
This post has been edited by pbl: 08 March 2013  01:23 PM
#9
Re: Dr Java does not recognize Math class
Posted 08 March 2013  01:36 PM
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum = 0; int average = 0; int max = Integer.MIN_VALUE; for(int counter = 0; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; max = Math.max(max, num); } average = (sum/5); JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); JOptionPane.showMessageDialog(null, "The maximum of the numbers is " + max); System.exit(0); } }
I am still getting the "cannot find symbol" error message for line 24, I am confused
Also, is there a way to write this code without using the MIN_VALUE? We have not covered this in class yet. Although I'm sure it would not lose me points, I just don't know its application or understand it, and I'd like to be able to complete the assignment without using parts of code I do not understand.
#10
Re: Dr Java does not recognize Math class
Posted 08 March 2013  02:31 PM
JackB121, on 08 March 2013  08:36 PM, said:
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum = 0; int average = 0; int max = Integer.MIN_VALUE; for(int counter = 0; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; max = Math.max(max, num); } average = (sum/5); JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); JOptionPane.showMessageDialog(null, "The maximum of the numbers is " + max); System.exit(0); } }
I am still getting the "cannot find symbol" error message for line 24, I am confused
Also, is there a way to write this code without using the MIN_VALUE? We have not covered this in class yet. Although I'm sure it would not lose me points, I just don't know its application or understand it, and I'd like to be able to complete the assignment without using parts of code I do not understand.
Uhm, Integer.MIN_VALUE is the field that represents 2^32, and I'm assuming that is not your intention. I think the original poster meant for you to change that, not just copy and paste the code...
#11
Re: Dr Java does not recognize Math class
Posted 08 March 2013  02:47 PM
#12
Re: Dr Java does not recognize Math class
Posted 08 March 2013  02:53 PM
JackB121, on 08 March 2013  09:47 PM, said:
That's not what Math.max() does. http://docs.oracle.c.../lang/Math.html look at that to find what it actually does. If you really want to do that, every time they enter a value, if that value is greater then some int variable, set the int variable to that value.
#13
Re: Dr Java does not recognize Math class
Posted 08 March 2013  04:48 PM
JackB121, on 08 March 2013  04:47 PM, said:
I don't see anything wrong with your code in post #9. If you're still seeing an error message, post the exact message and stack trace, copied and pasted.
As for your other question, "Also, is there a way to write this code without using the MIN_VALUE?" The only time the MIN_VALUE is needed is for the first comparison to ensure that max' initial value is less than any possible value input by the user. Do you know, can you use an if/else statement? If so, an option to using MIN_VALUE would be to set max = num when counter == 0, then use max = Math.max( num, max ) thereafter.
Keep coding!
#14
Re: Dr Java does not recognize Math class
Posted 08 March 2013  05:03 PM
#15
Re: Dr Java does not recognize Math class
Posted 09 March 2013  06:38 PM
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum; int average; int max; input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum = num; max =num ; // init max with first number // not we start with counter = 1 instead of 0 for(int counter = 1; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; max = Math.max(max, num); } average = (sum/5); JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); JOptionPane.showMessageDialog(null, "The maximum of the numbers is " + max); System.exit(0); } }
natecat, on 08 March 2013  04:31 PM, said:
To find the max() of 5 number within a loop you need to int max to something
Or you could the horribe
//This program is to calculate the sum, average, and maximum of 5 numbers. import javax.swing.JOptionPane; public class numberFacts { public static void main (String[] args) { String input; int numbers = 5; int sum = 0; int average = 0; int max = Integer.MIN_VALUE; for(int counter = 0; counter<numbers; counter++) { input = JOptionPane.showInputDialog("Please enter an integer number."); int num = Integer.parseInt(input); sum += num; if(counter == 0) max = num; else max = Math.max(max, num); } average = (sum/5); JOptionPane.showMessageDialog(null, "The sum of the numbers is " + sum); JOptionPane.showMessageDialog(null, "The average of the numbers is " + average); JOptionPane.showMessageDialog(null, "The maximum of the numbers is " + max); System.exit(0); } }
