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)
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:
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
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.
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.
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?
I was also wondering, if I changed the following lines of my implementation code to the following:
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.
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.