Help needed to find the smallest and largest number
807588Jul 16 2009 — edited Jul 16 2009I have an assignment where I have to find the smallest number, the largest number, the sum of the numbers and the sum of the squares from a line of integers.
I'm stuck. I cna't figure it out. I know the mistakes I have, but I don't know how to fix them
This is basically what I have (I won't paste the whole program, only the parts where I think I made the mistakes)
// declare the variables
String line = null; // the line of integers
int count = 0; // the number of tokens
int sum = 0; // a running sum
int sum2 = 0; // sum of squares of numbers
int min = 0; // the smallest number
int max = 0; // the largest number
// get the input
line = JOptionPane.showInputDialog("Enter a line of "
+ " integers.");
StringTokenizer tokens = new StringTokenizer(line);
count = tokens.countTokens();
// process the line
for (int i = 0; i < count; i++)
{
// process the next token
String token = tokens.nextToken();
int current = Integer.parseInt(token);
sum += current; // add number to sum of numbers
sum2 += current*current // add to sum of squares
// calculate smallest and largest numbers
if (current <= min )
min = current;
if (current >= max )
max = current;
}
When I run the program, min is always 0. How can I fix it?
Thanks in advance.