Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help needed to find the smallest and largest number

807588Jul 16 2009 — edited Jul 16 2009
I 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2009
Added on Jul 16 2009
14 comments
496 views