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

1. Member
Join Date
Mar 2011
Posts
5
Rep Power
0

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!

2. 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.

3. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
7
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:
Java 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.

4. 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.

5. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
7
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?

6. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
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.

7. 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.

8. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
7
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.

9. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
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.

10. Member
Join Date
Mar 2011
Posts
5
Rep Power
0
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

11. Member
Join Date
Mar 2011
Posts
5
Rep Power
0
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

12. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
7
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.

13. Senior Member
Join Date
Apr 2010
Location
Posts
180
Rep Power
0

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??
Java Code:
`<Mod edit: deleted>`
Regards,
Ujjal
Last edited by Fubarable; 03-29-2011 at 01:55 AM.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•