Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

t.split(".") returns ArrayIndexOutOfBounds exception.

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
I have a file of strings similar to:
AC00.1
AC00.2
AC11.3

I am using the following code:

String[] MyPrg = new String[2];

while ((t = in.readline()) != null) {
MyPrg = t.split(".");
System.out.println(MyPrg[0].toString());
}

I get error on first entry:
AC00.1
EXCEPTION: java.lang.ArrayIndexOutOfBoundsException: 0
java.lang.ArrayIndexOutOfBoundsException: 0
at Role2Tkn.main(Role2Tkn.java:224)

I would expect :
MyPrg[0] to contain AC00
and
MyPrg[1] to contain 1

What am I overlooking here?

Thanks for any help!!!
 
Appart from that, you don't need the toString clause, it's already a String array.

Cheers,
Dian
 
Hmmm...I guess its a bug in Eclipse that led me astray. I tried escpaing it and receiving a eclipse warning saying that this character did not need to be escaped.


I'll try out your suggestion. Thanks!
 
Perhaps you used just one backslash.
You need two - one to mask the dot, but since the backslash is used for escaping on the file-level, you have to escape it too.
\n is a newline
\t is a tab
\. is bogus, because a dot doesn't need to be escaped.
\\. is backslash + dot.

seeking a job as java-programmer in Berlin:
 
So for those like me that don't understando regular expresions, do we need to use "." or "\." to split?

I guess the problem comes because t hasn't what's suppose to have. I'd print t and chech the size of the splitted array before printing the results.

Cheers,
Dian
 
Oh, and I seriously doubt it's an Eclipse bug. If Eclipse and you disagree, I bet you're wrong.

Cheers,
Dian
 
Hi

Dian said:
So for those like me that don't understando regular expresions, do we need to use "." or "\." to split?
The period ( . ) in regular expressions means any character. But while aswolff wants to split on the literal period character, not on any character, must escape the period.

( In some languages with slightly different regular expression handling [tt]"AC00.1".split(".").length[/tt] would be 6. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top