HELP on while loops problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazzer
    New Member
    • Mar 2008
    • 6

    HELP on while loops problems

    Hi,
    I'm beginner at Java. I'm confused with my simple program of using while loops

    int i = 1;
    while (i <= input){
    System.out.prin tln("Enter description of product #" + i + " : ");
    item = keyboard.nextLi ne();

    System.out.prin tln("Enter price of " + item + " : ");
    price = keyboard.nextDo uble();
    i++;
    }

    I just wondering why when i run the program, the output will be:

    Enter description of product #1 :
    Enter price of :
    3
    Enter description of product #2 :
    Enter price of :
    2

    and while i change a bit to

    for (int i = 1; i <= input; i++){
    System.out.prin tln("Enter description of product #" + i + " : ");
    item = keyboard.nextLi ne();
    }

    the output will be:

    Enter description of product #1 :
    Enter description of product #2 :
    3

    really need helpp...

    Many thanks.
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by blazzer
    Hi,
    I'm beginner at Java. I'm confused with my simple program of using while loops

    [CODE=JAVA]

    int i = 1;
    while (i <= input){
    System.out.prin tln("Enter description of product #" + i + " : ");
    item = keyboard.nextLi ne();

    System.out.prin tln("Enter price of " + item + " : ");
    price = keyboard.nextDo uble();
    i++;
    }

    I just wondering why when i run the program, the output will be:

    Enter description of product #1 :
    Enter price of :
    3
    Enter description of product #2 :
    Enter price of :
    2

    and while i change a bit to

    for (int i = 1; i <= input; i++){
    System.out.prin tln("Enter description of product #" + i + " : ");
    item = keyboard.nextLi ne();
    }

    [/CODE]

    the output will be:

    Enter description of product #1 :
    Enter description of product #2 :
    3

    really need helpp...

    Many thanks.
    Hey there, is there additional class/code feeding into this one. Also, and I can be way off, I am still an apprentice; you should probably initialize from zero here:

    [CODE=JAVA]

    int i = 0;
    while (i = 0; i <= input; i++){

    [/CODE]

    Do you have to start at one?

    In a bit!

    Dököll

    Comment

    • blazzer
      New Member
      • Mar 2008
      • 6

      #3
      Originally posted by Dököll
      Hey there, is there additional class/code feeding into this one. Also, and I can be way off, I am still an apprentice; you should probably initialize from zero here:

      [CODE=JAVA]

      int i = 0;
      while (i = 0; i <= input; i++){

      [/CODE]

      Do you have to start at one?

      In a bit!

      Dököll
      if i start from zero, the product # will start from 0 too..
      it will print product #0 instead of 1.

      i'm confused that why it doesnt ask for the first input first then jump to second one..
      i cant enter product describtion..it will jump to price right away...
      any idea?

      many thanks...

      Comment

      • blazzer
        New Member
        • Mar 2008
        • 6

        #4
        apperantly i've changed using "for" loops
        but it doesn't change at all..

        for (int i = 1; i <= input; i++){
        System.out.prin tln("Enter description of product #" + i + " : ");
        item = keyboard.nextLi ne();

        System.out.prin tln("Enter price of " + item + " : ");
        price = keyboard.nextDo uble();
        total_Price += price;

        }

        is there any problem there? it works fine when asking first one..but once it loops, it will skip the description..
        any idea?

        many thanks

        blazzer

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by blazzer
          if i start from zero, the product # will start from 0 too..
          it will print product #0 instead of 1.

          i'm confused that why it doesnt ask for the first input first then jump to second one..
          i cant enter product describtion..it will jump to price right away...
          any idea?

          many thanks...
          I can't get how do you like to have the output....
          Can you post some output?

          And also, your expected correct output....

          Just confirming,
          sukatoa

          Comment

          • blazzer
            New Member
            • Mar 2008
            • 6

            #6
            Originally posted by sukatoa
            I can't get how do you like to have the output....
            Can you post some output?

            And also, your expected correct output....

            Just confirming,
            sukatoa
            this is the current output:

            How many items the customer is purchasing:
            2
            Starting new customer...
            Enter description of product #1 :
            bread
            Enter price of bread :
            12
            Enter description of product #2 :
            Enter price of :
            12

            I'm expected the output will ask for the description first and jump to price.

            but the actual output is..when reach to second loops and more, it doesnt ask for the description, it jump to price without asking for description.

            The problem is..after looping.

            Comment

            • sukatoa
              Contributor
              • Nov 2007
              • 539

              #7
              Originally posted by blazzer
              this is the current output:

              How many items the customer is purchasing:
              2
              Starting new customer...
              Enter description of product #1 :
              bread
              Enter price of bread :
              12
              Enter description of product #2 :
              Enter price of :
              12

              I'm expected the output will ask for the description first and jump to price.

              but the actual output is..when reach to second loops and more, it doesnt ask for the description, it jump to price without asking for description.

              The problem is..after looping.
              Since you are using nextLine(), the value after the previous input in that current line will automatically captured when it invoke at the next input........ i don't know what character it is....

              Because, invoking nextLine() will seek for "\n" for the end of the input....

              use next()... try to observe.....

              sukatoa

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #8
                You can do the test at your original post... the while loop...

                regards,
                sukatoa

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  A nextDouble() call stops reading at the character that can't be the last part of
                  a String representation of a double number and leaves it in the input buffer.

                  Note that an end of line character can never be part of s String representation of
                  a double number.

                  A nextLine() call reads everything including the end of line character from an
                  input buffer. Given these three observations reread the original code: the loops
                  use different methods: nextDouble and nextLine.

                  kind regards,

                  Jos

                  Comment

                  Working...