• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String index out of range error

 
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic