File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help a newby out!

 
sam myer
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well ive been informed not to use an array, since it's not a finite number of inputs.
 
Kyle Morgan
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then look into the ArrayList class and its add() method.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post in multiple forums, it wastes peoples' time.

http://forum.java.sun.com/thread.jspa?messageID=9513045
 
sam myer
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct syntax for a do-while loop is:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic