Win a copy of Design for the Mind this week in the Design forum!

sam myer
Greenhorn
Posts: 4
Here is what I'm trying to do:

lo : Smallest. Write a program that allows a user to enter a series of positive integers with a -99 to signal the end of the series. The program should then display the smallest of the numbers in the series. Do *not* use a bogus value for the smallest to begin the program. Use the first value in the data list (if any) to initialize your 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:

10, 3, 8, 2, -99 (lo = 2)
-99 (ouput should be: "no numbers, no lo")
4, 6, 1, 106, 22, -99 (lo = 1)

So far, I've established that...

loop, probably do/while
get input from user, input
if input != "-99",
set list (array) = input

if input == "-99"
set list = input (makes it easy to terminate your loop through the array and to tell if it is otherwise emtpy)
sort through array, comparing two elements of the array, keep lowest and compare to next available, till -99 found
print as necessary

- This is what my friend has told me so far, but I'm not sure what he means by 'set list (array) = unput.

Kyle Morgan
Greenhorn
Posts: 14
That depends. Are you using an array, or some sort of List (like an ArrayList). If you are using a List, just add the number the user entered to the end of the List.

sam myer
Greenhorn
Posts: 4
well ive been informed not to use an array, since it's not a finite number of inputs.

Kyle Morgan
Greenhorn
Posts: 14
Then look into the ArrayList class and its add() method.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
Unless you need to produce the list of inputs at the end, you don't need to use an array or List at all. Just use a single int variable "min". On the first input, set "min" to be the number you read. For each additional input, if it's not -99, then set min to the new number if and only if the number is less than min. If it is -99, then output min -- unless -99 is the first number, in which case you'd want to print that error message.

sam myer
Greenhorn
Posts: 4
ernest do you have AIM? I understand exactly what you are saying and that IS what I need to do.. but I'm having trouble setting up the loop statement. Do I use while or do-while? and do I put the prompt for input (JOPtionPane...) in the loop body? I just learned about these loops today, so it's very new to me.

Kyle Morgan
Greenhorn
Posts: 14
Please don't post in multiple forums, it wastes peoples' time.

sam myer
Greenhorn
Posts: 4
Here's what I've got so far:

import javax.swing.JOptionPane;

public class IO {

public static void main(String[]args){

String dataString =
JOptionPane.showInputDialog(null,
"Enter an int value",
"IO",
JOptionPane.QUESTION_MESSAGE);

int data = Integer.parseInt(dataString);

int min = data;

do {

dataString = JOptionPane.showInputDialog(null,
"Enter an int value: \n (the program exits if the input is -99)",
"IO",
JOptionPane.QUESTION_MESSAGE);

data = Integer.parseInt(dataString);

if (data != -99 && data > min){

min = data

while (number != -99) {

JOptionPane.showMessageDialog(null,
"The min value is " + data,
"IO",
JOptionPane.INFORMATION_MESSAGE);

}
}
}

Henry Wong
author
Marshal
Posts: 20996
76
Here's what I've got so far:

Does it compile? At a quick glance, it doesn't look like this code will compile.

Henry

Kyle Morgan
Greenhorn
Posts: 14
The correct syntax for a do-while loop is: