• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need some java help....

bubbadu

Diamond Member
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
 
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;
}
}
 
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?
 
The integers are taken in until -99 is pressed.. but if -99 is pressed first, the program will give an error.
 
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.
 
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;
}

}
 
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.
 
Back
Top