Need some java help....

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
This is really pissing me off.. its not that hard, but I just can't get it to work.. heres what has to happen.

Write a program that allows a user to enter a series of integers with a -99 to signal the end of the series. The program should then display the largest and the smallest of the numbers in the series. Do *not* use a bogus value for the largest and smallest to begin the program. Use the first value in the data list (if any) to initialize your largest and smallest. This program must have only *one* programmer-defined method other than main which outputs user directions to the console about how the program works (and that -99 is to be used to end the input). Note that an empty list is possible and should not confuse your program! After displaying directions to the console, use GUI for the remaining input and output. Try out your program on the following lists:

I have been sitting here for hours, trying to get this to work. I have tried just about everything, but can't get this simple sorting complete.. if anyone could help that would be great. THANKS
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
List your code so we don't have to do it for you, we'll make suggestions
 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
My code is messed up, but here it is.

import javax.swing.JOptionPane;
public class hilo{

public static void main(String args[]) {
int n = getValue();
int s = getValue();
int smallvalue;

if (n == -99)
System.out.println("No data");
else
{
s = n;
n = getValue();
}
while (n!= 99)
{
if (n < s)
s = n;
n = getValue();
System.out.println(n);
}
}

public static int getValue(){

String valueStr = JOptionPane.showInputDialog(null, "Enter a series of integers, enter -99 to end input", "Speed Input",
JOptionPane.QUESTION_MESSAGE);
int value = Integer.parseInt(valueStr);
while (value!= -99){
valueStr = JOptionPane.showInputDialog(null, "Enter a series of integers, enter -99 to end input", "Integer Input",
JOptionPane.QUESTION_MESSAGE);
value = Integer.parseInt(valueStr);
}
return value;
}
}
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Also, how do u want to handle this? Does a Box pop up and you enter one integer per box? Or are a list of integers taken in, separated by say a comma?
 

Dowfen

Senior member
Jul 16, 2002
284
0
0
Originally posted by: BigJ
List your code so we don't have to do it for you, we'll make suggestions

Yes, list the code.

Edit: Oops, already listed.
 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
The integers are taken in until -99 is pressed.. but if -99 is pressed first, the program will give an error.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Do you realize what you're doing here?

int n = getValue();
int s = getValue();

You're calling the method twice, so the user would have to enter the exact same string of numbers in each time to get an accurate reading.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Here's some code that may help you out:

public class hilo{
public static int smallestNumber;
public static int largestNumber;

public static void main(String args[]) {
getValue();
System.out.println("the smallest number is " + smallestNumber);
}


public static void getValue(){

String valueStr = JOptionPane.showInputDialog(null, "Enter a series of integers, enter -99 to end input", "Speed Input",
JOptionPane.QUESTION_MESSAGE);
int value = Integer.parseInt(valueStr);

if(value!= -99) {
smallestNumber = value;
largestNumber = value;
}

while (value!= -99){

valueStr = JOptionPane.showInputDialog(null, "Enter a series of integers, enter -99 to end input", "Integer Input",
JOptionPane.QUESTION_MESSAGE);
value = Integer.parseInt(valueStr);
if((smallestNumber > value) && value != -99)
smallestNumber = value;
if(largestNumber < value)
largestNumber = value;
}

}
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
That handles some of the conditions you need. Now you need to handle what will happen if the list is empty, and also displaying the smallest and largest.