?
Solved

User inputting Strings

Posted on 2003-03-19
8
Medium Priority
?
209 Views
Last Modified: 2010-08-05
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
Comment
Question by:WebCarnage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:kokchoon78
ID: 8171550
please specify your question... :)

regards,
Kok Choon.
0
 
LVL 1

Expert Comment

by:b612_forever
ID: 8171609
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
 

Expert Comment

by:fivesigmaevent
ID: 8171709
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 4

Accepted Solution

by:
kokchoon78 earned 40 total points
ID: 8171857
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
 
LVL 1

Expert Comment

by:b612_forever
ID: 8171898
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
 

Expert Comment

by:fivesigmaevent
ID: 8173274
kokchoon78,

Thanks for checking on that. My reply was quick because I was on my way out the door.
0
 

Author Comment

by:WebCarnage
ID: 8178397
Try telling me what you added, and what they do.

Don't just give me code to cut and paste :\. Thanks anyways, though.
0
 
LVL 4

Expert Comment

by:kokchoon78
ID: 8178732
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question