File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Please help a newby out! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help a newby out!" Watch "Please help a newby out!" New topic
Author

Please help a newby out!

sam myer
Greenhorn

Joined: Feb 21, 2007
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

Joined: Oct 12, 2006
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

Joined: Feb 21, 2007
Posts: 4
well ive been informed not to use an array, since it's not a finite number of inputs.
Kyle Morgan
Greenhorn

Joined: Oct 12, 2006
Posts: 14
Then look into the ArrayList class and its add() method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
sam myer
Greenhorn

Joined: Feb 21, 2007
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

Joined: Oct 12, 2006
Posts: 14
Please don't post in multiple forums, it wastes peoples' time.

http://forum.java.sun.com/thread.jspa?messageID=9513045
sam myer
Greenhorn

Joined: Feb 21, 2007
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
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kyle Morgan
Greenhorn

Joined: Oct 12, 2006
Posts: 14
The correct syntax for a do-while loop is:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help a newby out!