Link to home
Start Free TrialLog in
Avatar of mte01
mte01Flag for Lebanon

asked on

Problem when reading from stdin - Command Line

Hey experts,

I have the following lines of code:

        try
        {
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter userid: ");
            LoginUser = stdin.readLine();
            System.out.println("Enter password: ");
            LoginPass = stdin.readLine();
            System.out.println("end try");
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

I am running them on the command line, I am successfully entering the userid and password, but the program is never reaching the "end try" statement....any help on solving the problem??
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Areyou 100% sure that's the code you're running?
Please delete the duplicate you just posted before anyone posts to it
>> Please delete the duplicate you just posted before anyone posts to it
It's no duplicate. Here he tries to run on the command prompt.
In the other Q he tries to run it in netBeans.
mte01,
...and since it runs fine in netBeans it will run fine on the command prompt too.
Avatar of mte01

ASKER

ok..now I realized that I am doing a method before it...when I am removing this method, it's printing the end try successfully, but with it it's having this problem...this is the method:

public void performConnection()
    {
        try
        {
            socket = new Socket(host, port);
        }
        catch (UnknownHostException e)
        {
            System.out.println("ERROR: Can't find host: " + host);
            return;
        }
        catch (IOException e)
        {
            System.out.println("ERROR: Can't open socket on rendezvous port "
                           + port + " (on host " + host + ").");
            return;
        }

        //Try to open streams to read and write from the socket.
        try
        {
            os = socket.getOutputStream();
            is = socket.getInputStream();
        }
        catch (IOException e)
        {
            System.out.println("ERROR: Created data socket but can't "
                           + "open stream on it.");
            System.out.println("...Disconnecting.");
            return;
        }

        //Try to open a socket to the port.

        if ((os != null) && (is != null))
        {
            //Let the main applet thread know we've successfully rendezvoused.
            commandAvailable = false;
            new LoginThread(this).start();
        }
        else
        {
            System.out.println("ERROR: Port is valid but communication failed. "
                           + "Please TRY AGAIN.");
        }
    }


performConnection is being called before the beginning of the try block in the question.....
If you run it in debug mode in netBeans you'll immediately see where it gets stuck
Avatar of mte01

ASKER

>>zzynx

In netbeans, it's not getting stuck anywhere....it's ignoring the prompts, and continuing through the program.......while on the command line, as I said the end of try is not reached when I am using the above method....
Add some System.out.println() in your performConnection() so that you know what call is blocking
Avatar of mte01

ASKER

Something new...now it's printing the end try, but it's never exiting the program!....very weird!!
>> now it's printing the end try, but it's never exiting the program!.
1) What did you change in your code?
2) How do you know it doesn't exit?
Avatar of mte01

ASKER

>>zzynx

No, wait, this is normal because there is a thread that is still running (new LoginThread(this).start() in performConnection())....now after using your program I don't have a problem in the command prompt with the above, I need to see a problem in the method that follows the block in the question.....I'll see if I'll close this question in a few minutes.....

>> now after using your program I don't have a problem in the command prompt with the above
Aha!!!

>> I'll see if I'll close this question in a few minutes.....
OK
Thanks
>> ok..now I realized that I am doing a method before it

IOW the comment i posted was correct?:

>> Areyou 100% sure that's the code you're running?