1

I am trying to extract information from a file that is formatted as follows:

1 [email protected]|password|false 

However, I seem to be getting ArrayIndexOutOfBounds errors when running the following code and I am unable to determine the reason for this as I believe that my splitting should be functioning correctly. The error is obtained on the line beginning with "users".

 sc = new Scanner (System.in); BufferedReader in = new BufferedReader (new FileReader (USAVE)); int repeats = Integer.parseInt(in.readLine()); for (int c = 0; c < repeats; c++){ String info = in.readLine(); System.out.println (info); String[] extracted = info.split("\\|"); users.addUser(extracted[0], decryptPassword(extracted[1])); } in.close(); 

What could be the problem?

EDIT: I have changed "|" to "\|" but the problem persists.

EDIT2: StackTrace

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at OnlineCommunications.userFromFile(OnlineCommunications.java:165) at OnlineCommunications.logIn(OnlineCommunications.java:36) at OnlineCommunications.emailOption(OnlineCommunications.java:593) at OnlineCommunications.main(OnlineCommunications.java:683) 

The method I have posted above is the one named userFromFile.

4 Answers 4

6

String#split(regex) expects regex as a parameter and | is a meta character(special character) in regex world. In order to treat a meta charcter as a normal character you should escape it with backslash(\|)

String[] extracted = info.split("\\|"); 

or just include it inside a charcter class

String[] extracted = info.split("[|]"); 

Below are the meta characters in regex:

<([{\^-=$!|]})?*+.> 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer but it seems that I'm still getting the error. :( I had previously tried using "," to separate the values as well.
@mCode if i apply the split to [email protected]|password|false i get emailadd at index 0 password at index 1 and false at index 2. are you sure this is thesource of your exception. can you update the post with stacktrace please.
@PremGenError I have updated with a stacktrace (I am not sure what a stacktrace is so please let me know if that is not it).
@mCode whats on line 165 ?? and are you sure info prints out [email protected]|password|false??
@PremGenError Line 165 contains users.addUser(extracted[0], decryptPassword(extracted[1]));. I am assuming that info should be putting out the mentioned String because the in.readLine() should be telling it to behave as such. Perhaps it is an error of the BufferedReader? I should add that System.out.println(info); does not print anything before the error.
2

String.split(String regex) takes a regular expression as an argument, use:

String[] extracted = info.split("\\|"); 

1 Comment

Thank you for your answer but it seems that I'm still getting the error. :( I had previously tried using "," to separate the values as well.
1

similar post. Tokenizing Error: java.util.regex.PatternSyntaxException, dangling metacharacter '*' You have to use like this :

String[] extracted = info.split("\\|"); 

1 Comment

Thank you for your answer but it seems that I'm still getting the error. :( I had previously tried using "," to separate the values as well.
0

Actually there is nothing wrong with how you are parsing the string. The error lies elsewhere. I would add System.out.println (repeats) just before you enter the loop to make sure you are iterating the correct number of times. To debug even further, I would print the contents of extracted (Arrays.toString(extracted)) before the line invoking user.addUsers. If all that looks good, then the problem lies in the user.addUsers invocation.

1 Comment

I would look closer at what is going on in the user.addUser line. You didn't post the code, so I can't help you there. But, if you take that line out and just println extracted[0] and extracted[1] you don't get ANY errors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.