Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Little help please

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Little help please

    import java.util.Scanner;
     
    public class Donations
    {
       public static void main(String[] args)
       {
          Scanner keyboard= new Scanner (System.in);
          String donorName;
          int donationAmount, donationQuantity=0, maxDonation=0;
     
          do
          {
     
             System.out.println("Please enter your name");
             donorName=keyboard.nextLine();
     
             System.out.println("Please enter donation amount");
             donationAmount=keyboard.nextInt();
     
             donationQuantity=donationQuantity+1;
     
             maxDonation +=donationAmount;
     
     
          }
          while (maxDonation<500);
     
          System.out.println("It took " + donationQuantity + " donations to reach the £500");
     
     
       }//main
    }//class

    For some reason when the program loops the second time for a new customer to enter the name, the amount prompt appears directly below it, therefore not being able to enter the 2nd name. Any ideas guys?
    Last edited by Norm; February 13th, 2013 at 06:02 PM. Reason: changed quote to code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Little help please

    The Scanner class's nextInt() method leaves the lineend character in its buffer. The next call to the nextLine() method reads and returns that lineend character as an empty line.
    Add a call to the nextLine() method after calling the nextInt() method to read the lineend character.

    See also http://www.javaprogrammingforums.com...html#post95898
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Little help please

    I changed them about, now this is my output:

    Please enter donation amount
    65

    Please enter your name
    Please enter donation amount


    Therefore wont let me enter my name, sorry I am a beginner.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Little help please

    Please post the current version of the code.

    The order of the calls won't change what the nextInt() method does. It still leaves the lineend character in its buffer.
    To see that nextLine() reads an empty line, add a println() statement to print out its value after it is read.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Little help please

    import java.util.Scanner;
     
    public class Donations
    {
       public static void main(String[] args)
       {
          Scanner keyboard= new Scanner (System.in);
          String donorName;
          int donationAmount, donationQuantity=0, maxDonation=0;
     
          do
          {
     
             System.out.println("Please enter donation amount");
             donationAmount=keyboard.nextInt();
     
             System.out.println("\nPlease enter your name");
             donorName=keyboard.nextLine();
     
     
             donationQuantity=donationQuantity+1;
     
             maxDonation +=donationAmount;
     
     
          }
          while (maxDonation<500);
     
          System.out.println("It took " + donationQuantity + " donations to reach the £500");
     
     
       }//main
    }//class

    So, why is it reading an empty line tho, why won't it wait for the user input?

  6. #6
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Little help please

    4

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Little help please

    donorName=keyboard.next(); fixed the issue instead of donorName=keyboard.nextLine();

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Little help please

    That would work as long as the name doesn't have any spaces in it.
    If you don't understand my answer, don't ignore it, ask a question.