• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring - .lastIndexOf - I have it backwards  RSS feed

 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am still working on my assignment and I have my substring flipped around and I can't figure out why.

My String description = an item in my JComboBox (which is populated by an array[])



I just want the description for my constructor, but I am getting the $ amount of my string instead (I am stepping through the code and watching the variables)

It is probably something simple, but I can't see it. Thanks for any help. Brent

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent Van Scoy wrote:
I just want the description for my constructor, but I am getting the $ amount of my string instead (I am stepping through the code and watching the variables)

It is probably something simple, but I can't see it. Thanks for any help. Brent


Well, what does the JavaDoc for the java.lang.String class, say that the substring() method, that takes a single int, is supposed to return?

Henry
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mine is



java docs:

int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
int lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index


description is a String. My thought is I want to return a String, so I choose subString. Because I want to go backwards from "-"
I choose .lastIndexOf and then -1 so I don't get my "-"

Since description is a String, I was not looking for an int
debugging shows that I am getting a String, just the wrong portion.

I'm still looking. Thanks


 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this would be acceptable, but it does allow me to capture the portion of the String I want.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent Van Scoy wrote:I don't know if this would be acceptable, but it does allow me to capture the portion of the String I want.




This is why in my hint, I asked about the substring() method, and not about the lastIndexOf() method. Do you know what the javadoc says about the difference between the two substring() methods, and how they are used?

Henry
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I do know know about the 2 methods. The 1st method only has 1 parameter which is the starting point. The 2nd method has two parameters with the 2nd one being one index beyond the last character to be copied.

Thanks for the tip. I could see I had my parameters backwards.

Thanks, Brent
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!