Results 1 to 11 of 11
- 06-20-2011, 08:54 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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"));
}
}
- 06-20-2011, 08:57 PM #2
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.
- 06-20-2011, 09:00 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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:
- 06-20-2011, 09:04 PM #4
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 +"<");
- 06-20-2011, 09:08 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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^
- 06-20-2011, 09:32 PM #6
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>
- 06-20-2011, 09:42 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Yeah sorry, the whole program exits there. Im not sure whats going on?
- 06-20-2011, 09:54 PM #8
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.
- 06-20-2011, 10:03 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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.
- 06-20-2011, 10:09 PM #10
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.
- 06-20-2011, 10:28 PM #11
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks