aspose file tools*
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
Author

Taking in input at the command line

Thomas N. Williams
Greenhorn

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Thomas,
Welcome to JavaRanch!

The Scanner class JavaDoc actually uses reading input from System.in (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

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Thomas N. Williams
Greenhorn

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.run(Run.java:35)
at Run.main(Run.java:42)


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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thomas N. Williams
Greenhorn

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Taking in input at the command line