[Logo]
Forums Register Login
String index out of range error
I'm getting a String index out of range error with this line of code: field = splitString.substring(0,index);

Here is a portion of my code:

String field = "";
String pattern = "[,\\s]";

while ((line = br.readLine()) != null)
{


String[] splits = line.split(pattern);
for (String splitString : splits)
{
if (splitString != null && !splitString.isEmpty())
{
System.out.println(splitString);
index = splitString.indexOf("=");

field = splitString.substring(0,index);

The String that is being passed through the while loop looks like this:
Printed Name=printed name, Reason=Wrecked, Signature of Owner=signature, vehicleMake=Honda]

What exactly am I doing wrong and how can I correct it?
Your key value pairs are separated by comma AND followed by space
Your pattern [,\\s] actually mean comma OR space
You don't need the square bracket
 

Raymond Tong wrote:Your key value pairs are separated by comma AND followed by space
Your pattern [,\\s] actually mean comma OR space
You don't need the square bracket



I'll change the pattern to mean comma.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1093 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 16, 2018 01:04:05.