Results 1 to 10 of 10
Thread: Stuck in a bad loop
- 01-19-2011, 03:57 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Stuck in a bad loop
Hello all, newbie here, trying to write a very simple program which lets me write in a question, three possible answers, then stores them to file. Mostly working fine but for one strange thing. On the first run through i can enter question and answers, BUT when it loops after I have entered 'true' to continue entering questions it skips past the enter new question input and goes straight to the answer input stage, i cannot work out why?
edited post - removed attachment on adviceJava Code:import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import static java.lang.System.in; import static java.lang.System.out; public class Teacher { public static void main(String args[]) throws FileNotFoundException{ Scanner input=new Scanner(in); PrintStream output=new PrintStream("revisionquestions.txt"); PrintStream ans=new PrintStream("revisionanswers.txt"); int questionId=0; int answerId; String answers; String question; boolean yes=true; for (;yes;){ out.print("Enter question: "); question=input.nextLine(); output.println(question); for (int x=0;x<3;x++){ out.print("Enter answer: "); answers=input.nextLine(); /*problen here on second loop*/ ans.println(answers); } questionId++; out.print("question ID "); out.println(questionId); out.print(" Do you wish to add another question:(true or false) "); yes=input.nextBoolean(); } out.print("fin"); } }Last edited by Nadian; 01-19-2011 at 05:04 PM. Reason: shouldn't have attached code
- 01-19-2011, 04:36 PM #2
To post code, make sure you use the CODE tags to preserve formatting. Not many people are going to be willing to download your attached file.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-19-2011, 05:05 PM #3
Scanner.nextBoolean() does not scan past the current line, so it doesn't absorb the last "enter" pressed by the user after typing true.
Then when you get back to question = input.nextLine(), that absorbs that last "enter", giving you an empty String for the value of question.
You could simply call input.nextLine after you call input.nextBoolean().How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-19-2011, 05:08 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
- 01-19-2011, 05:11 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Solved
thanks for that, i hadn't understood how scanner misses the enter, it makes sense as it was like I'd pressed enter twice.
Last edited by Nadian; 01-19-2011 at 05:13 PM. Reason: terrible english
- 01-19-2011, 05:13 PM #6
Think about it this way: what if you had a whole row of booleans you wanted to read in via a Scanner? You would call Scanner.nextBoolean() for each one, which wouldn't read in the entire line, just the very next boolean.
Another way to do this would be to use nextLine() instead of nextBoolean(), then convert the String returned from nextLine() into a boolean.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-19-2011, 05:38 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
typecasting?
would reading it as a string then convert to boolean be typecasting? i.e
new boolean=Boolean.parseBoolean(string),
- 01-19-2011, 06:03 PM #8
What happened when you tried that?
Depending on your definition of typecasting, that could be considered typecasting.
Although, you are mixing the syntax up a bit. You wouldn't need the new keyword if you're just trying to reference the Boolean returned from Boolean.parseBoolean() or Boolean.valueOf().How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-19-2011, 06:29 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
typecasting
I didn't try it as your solution has worked, and I'm not currently trying to read from several booleans in a line, but i may be back for help later when i'm trying to read my questions and answers back from the text files
- 01-19-2011, 06:52 PM #10
The parseBoolean() or valueOf() approaches might be a better way to go, I'm not sure. Either way it wouldn't hurt to try :p
I only provided the example of reading several booleans in a line to show why nextBoolean() worked the way it did.
I'm glad you got it working though!How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Stuck while loop
By ile4 in forum New To JavaReplies: 2Last Post: 12-06-2010, 08:17 PM -
I'm stuck help!!!
By nobody58 in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 02:52 PM -
Stuck in sea
By programmer_007 in forum JDBCReplies: 1Last Post: 09-17-2009, 04:00 AM -
really stuck now..
By shongo in forum Advanced JavaReplies: 2Last Post: 11-09-2008, 02:56 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks