Results 1 to 10 of 10
  1. #1
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default 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?
    Java 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");
      }
    }
    edited post - removed attachment on advice
    Last edited by Nadian; 01-19-2011 at 05:04 PM. Reason: shouldn't have attached code

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    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!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    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!

  4. #4
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    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().
    thanks I hadn't understood that scanner missed the enter like that, but it makes sense as it acts like I've pressed enter twice..

  5. #5
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default 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

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    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!

  7. #7
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default typecasting?

    would reading it as a string then convert to boolean be typecasting? i.e
    new boolean=Boolean.parseBoolean(string),

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    Quote Originally Posted by Nadian View Post
    would reading it as a string then convert to boolean be typecasting? i.e
    new boolean=Boolean.parseBoolean(string),
    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!

  9. #9
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default 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

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    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

  1. Stuck while loop
    By ile4 in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 08:17 PM
  2. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:52 PM
  3. Stuck in sea
    By programmer_007 in forum JDBC
    Replies: 1
    Last Post: 09-17-2009, 04:00 AM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 02:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 02:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •