Results 1 to 18 of 18
  1. #1
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default What's the "turn me on" button here?

    Java Code:
    import java.io.*;
    import java.util.*;
    import acm.program.*; 
    import acm.util.ErrorException;
    
    
    public class myMaze extends ConsoleProgram {
    	public void run ( ) {
    		int row=0, cols; 
    		Stack<String> mazeStack= new Stack<String>( ); 
    
    //1. Get the maze structure from the user. 
    		InputStreamReader isr = new InputStreamReader(System.in); 
    		BufferedReader buffer = new BufferedReader(isr); 
    		println("please input your maze here: "); 
    		
    		try {
    			String line = buffer.readLine( ); 
    			while (line!=null) {
    				mazeStack.push(line); 
    			}
    			line = buffer.readLine( );  		
    		} catch (IOException ex) {
    			throw new ErrorException(ex); 
    		} 
    		
    		println("out of maze" + mazeStack); 
    	}
    }
    I am trying to have the user input a row of "1"s and "0"s and push the line into the stack. However, when running the above code, my console has no reactions when I punching my keyboard. All it prints out is one line says: "please input your maze here:".

    What am I missing here?

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

    Default

    Comments on your code:
    How do you exit the while(...) loop?
    What do you do with what's read into line after the while loop?

    You should echo what is read into line for debugging purposes.

  3. #3
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    well, I have "while (line!=null)"...when the user end with "ctrl+Z", then the line is NULL. No?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by niu_niu View Post
    well, I have "while (line!=null)"...when the user end with "ctrl+Z", then the line is NULL. No?
    If you have a while-loop like this:

    Java Code:
    while (<condition>)
       <body>
    ... and there's nothing in the <body> that can change your <condition> you have effectively create an endless loop.

    kind regards,

    Jos

    p.s. ignoring break or return or System.exit(<whatever>) of course.

  5. #5
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    import acm.program.*; 
    import acm.util.ErrorException;
    
    
    public class myMaze extends ConsoleProgram {
    	public void run ( ) {
    		int row=0, cols; 
    		Stack<String> mazeStack= new Stack<String>( ); 
    
    //1. Get the maze structure from the user. 
    		InputStreamReader isr = new InputStreamReader(System.in); 
    		BufferedReader buffer = new BufferedReader(isr); 
    		println("please input your maze here: "); 
    		
    		try {
    			String line = buffer.readLine( ); 
    				mazeStack.push(line); 
    				println("endless love");				
    		   line = buffer.readLine( );  		
    		} catch (IOException ex) {
    			throw new ErrorException(ex); 
    		} 
    		
    		println("out of maze" + mazeStack); 
    	}
    }
    I took the while statement out completely. and it is still giving me the same problem. BTW, I didn't think it was a while problem from the start.

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

    Default

    What do you see now on the console? Does your println() output anything?
    Do you end your input by pressing the Enter key?
    The readLine() method reads all of a line up to the Enter key press.

  7. #7
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Since i dont have any of the acm library, i converted the code to standard java:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Stack;
    
    class Main {
        public static void main (String [] args ){
            int row=0, cols;
            Stack<String> mazeStack= new Stack<String>( );
    
            //1. Get the maze structure from the user.
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader buffer = new BufferedReader(isr);
            System.out.println("please input your maze here: ");
    
            try {
                String line = buffer.readLine( );
                while (line!=null) {
                    mazeStack.push(line);
                    System.out.println ("Pushed to stack");
                }
                line = buffer.readLine( );
            } catch (IOException ex) {
                System.out.println ("An IOException occurred" + ex.getLocalizedMessage());
            }
    
            System.out.println("out of maze" + mazeStack);
        }
    }
    I can enter the input fine using the buffer.readln(), however it then goes into a perpetual loop and runs out of memory.

    I believe what you wanted to do with the loop is this:

    Java Code:
    while (line!=null) {
        mazeStack.push(line);
        line = buffer.readLine( ); 
    }
    Otherwise you are continously adding the same line to the stack without changing it or reading another line.

    Further more, i dont believe line will ever be null. It may be empty but the object will still exist.
    Therefore i suggest you use:
    Java Code:
                while (!line.equals("")) {
                    mazeStack.push(line);
                    line = buffer.readLine( );
                    System.out.println ("Pushed to stack");
                }
    Hope this helps!
    Berkeleybross

  8. #8
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    import acm.program.*; 
    import acm.util.ErrorException;
    
    
    public class myMaze extends ConsoleProgram {
    	public void run ( ) {
    		int row=0, cols; 
    		Stack<String> mazeStack= new Stack<String>( ); 
    
    //1. Get the maze structure from the user. 
    		InputStreamReader isr = new InputStreamReader(System.in); 
    		BufferedReader buffer = new BufferedReader(isr); 
    		println("punch the keyboard to turn me on: "); 
    		
    		try {
    			String line = buffer.readLine( ); 
    			while (!line.equals("")) {
                    mazeStack.push(line);
                    line = buffer.readLine( );
                    System.out.println ("Pushed to stack");
                } 		
    		} catch (IOException ex) {
    			throw new ErrorException(ex); 
    		}	
    			
    	}
    }
    thanks for the reply. but I am still having the same problem. it has no reaction at all when I punch the keyboard.

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Ok a few questions to hopefully clear things up.

    When you type something in, does it actually display what you are typing on the console?
    Is their a cursor or anything like that to indicate it is trying to read a line?

    Does the ErrorException get thrown at any point? It should produce a stack trace which would indicate a problem.

    Can you also try the following code for the exception:

    Java Code:
        catch (IOException ex) {
            System.out.println ("An IOException occured");
            throw new ErrorException(ex); 
        }
    It seems a bit odd that you are catching an exception and then throwing another one immediatly doing nothing else. It kind of goes against my ideas of throwing exceptions!

    Berkeleybross

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

    Default

    it has no reaction at all when I punch the keyboard.
    Do you end your input by pressing the Enter key?
    The readLine() method reads all of a line up to the Enter key press.

  11. #11
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    import acm.program.*; 
    import acm.util.ErrorException;
    
    
    public class myMaze extends ConsoleProgram {
    	public void run ( ) {
    		int row=0, cols; 
    		Stack<String> mazeStack= new Stack<String>( ); 
    
    //1. Get the maze structure from the user. 
    		InputStreamReader isr = new InputStreamReader(System.in); 
    		BufferedReader buffer = new BufferedReader(isr); 
    		println("punch the keyboard to turn me on: "); 
    		
    		try {
    			String line = buffer.readLine( ); 
    			while (!line.equals("")) {
                    mazeStack.push(line);
                    line = buffer.readLine( );
                    System.out.println ("Pushed to stack");
                }
    		}
    			catch (IOException ex) {
    		        System.out.println ("An IOException occured");
    		        throw new ErrorException(ex); 
    		    }				
    	}
    }
    When you type something in, does it actually display what you are typing on the console?
    Is their a cursor or anything like that to indicate it is trying to read a line?
    it doesn't display anything I type in. it's like using a keyboard that is not connected at all. there is a cursor blinking but it is not moving at all when I typed.

    Does the ErrorException get thrown at any point?
    Nope. nothing. not even with the new code you gave me. :(((

    It seems a bit odd that you are catching an exception and then throwing another one immediatly doing nothing else. It kind of goes against my ideas of throwing exceptions!
    I try to be normal and thanks for helping me.

    So, I am still having the exact same problem.

  12. #12
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Do you end your input by pressing the Enter key?
    Yes. I did. It didn't work.

    What is that thing in your mouth? a cigarette? and what's that in your hand?

  13. #13
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I think i know your problem.

    RTFM! :P

    You are using a "ConsoleProgram" which includes its own read and write methods. Obviously this is conflicting with standard java.

    I dont have access to the class so i will have to guess at the code, but i think you should be using readLine(); not a buffer.readline();

    Java Code:
    import java.io.*;
    import java.util.*;
    import acm.program.*; 
    import acm.util.ErrorException;
    
    
    public class myMaze extends ConsoleProgram {
        public void run ( ) {
            int row=0, cols; 
            Stack<String> mazeStack= new Stack<String>( ); 
    
    //1. Get the maze structure from the user. 
            println("punch the keyboard to turn me on: "); 
            try {
                String line = readLine( ); 
                while (!line.equals("")) {
                    mazeStack.push(line);
                    line = readLine( );
                    println ("Pushed to stack");
                }
            }
            catch (IOException ex) {
                println ("An IOException occured");
                throw new ErrorException(ex); 
            }				
        }
    }
    It doesnt look like you need the try - catch statements either, but ive left them in just in case (experiment as needed)

    Hope this helps! (If it does, feel free to rep me :P *Shameless self promotion*)
    Berkeleybross

  14. #14
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Hope this helps
    hahaha!!! it works. yes, consoleProgram was the culprit! my program is on turned on....or at least for the part it is containing.

    how come you know so much about java?

  15. #15
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Glad it works now!

    how come you know so much about java?
    Im studying computer science for my degree. I've been doing java for two years, and played with pascal / delphi for several years before that.
    Also, google is your friend. :P

    Since it worked, you can rep me if you want :D

    Berkeleybross

  16. #16
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    rep me
    oh, sorry! I just figured our what you meant by 'rep me'...okay..i just 'repped' you.

  17. #17
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    sounds like you are quite young. somehow I imaged you as a grey beard round face guy...poor imagination.

  18. #18
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Haha no, im only 20! Working on that beard tho... (not gray just yet)

    Cheers for the rep :) (My first one!)

    I would try and guess something about you but my imagination is worse than yours! You'll have to give a few clues first...

    Berkeleybross

Similar Threads

  1. Replies: 0
    Last Post: 06-07-2010, 06:23 PM
  2. Adding "Choose File" button to applet
    By mju4t in forum Java Applets
    Replies: 1
    Last Post: 09-17-2009, 08:11 AM
  3. Replies: 8
    Last Post: 03-09-2009, 11:32 AM
  4. Replies: 10
    Last Post: 12-30-2008, 03:46 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •