Reputation: 125
I am simulating a simple chat room by creating a client and server relationship. I'm just using the Netbeans IDE to run this. When the user presses enter, whatever they typed into the IDE console is sent to the server and then echoed back.
console = new BufferedReader(new InputStreamReader(System.in));
while (thread != null) {
try {
streamOut.writeUTF(console.readLine());
streamOut.flush();
} catch(IOException ioe){
System.out.println("Sending error: " + ioe.getMessage());
stop();
}
}
I am trying to create a login/register system where the user is asked to enter username and password. I want the program to pause execution until the enter key is pressed. What I currently have is cutting off the first character from what is entered for username
System.out.println("Type 1 if you are an existing user \nType 2 if you want to register");
System.in.read();
System.out.println("Username:");
System.in.read();
System.out.println("Password:");
Sample output
Connected: Socket[addr=/127.0.0.1,port=10233,localport=52932]
Type 1 if you are an existing user
Type 2 if you want to register
2
Username:
foo
Password:
52932: oo
bar
52932: bar
Upvotes: 0
Views: 1214
Reputation: 530
I would do this with a BufferedReader.
Something like this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Username: ");
String username = reader.readLine();
System.out.print("Password: ");
String password = reader.readLine();
Please note, it is bad practice to send passwords in plain text and to store them in String objects. An attacker would be able to sniff the plain text username and password without much hassle and there is a chance that the String's data could be read before it is destroyed.
Upvotes: 1