• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Taking in input at the command line

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic