Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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.");
               }
          }
     }
}
0
WebCarnage
Asked:
WebCarnage
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now