File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Out" Watch "Out" New topic
Author

Out

andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Sorry to bother you all... But, I am getting an Out of Bounds Exception on Line 15. String content = line.split("\"")[1];
It says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Validator.main(Validator.java:15)

***UPDATE: Now for no reason at all. I didn't change A THING, I am no longer getting the Array Out of Bounds Exception...???*** What the heck???

If I change the [1] to a [0], I don’t get the exception anymore, but obviously it doesn’t return the right values.

Also, the if statements loop through everything 5 times! No good! The point of the project is to validate input data. Thanks!!!

This is my input, and I just want it to check each line once, not for every method. I messed up somewhere bad...
FIRSTNAME "William" #
LASTNAME "Rodgers" #
ADDRESS "7500 Fox Court, Pensacola FL 32506" #
USERNAME "wrodgers" #
PASSWORD "foxtrot!" #

and this is the output I am getting:

VALID FIRSTNAME "William" #
XXXXX FIRSTNAME "William" #, UNRECOGNIZED FIELD
XXXXX FIRSTNAME "William" #, UNRECOGNIZED FIELD
XXXXX FIRSTNAME "William" #, UNRECOGNIZED FIELD
XXXXX FIRSTNAME "William" #, UNRECOGNIZED FIELD
XXXXX LASTNAME "Rodgers" #, UNRECOGNIZED FIELD
VALID LASTNAME "Rodgers" #
XXXXX LASTNAME "Rodgers" #, UNRECOGNIZED FIELD
XXXXX LASTNAME "Rodgers" #, UNRECOGNIZED FIELD
XXXXX LASTNAME "Rodgers" #, UNRECOGNIZED FIELD
XXXXX ADDRESS "7500 Fox Court, Pensacola FL 32506" #, UNRECOGNIZED FIELD
XXXXX ADDRESS "7500 Fox Court, Pensacola FL 32506" #, UNRECOGNIZED FIELD
VALID ADDRESS "7500 Fox Court, Pensacola FL 32506" #
XXXXX ADDRESS "7500 Fox Court, Pensacola FL 32506" #, UNRECOGNIZED FIELD
XXXXX ADDRESS "7500 Fox Court, Pensacola FL 32506" #, UNRECOGNIZED FIELD
XXXXX USERNAME "wrodgers" #, UNRECOGNIZED FIELD
XXXXX USERNAME "wrodgers" #, UNRECOGNIZED FIELD
XXXXX USERNAME "wrodgers" #, UNRECOGNIZED FIELD
XXXXX USERNAME "wrodgers" #, MALFORMED STRING
XXXXX USERNAME "wrodgers" #, UNRECOGNIZED FIELD
XXXXX PASSWORD "foxtrot!" #, UNRECOGNIZED FIELD
XXXXX PASSWORD "foxtrot!" #, UNRECOGNIZED FIELD
XXXXX PASSWORD "foxtrot!" #, UNRECOGNIZED FIELD
XXXXX PASSWORD "foxtrot!" #, UNRECOGNIZED FIELD
XXXXX PASSWORD "foxtrot!" #, MALFORMED STRING

This is my code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You will have to print out the full details of that array, so you can see its size change.
You appear to be splitting something on a quote mark; if there are no quotes in that String, it will split into a 1-element array, and you can only use index [0]. Remember index [1] entails a 2-element array.

Do you know how to throw Exceptions? You might consider using a MalformedPasswordException instead of those System.out.println() calls. By the way: it is customary to use System.err for error messages.
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Ok, well I figured ot why it was doing the exception. I had my cursor far down on the page and it was reading in empty lines...*sigh* another issue. And no, I have only been working with Java for a few weeks, so I don't know how to do anything like that. I fixed a bunch of the repeated stuff by disabling the else statements, and adding a few lines of code, but it is still reading through everything more than once. As far as all the print statements, it is the way my teacher wants it to appear. We have to match her output EXACTLY or no credit. Not even an extra whitespace can appear.

For example, just to test, I input:
FIRSTNAME "Sarah" #
LASTNAME "Jones" #

And this is now my output"

VALID FIRSTNAME "Sarah" #
XXXXX FIRSTNAME "Sarah" #, UNRECOGNIZED FIELD
VALID LASTNAME "Jones" #
XXXXX LASTNAME "Jones" #, UNRECOGNIZED FIELD

This is my new code with the changes made in the checkFirstname, checkLastname, etc. methods:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You appear to be checking whether a line is a first name, whether the same line is a last name, whether the same line is an address, and something else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Out