Results 1 to 6 of 6
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default [SOLVED] What am I doing wrong?

    Hi all I have included the excerpt of the code I'm working with for an assignment and I can't understand why it is not accepting an input from the terminal:

    Java Code:
    if (choice == '1')
    		{
    			System.out.print("random string ");
    			System.out.flush();
    			BufferedReader crazy = new BufferedReader (new InputStreamReader(System.in));
    			info = crazy.readLine();
                    }
    there is a little bit of stuff that would normally happen after that, but this thing is not allowing me to enter an input. Why would it do that?

    When I move it out of the 'if' statement it works just fine, is there any reason why this statement shouldn't function within an 'if'?
    Last edited by porchrat; 04-02-2009 at 10:46 PM.

  2. #2
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default

    well i am puzzled too.

    Shouldn't you normally ask for the user input first and then make the decision dependent on the input the user made?

    What does the exact assignment sound like? That would perhaps help to help ;)
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by mcfrog View Post
    well i am puzzled too.

    Shouldn't you normally ask for the user input first and then make the decision dependent on the input the user made?

    What does the exact assignment sound like? That would perhaps help to help ;)
    Technically the user has made a decision before this already (hence the 'if' condition), now I want the user to enter a string, but instead it just terminates the program :(

    Also I'm against putting the entire assignment up as I don't want someone answering the entire thing for me, I need to learn, it is just that this strange thing has me completely stumped.

  4. #4
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default

    call me crazy but perhaps the if-clause is never entered. Maybe it is not '1' but "1". (char vs. String) In that case you must use String#equals()
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  5. #5
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by mcfrog View Post
    call me crazy but perhaps the if-clause is never entered. Maybe it is not '1' but "1". (char vs. String) In that case you must use String#equals()
    Well I assumed the 'if' was entered into because "random string" is printed to the screen. Is that not necessarily true?

    Also I used this higher up the program:
    Java Code:
    char choice = (char) System.in.read();
    I assumed the explicit cast to char made it a char, is that not correct?
    EDIT: Therefore when the user enters a char 'choice', if that char is '1' then it will enter the 'if' statement?
    Last edited by porchrat; 04-02-2009 at 11:10 PM.

  6. #6
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    OK I have found the problem it was this line:

    Java Code:
    char choice = (char) System.in.read();
    I wrapped a BufferedStreamReader around an InputStreamReader instead and that seems to have solved it.

    Thank you for your time :)

Similar Threads

  1. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 03:55 AM
  2. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 03:50 PM
  3. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 12:45 PM
  4. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM

Posting Permissions

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