Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Password Checker  RSS feed

 
ari shusterman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this assignment from school to write a password checker program that will check passwords from a text file( I, or the user will put the passwords in there). The program will check these things for each password
1. a password must have at least characters
2. A password conists of only letters and digits
3. A password must contain at least two digits'

If these conditions are satisfied, the program displays the password, then a hyphen, and to the right of that "Valid Password".
If these conditions are not satisfied, the program displays the password, then a hyphen, and to the right of that "Invalid Password".

It checks all the passwords in the text file and keeps printing out whether it is valid or invalid. It also needs to catch all exceptions. Below is the code that I wrote so far. I believe that it is most of the code needed for this program to run.
The problem is, the program keeps telling me that the file that i made and put passwords inside is not found. Can somoene help me get this to work

Note: Command Line Arguments must be used.





[NK: Added CodeTags. UseCodeTags for code formatting]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ari shusterman wrote:It also needs to catch all exceptions.


That's a horrible bit of instruction. Is that literally what the requirement is? Or is it something more reasonable, like, "Error conditions must be handled appropriately"?

Below is the code that I wrote so far.


When you post code, please UseCodeTags(⇐click) so it will be readable.

The problem is, the program keeps telling me that the file that i made and put passwords inside is not found.


It's not lying. So you need to find out where the discrepancy is between your assumptions and what Java is actually doing. A good way to do that is to put in a bunch of print statements. For instance:


There are a couple other problems in your code that jump out at me. (And there may be others I missed.)


If args.length is 0, you'll get an ArrayIndexOutOfBoundsException at line A.

At B, you check if there are no args, you print out a message to enter a file, but then you just go on as if everything is fine. You either need to exit there (which seems to me what you should do, since you are required to use command line args), or else you need to have the user type in a file (which seems to be what you're trying to do).


And welcome to the Ranch!
 
ari shusterman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I don't use args[0], how will I be able to tell the program that the file im referring to is the one user inputs. Args[0] is the first oar fumet entered by the user isn't it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ari shusterman wrote:If I don't use args[0],


I didn't say don't use it. I'm saying you have to check if there even is an args[0] before trying to use it.

Either that or just assume it's there and let your program blow up if the user doesn't provide an arg at startup. That's perfectly valid, but since you're already checking for its existence with the args.length check, I figured you didn't want to just blow up.

The point is, if you're going to do that args.length test, you have to do it before referring to args[0]. If the test would return true (because there are no args), then the args[0] that you currently have ahead of it will give you the exception before you even get to the test. So, with your current code, by the time you get to the test, you already know that args.length == 0 will be false.

 
ari shusterman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What else is wrong with it. Where shoul I do the try catch
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ari shusterman wrote:What else is wrong with it. Where shoul I do the try catch


I'm sorry, but I don't think I can help you. A question like that suggests to me that you are viewing this as "What do I type to make it work?" as if it's a matter of memorizing the right magic words like Harry Potter.

You need to take a step back and do two important, largely independent tasks:

1) Understand the problem and describe a solution clearly and precisely, in very small, basic steps, without regard to Java. Do it in English or pseudocode or your native language or some combination of those.

2) Take the time to study and understand the Java concepts involved (such as exception handling) without any regard to your particular homework problem.

"What do I type" is not an effective way to program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!