WebCarnage
asked on
User inputting Strings
I'm not an advanced Java programmer yet. So I'm sure this is just code I simply havn't ever come across before. What I'm trying to do is get input from a user, and make the input a string, instead of a char, or int or anything else. Here's some sample code I've been trying to get work.
String userUser is the users's input for a username, and string userPass is the user's input for the password. And I'm trying to compare them with two predefined strings (user, and pass).
//Password program
public class Password
{
public static void main(String[] args) throws Exception
{
System.out.println("Welcom e to SomeCoolPlace.");
System.out.println("Please enter your username and password.");
System.out.println("-- type 'newuser' as your username for help with setting");
System.out.println(" up a new user account.");
String nu = "newuser";
String user = "okay";
String pass = "go";
String userUser = (String)System.in.read(); System.in.read();
if(userInput.equalsIgnoreC ase(nu))
newUser();
while(userInput.equals(use r)) {
System.out.println("Passwo rd: ");
String userPass = (String)System.in.read(); System.in.read();
if(userPass.equals(pass)){
System.out.println("Hello " + user);
System.out.println("Please select an navigational option below.");
}
}
}
}
String userUser is the users's input for a username, and string userPass is the user's input for the password. And I'm trying to compare them with two predefined strings (user, and pass).
//Password program
public class Password
{
public static void main(String[] args) throws Exception
{
System.out.println("Welcom
System.out.println("Please
System.out.println("-- type 'newuser' as your username for help with setting");
System.out.println(" up a new user account.");
String nu = "newuser";
String user = "okay";
String pass = "go";
String userUser = (String)System.in.read(); System.in.read();
if(userInput.equalsIgnoreC
newUser();
while(userInput.equals(use
System.out.println("Passwo
String userPass = (String)System.in.read(); System.in.read();
if(userPass.equals(pass)){
System.out.println("Hello " + user);
System.out.println("Please
}
}
}
}
It is lots more convenient to have a Keyboard.class to read input from user if your program is not the GUI one.
With this Keyboard.class, you can just read user input of String(or other type of variable such as int, double, etc)
with code like this:
String userUser = Keyboard.readString();
you may get a copy of Keyboard.class(with source code as well) at:
http://duke.csc.villanova.edu/jss/keyboard.html
Hope this can help.
With this Keyboard.class, you can just read user input of String(or other type of variable such as int, double, etc)
with code like this:
String userUser = Keyboard.readString();
you may get a copy of Keyboard.class(with source code as well) at:
http://duke.csc.villanova.edu/jss/keyboard.html
Hope this can help.
System.in only reads one keystroke at a time. I think you can chain the input stream to a DataInputStream and read a full line. I haven't tried this, so I'm not certain it works, but you may want to give it a shot:
// chain the System's input stream to a DataInputStream object
DataInputStream ds = new DataInputStream( System.in );
String username = ds.readLine();
// compare input name to user name
if(user.equals( username ))
Good luck.
// chain the System's input stream to a DataInputStream object
DataInputStream ds = new DataInputStream( System.in );
String username = ds.readLine();
// compare input name to user name
if(user.equals( username ))
Good luck.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
kokchoon78 way is correct, the Keyboard.class I suggest is actually implement on all these Reader in the java.io package.
It is better to follow kokchoon78 ways since my way is just a shortcut and it won't help you much in improve your java skill.
It is better to follow kokchoon78 ways since my way is just a shortcut and it won't help you much in improve your java skill.
kokchoon78,
Thanks for checking on that. My reply was quick because I was on my way out the door.
Thanks for checking on that. My reply was quick because I was on my way out the door.
ASKER
Try telling me what you added, and what they do.
Don't just give me code to cut and paste :\. Thanks anyways, though.
Don't just give me code to cut and paste :\. Thanks anyways, though.
Greetings,
Added code :
//This code is to read user name from console
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
String userUser = br.readLine();
BufferedReader will reformat the Input Stream and provides readLine method to read a list of char (String).
and i did the samething to get the password.
for more information on how to use InputStream / Reader, please visit :
http://java.sun.com/docs/books/tutorial/essential/io/index.html
hth,
Kok Choon.
Added code :
//This code is to read user name from console
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
String userUser = br.readLine();
BufferedReader will reformat the Input Stream and provides readLine method to read a list of char (String).
and i did the samething to get the password.
for more information on how to use InputStream / Reader, please visit :
http://java.sun.com/docs/books/tutorial/essential/io/index.html
hth,
Kok Choon.
regards,
Kok Choon.