Can someone please help with this simple Java practice problem

RecklaZ

Senior member
Mar 13, 2001
305
0
0
This is what the program wants us to do


Write a public class SecondLargest that contains only the method main.
The method prints the title

SECOND LARGEST NUMBER
=====================

and then opens a JOption pane that prompts the user to enter a line of integers.
Then, main reads the numbers using a tokenizer. It computes and prints out the
second largest number in the line.

If the input line is empty, it prints
The second largest is undefined because the line is empty!

If the input line has only one number, it prints the message,
The second largest is undefined because the line has only one number!

If the input line has 2 or more numbers, like the one below, it prints
the largest and the second largest number. You must take into account that
the numbers may have several occurrences in the line.
For the the line
9 -5 9 -8
main prints the message:
The largest number is 9.
The second largest is 9.

Do not assume that the largest or the second largest are positive.

All you have to do is to give me a printed copy of the file and the source
code on a diskette. Don't forget to put them in
an envelope (folder) with your name, course, and section written on it.
Also, obey the style guidelines.

----------------------------------------------------------------------
//This is what i have so far.................

import javax.swing.JOptionPane; //for input

import java.util.StringTokenizer; //breaks up the line


public class SecondLargest
{

public static void main(String[]args)
{

//prints the title and underlines it
System.out.println("SECOND LARGEST NUMBER");
System.out.println("=====================\n\n\n");

int count = 0;
//gets the input put into the input dialog
String input = JOptionPane.showInputDialog("Enter a line of integers");

StringTokenizer tokenizer = new StringTokenizer(input);

while(tokenizer.hasMoreTokens()) //processes the next token
{
String token = tokenizer.nextToken();
int current = Integer.parseInt(token);
count ++;
System.out.println("data("+ count+") "+current);
}
if(count == 0)
System.out.println("The second largest is undefined because the line is empty");
else if(count == 1)
System.out.println("The second largest is undefined because the line has only one number");



}

}

----------

I'm stuck....... Oh and btw this isn't homework or anything, this is a practice problem and my teacher said we can get help from anywhere. I will appreciate it if someone pm me the code or posted it here so i can work backwards on it. Thanks!!
 

RecklaZ

Senior member
Mar 13, 2001
305
0
0
can u then please tell me any forum i can get help from with problems like this?
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
Add a sorting system to it next. If you haven't learned anything fancier, try a simple one like cycling through the numbers, comparing them and picking the highest then removing it. Then do that again and you have completed it. I would suggest putting the int's into an array or some other structure to do this a little easier though.