Results 1 to 11 of 11
  1. #1
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default Please help with a do--while loop

    Hello,
    I am very new to java so please excuse me for asking noob questions!

    I am trying to create a do--while loop in my program which repeats itself upon user input of y or Y. Here is the code. All help is much appreciated!
    -stevo



    import java.util.Scanner;

    public class myBook
    {
    public static void main (String[] args)
    {
    String another = "y", title, author, publisher;
    int year;

    Scanner scan = new Scanner (System.in);

    do
    {
    System.out.print("Enter book info:\nTitle:");
    title = scan.nextLine();

    System.out.print("Author:");
    author = scan.nextLine();

    System.out.print("Publisher:");
    publisher = scan.nextLine();

    System.out.print("Year:");
    year = scan.nextInt();
    System.out.println();

    Book book1 = new Book(title, author, publisher, year);
    System.out.println(book1);

    System.out.println();
    System.out.print ("Enter another book (y/n)? ");
    another = scan.nextLine();
    }
    while (another.equalsIgnoreCase("y"));
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default

    Does the code work ok? If not please explain what the problem is and show us the console output.

    I suspect it's Scanner's buffering of input (and lineends) that is the problem. nextInt does not clear the line end that is in the buffer so the next read could return only the lineend that is in the buffer.

    Printout the value of another to see what is being read:
    System.out.println("another=" + another +"<");
    Last edited by Norm; 06-20-2011 at 09:00 PM.

  3. #3
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Hi, thanks for your reply.

    The code works fine, but when i get to the end where it asks user to input y/n the program automatically thinks i hit y. Here is the console output:


    Enter book info:
    Title:java
    Author:lewis
    Publisher:addison wesley
    Year:2008

    Title: java
    Author: lewis
    Publisher: addison wesley
    Year: 2008

    Enter another book (y/n)? Enter book info:
    Title:asdf
    Author:asdf
    Publisher:asdf
    Year:3

    Title: asdf
    Author: asdf
    Publisher: asdf
    Year: 3

    Enter another book (y/n)? Enter book info:
    Title:

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default

    You might have missed this edit to my last post:

    Printout the value of another to see what is being read:
    System.out.println("another=" + another +"<");

  5. #5
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    here is the output when i add that println:




    Enter book info:
    Title:a
    Author:s
    Publisher:d
    Year:3

    Title: a
    Author: s
    Publisher: d
    Year: 3

    another=y<
    Enter another book (y/n)?
    And this is where the program ends^

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default

    By ends do you mean the execution exits the do{} while loop and then the program?

    Why don't the questions and answers show up on the last post you made?
    Here is what I get when I execute the code:
    PHP Code:
    D:\JavaDevelopment\Testing\ForumQuestions5>java MyBook
    Enter book info:
    Title:fsdfdssdf
    Author:fdsfdsfsd
    Publisher:ffsdsdf
    Year:123
    
    
    Enter another book (y/n)? another=<
    
    D:\JavaDevelopment\Testing\ForumQuestions5>

  7. #7
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Yeah sorry, the whole program exits there. Im not sure whats going on?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default

    Do you print the value of another before or after you read it from the Scanner?
    Your console shows that you print the value BEFORE it is changed by read.
    Set another's initial value to "XYZ" or anything but what the user will type in.
    What you want to see is the value that is read and that is used by the while statement to test if to continue the loop.

  9. #9
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Here is what happens. Its like i cant change another.


    Enter book info:
    Title:l
    Author:l
    Publisher:l
    Year:2

    Title: l
    Author: l
    Publisher: l
    Year: 2

    Enter another book (y/n)? another=<

    Again, this is where the program exits.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default

    I mentioned in post#2 that the Scanner class will leave stuff in the buffer that can cause you problems.
    If you do not read the lineend character, it is still there. nextInt() does NOT read the lineend. nextLine does read the next endline. Read the Scanner class's API doc for these methods to understand what they do.
    Use nextLine to clear out the buffer before asking for and receiving the user's input.

  11. #11
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

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. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 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
  •