Results 1 to 6 of 6

Thread: while loop

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

    Exclamation while loop

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Post what you've done, where your problems are and what specific issues you are having. We're not a code service, but can help you along the way with specific problems.

  3. #3
    95673641 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    public static void main(String[] args) {
    // print the first & last number from the list
    //{5,6,-2,-10,100,7}
    int num,w;
    Scanner keyboard=new Scanner(System.in);
    System.out.println("enter the numbers from the list");
    w=1;
    while(w>=6)
    {

    num=keyboard.nextInt();
    w++;
    }
    }
    }
    this is what i got so far
    i don't know what do i need to print out the first and the last number from the list

  4. #4
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    When you are taking inputs using scanner,put them on a array list.then just print the array[0] and array[arraylength-1]
    Simple..


    Java Code:
    <moderator edit: code deleted>
    See,this should work.Please read your book before posting this type of easy problem which can be solved by a newer easily.


    Regards
    Ujjal
    Last edited by Fubarable; 03-29-2011 at 01:19 AM. Reason: deleted spoon-fed answer

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    ...and a great example of spoonfeeding

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Posting Permissions

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