Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taking in input at the command line

 
Thomas N. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas N. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're running this code from an IDE, right? Make sure you fix all compilation problems before you run your application.
 
Thomas N. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic