Results 1 to 13 of 13
  1. #1
    95673641 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by 95673641 View Post
    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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    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. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by yellowledbet View Post
    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. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by yellowledbet View Post
    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. #9
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    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. #10
    95673641 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

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

    Default

    Quote Originally Posted by 95673641 View Post
    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. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by 95673641 View Post
    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. #13
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default


    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.

Similar Threads

  1. Replies: 10
    Last Post: 12-20-2010, 12:47 PM
  2. Replies: 6
    Last Post: 12-08-2010, 12:39 AM
  3. how to multiply numbers in rows and print it next to it
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 06:24 PM
  4. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 02:15 AM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 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
  •