• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

ArrayList StringIndexOutOfBound Exception

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having problems trying to get my main method to work. It keeps saying that I have a StringIndexOutOfBound Exception, I know what it means but I am not sure how to fix it. I would appreciate it if someone could explain it to me so I could better understand the concept. (I am relatively new with learning ArrayLists)
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you include the error message and stack trace to help with diagnosing the issue?
Take a look at the stack trace, and it should tell you exactly which line of code it is occurring on, which is the first step to fixing it.

If its a StringIndexOutOfBoundsException it will be related to your String manipulation code rather than ArrayLists.
The most likely place for this to happen in the code you have here is the substring call. If it can't find a comma in the string, it will try and do a substring to character -1 which will produce the exception.


Rather than constantly calling cities.get(i) I would recommend calling it once and storing it into a local variable.


The other thing you might want to look at for Lists is an Iterator (loops through the list giving you all the values without you having to use indexes all the time)
There is an enhanced for loop which is much easier than using the traditional method using i:
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to include that I did use the debugger, and it is exactly this line of code that's giving me the exception.


This was not an issue before, but my teacher said I could not hardcode therefore I had to change a few lines of code in my implementation class which caused me to get the StringIndexOutofBound Exception in my main method, I did go through the stack trace, but I cannot figure out how to manipulate the substring so that it does not give a -1

(this how my code looked like before):
 
Saloon Keeper
Posts: 8437
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like your program is not finding a comma in you cityStateZip. Try printing it out prior to calling substring();
??? JOptionPane is a Swing class but I don't see you starting up a window or frame in main.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here is the change in the code that has revealed this bug:



Take another look at the new expression. In what situations do you think it will evaluate to true? When will it be false? Is it doing what you want it to?

What should you do if the user doesn't type in a comma separating the city and state information?
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm treating it as a hybrid class, there's no need to pass JOption swing again(??), in my implementation class, I am using it in order to get the user's input and I am using that input to extract the city which is then followed by a comma, how can I print it out and then find the comma?

I am confused as to how to proceed. In my original code, it was finding the comma but without hardcoding, it is not. I'm not sure how I can correct the problem.
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, so I found that if I left


It would not go past that condition given that I changed the following lines in my implementation class:

and

}
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrea,

I hope it's not too late, but welcome to the Ranch!

The central issue in the code is the "initialization block" you have here:


Unlike an Array, an ArrayList is designed to be dynamic in size. You are creating ten " " Strings within the cities ArrayList.
The user begins to add their input after those placeholders, resulting in:
" "," "," "," "," "," "," "," "," "," ","Beverly Hills, CA, 90210"

What then happens when you attempt to determine the indexOf(",") in a String that is only " "?

You don't want to "initialize" an ArrayList with placeholders. Your ArrayList<String> cities= new ArrayList<String>(); statement did all the "initialization" you needed.
Instead, think of a way to allow users to enter as much data as they want. Are you familiar with sentinel values?

Cheers!
Chris
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I'm not quite familiar with sentinel values. But I am sure I could look them up. Thank you.
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was also wondering, if I changed the following lines of my implementation code to the following:


instead of



would it make a difference at all?

Because as I was going through the debugger, I found that if I kept my code as the following :



When calling the method extractcity, it would not pass the condition. Would changing the order of the main method solve the problem, so that instead of looking like this

it would look like this


Thank you for all the help in advance. I do appreciate it, I'm just really confused as to what to change and what not to change. Because it seems that if I try fixing something, another problem arises.
 
Carey Brown
Saloon Keeper
Posts: 8437
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get rid of your '10' count and just let the size of the ArrayList expand as needed. Do away with the prefilling of cityStateZip with " ". Remove all tests of " " as it should no longer be needed.
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Carey mentioned, that "initialization block" shouldn't be there at all as ArrayLists are designed to be dynamic in size.
If the ArrayList doesn't contain " " placeholders, then the whole issue you are being forced to deal with by having break on equals(" ") within subsequent for loops disappears.

Of course, if you get rid of the placeholders, the size of cityStateZip is going to be 0 initially (as it should be). And that is going to require you to rethink this for loop:

Also, this solution above doesn't provide the user much flexibility. You are telling the user they can either press "Q" or enter 10 more values. What if I wanted to enter 11 values?
I think a sentinel controlled while loop or do...while loop would make more sense.

Cheers!
Chris>
 
Andrea Del
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I see what Carey means. It really helped. And I will try the while loop, it seems more efficient and makes more sense that way. Thank you very much.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic