• Post Reply Bookmark Topic Watch Topic
  • New Topic

JOption pane keeps prompting error over and over  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, line 33 is causing me some trouble, well the whole statement. If my user inputs any special character, the program outputs the error over and over. Any ideas on some possible ways around this?



 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this way the loop will stop when valid is false.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, nice. I am also receiving the error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2

I normally receive this error when I forget an = sign, however I don't think this is the case.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need the full stack trace -- as you know -- to help you with that.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really too familiar with how that all works with the settings and folders
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I view the full stack trace?
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally when you see an exception like that, the stuff directly below it is the stack trace.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at Password.checkLetters(Password.java:39)
at Password.main(Password.java:23)
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that's it. So here's how to read it: Look at each line, starting from the top. Each line in the stack trace refers to a single line of code, which could be your code or somebody else's code which was called by your code. The first line, for example, is a line from the java.lang.String class. You know that class, you use it all the time. And it refers to a line of code which is in the String class's charAt() method. It says "Unknown Source", which means that the JVM couldn't figure out anything about the source code which the String class was compiled from. But that doesn't matter to you because it's not your code.

So go down to the next line. It refers to a line of code in the Password class -- that's your code, obviously. And it refers to a line of code which is in the checkLetters() method, specifically the code at line number 39 of the Password class. Your text editor or IDE should tell you which line is number 39; if it doesn't than that could be inconvenient, but at any rate you would look for a line of code in the checkLetters() method which calls String's charAt() method.

That's the first reference to your code, so that's the line of code you should be looking at because it's the line which is guilty of passing an out-of-range String index to the charAt() method. And you have to find out why it did that.

Does that make sense?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it makes sense, only that I don't know why those lines are throwing the error. they both say

Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.

Don't know what to do in regards to this
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are the lines throwing the error



 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good.  Now the exception is a StringIndexOutOfBoundsException, so look for what could be cause the String index to be too big or small.  The only thing that could cause that is here: So what is i at that point?  Look at the surrounding loop:
So i can be zero, which valid and it can be up to the length of the String.  But how many indices does a five character String have?  Zero through four.  So five is invalid.  What would you do to the for loop so that all indices are valid?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5 is supposed to be invalid, anything above zero is to be invalid, as the user is not supposed to use special characters. only thing I can think of doing is something like
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:5 is supposed to be invalid, anything above zero is to be invalid, as the user is not supposed to use special characters. only thing I can think of doing is something like
Look closely to your use of "length".
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so you think change it too something like

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I switched it up, and I seem to have gotten rid of my string out of bounds error. Now I have the problem of organizing this mess.

Do you have any reccomendations on how you would output the specific things that are missing from the password the user has input.

The length must be at least 8, it must have at least 2 digits, and no special characters. I don't think the if statements seem to be rite for this.

This whole code is a mess, it is very difficult to organize, as I am not allowed to use methods in this.


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop guessing. You can guess 1,000,000× and get it right, as long as the guesses are all different. All you have to do now is work out which is the correct guess. Or you can go back to what you were taught.

There is a basic format for a for loop. Always start from that basic format, even if you have to change the numbers later. Reasons for changing the numbers include,
  • 1: Not wanting to iterate the entire data structure.
  • 2; Wanting to manipulate elements along with their preceding element.
  • 3: Wanting to process elements along with their following element.
  • 4: Wanting to iterate it backwards.
  • And this is the basic format:-Start by writing your for loop like that. Don't simply write length; it is something.length, well myArray.length, or myString.length() or myList.size(). Use one of those formats after <.
    Then look at your requirements. Do you need to iterate the entire array without dealing with other elements at the same time? Are you iterating the array forwards? By the time you have answered enough of those questions, you wil know whether you need to tweak my basic form, or use the basic form unchanged.
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is an update to the code, it is a bit cleaner now, my if statements at the bottom are what I was reffering to, is their a simpler way to do such? rather than code an if statement for every possible way the password could be invalid?

     
    Carey Brown
    Saloon Keeper
    Posts: 3323
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Musts:
  • Clean up indentation - it's a mess.
  • Remove comments that are self evident by the code.
  • Remove OptionPane from inside of loop - it gets called more than once if you have more than one special character.
  • In OptionPane don't print the entered password - this goes against privacy standards.
  • Create loop to re-prompt per requirements.

  • Suggestion:
    Create variables for: length, letterCount, digitCount, specialCount and loop through the password incrementing these counters. Then present error message(s). If it were me, I'd like to see all my errors together so that if I had a password that was too short AND didn't have two digits it would let me know. That way I wouldn't have to fix the password multiple times.
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great advice, in regards to the creating of variables, and incrementing counters to display error messages. How would you display all of these errors at once in the same window with the JOption pane.

    if ( specialCharacters > 0) {

    }

    if ( lengthTest > 0){
    }

    if (digitTest > 0) {
    }

    what would  you do to get every test that is greater than 0 to show up in the dialog box?
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is what I have come up with, now i think the only problem I am having is that
    1.) answer is never valid, I always receive the prompt that I must use at least 8 letters.

    2.)sometimes the prompts will show up multiple times and sometimes they show only once.

     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A simple way could go like this:
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look carefully at the above code.  It's not doing what you think it is doing.
     
    Carey Brown
    Saloon Keeper
    Posts: 3323
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for cleaning up the indents. Doesn't that look better?


     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are still short of {} after the ifs. That can lead to problems later on.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!