• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Remove the leading chars from an array element if it contains "-"?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was told to use indexOf.  I know that will find the index of the char "-" but how to I code my program to set only the chars that follow the "-".


Here is a snippet of my code.

address.setStreetNumber(streetInfo[0]);

if(streetInfo[0].contains("-") {
streetInfo[0].indexOf('-');

//figure out some code that tells it to store what is after the "-" (substring??)

} else {
address.setStreetNumber(streetInfo[0]);
}
 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the indexOf method to find the location of the "-", then use that location in a call to the substring method to retrieve a new String from the original String.
 
Kat Cur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a beginner.  Is there any video, example code I could see to better understand what you are saying?
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//figure out some code that tells it to store what is after the "-" (substring??)  


Your intuition is correct; use the substring method in the String class.  You can learn about substrings and a whole lot of other methods in the API documentation:

https://docs.oracle.com/javase/10/docs/api/java/lang/String.html

I usually find that URL by googling java 10 string.  Remember that because it will be your friend!
 
Kat Cur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm basically trying to remove any apartment or unit number from a street address before storing it as an array element.
So if the address is 3-175 Mark Street --> it will store only 175 in the setStreetNumber index[0] and not 3-175.  
The addresses are being read from a text file using Scanner.
 
Saloon Keeper
Posts: 8589
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
The use of indexOf() typically looks like this
I suggest reading the Javadocs for the String class and its methods: indexOf() and substring().

Alternatively
 
Kat Cur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the help in referring me to the docs.oracle page … I've read it. I'm a beginner … like infant beginner and have searched the internet high and low for an example of the syntax you would use when using the indexOf an array element and then removing the leading chars up to the index of that char.  I understand that I need to use substring somehow but that is my question … how?
The comments "//do something with … " is what I am asking help with. I don't know what to do other than "use substring". How? Any examples or links to examples so I can visualize it would be greatly appreciated.  I'm feeling frustrated with myself :-(
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a suggestion for testing what the substring method does.  Write a simple program with these statements.  Compile and execute them.


Make some changes to the values of str and ix, compile and execute again to see what happens.
Use the indexOf method to find a substring in the str value and assign the value returned to ix in place of the hardcoded 4.
 
Carey Brown
Saloon Keeper
Posts: 8589
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
Here's the doc for substring(). It even has examples. You say you already know how to use indexOf() to get the index. Substring() uses an index. I fail to see where you're stuck. Edit: Do you find the Javadocs difficult to understand?


public String substring​(int beginIndex)

Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

    "unhappy".substring(2) returns "happy"
    "Harbison".substring(3) returns "bison"
    "emptiness".substring(9) returns "" (an empty string)

Parameters:
   beginIndex - the beginning index, inclusive.
Returns:
   the specified substring.
Throws:
   IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kat Cur wrote:I appreciate the help in referring me to the docs.oracle page … I've read it. I'm a beginner … like infant beginner and have searched the internet high and low for an example of the syntax you would use when using the indexOf an array element and then removing the leading chars up to the index of that char.  I understand that I need to use substring somehow but that is my question … how?
The comments "//do something with … " is what I am asking help with. I don't know what to do other than "use substring". How? Any examples or links to examples so I can visualize it would be greatly appreciated.  I'm feeling frustrated with myself :-(


I understand your frustration, but this is something that you will have to do a lot during your learning about Java.  Try things out.  Experiment.  Read the documentation.  

Just googling java substring brought up an abundance of pages that explain and demonstrate substrings.
 
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kat, I'm guessing that you're thinking "Okay, okay, so there's a substring method. But what substring do I need?"

So here's what I do, because even though I'm a long-time programmer I still tend to get it wrong. The substring method... well, it's easy to get an off-by-one error if you're not paying attention. So let's take your example:



You want to extract "175", and you're going to do that by getting the text between the dash and the next space after that. (Right?) So first find the location of the dash using the indexOf method. The version which starts at the beginning of the string, that is. Assign the result to a variable (you could call the variable "dashLocation"). You should get 1, because string indexes start at zero.



Now use the other version of indexOf to find the location of the first space after that dash, and assign it to another variable (you could call it "spaceLocation"). You should get 5.



Okay so far? Now the last step is to find the substring between those two locations. You're going to use the two variables you created already, only you might have to add or subtract 1 from them so that you end up with the string from positions 2 to 4. I always have to look up the method description in the API because I never remember the fine details, so I don't know exactly what code you're going to end up with.

So give that a try and see where you get. We're still here if you get stuck, but tell us about your progress.
 
Kat Cur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The one line of code I needed instead of an if/else statement(thanks to a java programming friend.

address.setStreetNumber(streetInfo[0].substring(streetInfo[0].indexOf("-")+1));
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, you got a line of Java that will work in some situations.  Next question: do you understand it?  If not, when it breaks you won't be able to fix it.
 
Paul Clapham
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kat Cur wrote:The one line of code I needed instead of an if/else statement...



You certainly didn't need if/else to do the thing you wanted to do. If you'd mentioned that you had that idea, anybody here would have told you that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic