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("Welcome 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.equalsIgnoreCase(nu))
               newUser();
          while(userInput.equals(user)) {
               System.out.println("Password: ");
               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.");
               }
          }
     }
}
WebCarnageAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kokchoon78Commented:
please specify your question... :)

regards,
Kok Choon.
0
b612_foreverCommented:
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.
0
fivesigmaeventCommented:
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

kokchoon78Commented:
Hi,

   the readLine() method in DataInputStream is deprecated. so you can try BufferedReader, here is the example :

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Password
{
    public static void main(String[] args) throws Exception
    {
         System.out.println("Welcome 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";

         try
         {
     BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
     String userUser = br.readLine();

            if(userUser.equalsIgnoreCase(nu))
            {
      System.out.println("New User");
     }
      else if (userUser.equals(user))
            {
         System.out.println("Password: ");
         String userPass = br.readLine();
         if(userPass.equals(pass))
         {
              System.out.println("Hello " + user);
              System.out.println("Please select an navigational option below.");
         }
         else
         {
       System.out.println("Invalid Password.");
      }
        }
        else
        {
     System.out.println("Invalid User.");
     }
        br.close();
}
catch ( Exception e )
{
     e.printStackTrace();
}

}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b612_foreverCommented:
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.

0
fivesigmaeventCommented:
kokchoon78,

Thanks for checking on that. My reply was quick because I was on my way out the door.
0
WebCarnageAuthor Commented:
Try telling me what you added, and what they do.

Don't just give me code to cut and paste :\. Thanks anyways, though.
0
kokchoon78Commented:
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.
           
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.