Question:
How do you create an indetermined number of inputs in java?
Mary Jean
2009-08-31 16:54:35 UTC
I need to create a program that takes in an indetermined number of inputs (this is decided by the user) using java. I need to search these inputs and find and work with the largest one. Any help or advice is greatly appreciated. I understand c++ pretty well but im new to java!
Four answers:
Robin
2009-09-02 08:14:08 UTC
This is a script I found elsewhere that I was hoping to tweak to do something just a little different. Can't figure out its error. I wouln't mind paying someone to help out if needed to get it to run as I'd like as well.



Thank you.



This is a test page on my site of the same script below:

http://www.personalizedletter.com/Letters/printtest.html











































Instant Christmas Story Generator


Elves at the North Pole


 









To create your instant Christmas letter,

simply fill in the boxes below and then click the Create button. You will then

be able to copy and print the story on your own letterhead. For best results,

you can put more than one sentence in each box. See the sample below for ideas.


Mark aka jack573
2009-09-04 11:47:27 UTC
You could use a Vector, this would probably be the best way to do it.



Other solutions would be using a List, but a Vector would probably be better.



The API for Vector is here:

http://java.sun.com/javase/6/docs/api/java/util/Vector.html



Here is a summary of the Vector from the API

"The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created."



The only disadvantage of using the Vector is you need to use objects and not the primitive classes. But, Java gives you wrapper classes for the primitives, so you can put a primitive into a wrapper class, and get the primitive back. The wrapper classes are objects, so you can put it into a Vector.



So lets say the inputs are ints. You would do something lilke this.



Vector vec = new Vector(); // The Integer class is the wrapper class for an int.

int num;

Integer objNum;

// you would need a way for the user to indicate they have stopped inputting the number. Let's assume that the user types -999 to stop the input.

while (true)

{

// Output a message to ask the user to provide a number.

num = // Get the input here.

if (num == -999)

{

break();

}

objNum = new Integer(num);

vec.add( objNum );

}



Now to go through all of the numbers you have got from input, you would do something like this.



if ( ! vec.isEmpty() )

{

int largest = vec.firstElement();

int smallest = largest;

for (int i = 1; i < vec.size(); i++)

{

objNum = vec.elementAt( i );

num = objNum.intValue();

if (num > largest)

{

largest = num;

}

if (num < smallest)

{

smallest = num;

}

}

// Output the largest and smallest.

}

else

{

// Output no input was received.

}





Hope that helps you out.
tuaamin13
2009-08-31 17:00:15 UTC
It's the ellipsis.



I forget the usage, but you can probably find some examples of variable input functions online.
Steve
2009-08-31 17:00:24 UTC
Since Java 5, it has supported variable length arguments:



void printall(String... args) {

System.out.println("There are "+args.length+" args");

for (String arg : args) {

System.out.println(arg);

}

}



Hope that helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...