Results 1 to 6 of 6
  1. #1
    MrHouse is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Problem with 'while' loop, PrintStream

    So,I am trying to create a program that gives random answers to questions asked by the user,and then stores the Q&A in a text file.

    Here's the code:

    Java Code:
    import static java.lang.System.in;
    import static java.lang.System.out;
    import java.util.Scanner;
    import java.util.Random;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import java.io.File;
    
    
    public class QALogger {
    
    
    	
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		Scanner myscan = new Scanner(in);
    		PrintStream printer = new PrintStream("log.txt");
    		Random myran = new Random();
    		
    		int inum;char reply = 'y';
    		String quest;
    		
    		while (reply =='y' || reply == 'Y')
    		{
    			out.println("Ask a Question! ");
    			quest = myscan.nextLine();
    
    			printer.println();
    			printer.println(quest);
    			
    			
    			inum = myran.nextInt(5) + 1;
    			switch (inum)
    			{
    			case 1:
    				out.println("Yes!Isn't that obvious?");
    				printer.println("Yes!Isn't that obvious?");
    				break;
    			case 2:
    				out.println("No!And do not ask again!");
    				printer.println("No!And do not ask again!");
    				break;
    			case 3:
    				
    				out.println("Possible,but not necessary...");
    				printer.println("Possible,but not necessary...");
    				break;
    			case 4:
    				out.println("That is a very stupid question!");
    				printer.println("That is a very stupid question!");
    				break;
    			case 5:
    				out.println("I cannot decide right now.");
    				printer.println("I cannot decide right now.");
    				break;
    			default:
    				out.println("BEEP BOP BEEP ERROR! ERROR! ERROR!");
    				printer.println("BEEP BOP BEEP ERROR! ERROR! ERROR!");
    				break;
    			}
    			out.println("Do you wish to ask another question?(Y/N) ");
    			reply = myscan.findWithinHorizon(".",0).charAt(0);
    			
    			}
    		}
    
    }
    The problem is,whenever I input y at the end,the loop starts again,but it does not let me enter the question again.
    Problem with 'while' loop, PrintStream-error.jpg

    And,is it possible to print text on the screen AND in the text file without writing the message twice?As you can see,I have written the same text two times: inside out.println and inside printer.println.
    Last edited by JosAH; 07-17-2013 at 05:16 PM. Reason: fixed the [code] ... [/code] tags

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Problem with 'while' loop, PrintStream

    I wonder why you read the question using nextLine(), and read if you want to continue yes/no using findWithinHorizon(). Both cases are the same: you want to get some input from the user.

    And,is it possible to print text on the screen AND in the text file without writing the message twice?
    Its never a question of it being possible, you want to ask how to do it. Perhaps you can't just yet, since you're still in the phase where you are cramming all your code in the main() method. When you are a little further in your study and you start to create your own classes and methods, then you will be able to answer your own question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Problem with 'while' loop, PrintStream

    There are still characters in the input buffer after you try and set reply. I would replace the last statement with.

    Java Code:
    reply = myscan.nextLine().charAt(0);
    I also suggest you read the API on the Scanner methods and do some experimenting to ensure you understand what is happening.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    MrHouse is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem with 'while' loop, PrintStream

    Quote Originally Posted by gimbal2 View Post
    I wonder why you read the question using nextLine(), and read if you want to continue yes/no using findWithinHorizon(). Both cases are the same: you want to get some input from the user.
    What if the user doesn't want to type another question?The input at the end will prevent the loop from starting again(Yes,I know there should be an exit in case of 'n',but it really doesn't matter here...its just a program I made to put together some of the stuff I have learned)

    Quote Originally Posted by jim829 View Post
    There are still characters in the input buffer after you try and set reply. I would replace the last statement with.

    Java Code:
    reply = myscan.nextLine().charAt(0);
    I also suggest you read the API on the Scanner methods and do some experimenting to ensure you understand what is happening.

    Regards,
    Jim
    Yup,that worked!

    And yeah,I should read the API.It will help me understand the functions better.
    Last edited by MrHouse; 07-17-2013 at 04:25 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Problem with 'while' loop, PrintStream

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Problem with 'while' loop, PrintStream

    Quote Originally Posted by MrHouse View Post
    What if the user doesn't want to type another question?
    Obviously you didn't understand what I said. But luckily jim829's direct code example is more clear to you. I hope you can now work out for yourself why that code works, and yours didn't. Understanding is more important than getting it to work.

Similar Threads

  1. PrintStream
    By gyijhbk in forum New To Java
    Replies: 3
    Last Post: 05-01-2012, 11:12 AM
  2. PrintStream file manipulation
    By mDennis10 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 02:37 AM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. NullPointerException with PrintStream write
    By vinci in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2011, 04:21 AM
  5. Do uncaught errors appear in the PrintStream?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 08:56 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
  •