to print first and last number from a set of input numbers

• 03-28-2011, 03:40 AM
95673641
to print first and last number from a set of input numbers
i need help with a program
the output should be the first and last number from a set of number that you input. i am confused.
can anybody help me? i appreciated!
• 03-28-2011, 03:48 AM
Fubarable
Please tell us how you are confused and what specific questions you have. Show us your code, and ask a detailed question to get a helpful answer.
• 03-28-2011, 04:20 AM
Solarsonic
Quote:

Originally Posted by 95673641
i need help with a program
the output should be the first and last number from a set of number that you input. i am confused.
can anybody help me? i appreciated!

Create an ArrayList of the Integer type. Then use an infinite loop:
Code:

`for(; ;){}`
to ask for input using a Scanner object until the user notifies that he is done entering numbers. For each number entered, add this number to your ArrayList.

Finally, use the ArrayList methods found in the Java API to get the first and last numbers.
• 03-28-2011, 04:38 AM
Junky
If the only objective is to display the first and last number then storing all the numbers in between is pointless. Just use 2 variables and a boolean to determine if/when to store the first value.
• 03-28-2011, 05:06 AM
Solarsonic
Quote:

Originally Posted by Junky
If the only objective is to display the first and last number then storing all the numbers in between is pointless. Just use 2 variables and a boolean to determine if/when to store the first value.

This could be possible but this is the noob and overcomplicated way to do it imo. How would you know which is the last value if there's no fixed amount of numbers?
• 03-28-2011, 05:17 AM
yellowledbet
Quote:

Originally Posted by Solarsonic
This could be possible but this is the noob and overcomplicated way to do it imo. How would you know which is the last value if there's no fixed amount of numbers?

if the input is of unknown length, you could set the value to the same variable through each iteration until a sentinel value is reached.
• 03-28-2011, 05:22 AM
Fubarable
Quote:

Originally Posted by yellowledbet
if the input is of unknown length, you could set the value to the same variable through each iteration until a sentinel value is reached.

Exactly. And it's a lot easier to do than use ArrayLists, which I can guarantee that the OP hasn't learned yet.
• 03-28-2011, 05:23 AM
Solarsonic
Quote:

Originally Posted by yellowledbet
if the input is of unknown length, you could set the value to the same variable through each iteration until a sentinel value is reached.

True, but in a realistic situation all of the values would be useful to be stored. I agree this is easier though.
• 03-28-2011, 05:26 AM
yellowledbet
Quote:

Originally Posted by Solarsonic
Plausible, but in a realistic situation all of the values would be useful to be stored.

absolutely :D unless memory is an issue and you in fact only need the two variables.
• 03-28-2011, 09:05 PM
95673641
problem is to write a program to print the first and last number from the list using keyboard and While-Loop
the list contains 5,6,-2,-10,100,-7
the output should be 5 and -7
• 03-28-2011, 09:07 PM
95673641
Quote:

Originally Posted by 95673641
i need help with a program
the output should be the first and last number from a set of number that you input. i am confused.
can anybody help me? i appreciated!

problem is to write a program to print the first and last number from the list using keyboard and While-Loop
the list contains 5,6,-2,-10,100,-7
the output should be 5 and -7
• 03-28-2011, 09:56 PM
Solarsonic
Quote:

Originally Posted by 95673641
problem is to write a program to print the first and last number from the list using keyboard and While-Loop
the list contains 5,6,-2,-10,100,-7
the output should be 5 and -7

Ill get you started; you'll be using the Robot and KeyEvent classes for the keyboard part. Use a variable and keep setting it to to the next number in the while loop so when the user says he is done inputting you have the last number.
• 03-28-2011, 10:30 PM
UJJAL DHAR
Quote:

to print first and last number from a set of input numbers
i need help with a program
the output should be the first and last number from a set of number that you input. i am confused.
can anybody help me? i appreciated!
I have posted a code in your last thread.Didn't you see that??
Code:

`<Mod edit: deleted>`
Regards,
Ujjal