File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Taking in input at the command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Taking in input at the command line" Watch "Taking in input at the command line" New topic

Taking in input at the command line

Thomas N. Williams

Joined: Mar 11, 2010
Posts: 4
Hi there. I've attached some code for my program in which I have to enter a series of usernames and passwords, add them to a binary search tree and print them out. I have only ever used input from a text file or in the main method, but for the next part I have to input the usernames and passwords at the command line. Can anyone help me out, perhaps show me which parts to modify? Thanks.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Welcome to JavaRanch!

The Scanner class JavaDoc actually uses reading input from (console input) as one of the examples. It is similar to reading from a file. The biggest difference is that you'll want to alternate:
prompt for user id
read user id
prompt for password
read password

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Thomas N. Williams

Joined: Mar 11, 2010
Posts: 4
Thanks Jeanne, I worked on it a bit and came up with this:

which returns

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "Finally" to complete TryStatement

at Run.main(

Is this a correct way of getting the usernames and passwords? Could anyone help me get rid of this error? Thanks
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

You're running this code from an IDE, right? Make sure you fix all compilation problems before you run your application.

How To Ask Questions How To Answer Questions
Thomas N. Williams

Joined: Mar 11, 2010
Posts: 4
Hi Rob, yes, I am using Eclipse. I changed it to this:

which got rid of the error, but the BST doesn't appear to print out. Also, is there a way to loop the inputs so I can enter more than just the one username and password? Thanks
Bob Livingston

Joined: Apr 06, 2010
Posts: 4
Thomas from the code that you just added it doesn't input the name nor password into the BSTree anywhere.

To add your second question that would be an easy fix just add something that asks the user how many users and passwords they would like to enter capture that number and loop through.

Something like

double holder = 0;

System.out.println("How many users would you like to enter?")
holder = userInput.nextDouble();

for (int num=0; num<holder; num++)
// rest of your code
I agree. Here's the link:
subject: Taking in input at the command line
It's not a secret anymore!