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

ArrayList: Substring or String split

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to read a list of names (first and last) and print out the last name. I'm trying to figure which way would be easier. Since the last name doesn't begin as a specific character point, I know that I should look for where a " " occurs at each name as I loop through the list, and then print out everything that comes after the " ".
 
author & internet detective
Posts: 41656
883
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like split() better because it is easier to read the resulting code. People can have more than one space in their name, but you have that problem with either appraoch.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, is there any way you could help me get started? I have never done this before and beyond what I've written above, I'm not sure how to approach this.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam

Do you have control over the format of the names being read? If so, how about a character like # to separate the first and last names?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not. This list has been given to me and I cannot modify it. It looks like this.

 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam

Are you not being asked to read the names from a resource such as a file?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, both sections of code that I've posted are part of the same Java file. The part with the list is at the top of the program.
 
Jeanne Boyarsky
author & internet detective
Posts: 41656
883
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:Ok, is there any way you could help me get started? I have never done this before and beyond what I've written above, I'm not sure how to approach this.


Step #1 - loop through the list
Step #2 - call split
Step #3 - print out last name

Which of these steps are you having trouble with? What do you have so far?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble with the split

>
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:I'm having trouble with the split

>



Based on that code your problem seesm to be that you aren't calling the split method. Can you show us the code where you do call it and then explain what the exact problem you are having is.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is all I have to be honest.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String split method example (Java Code Geeks)

Java 7 API for String split method
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, here's what I have. I getting red Xs under "String s" and under the "i" in the same line. I know that this really isn't a string, but I need to find some way to invoke with copy and pasting the entire list.

>
 
Greenhorn
Posts: 7
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably need to replace your inner for loop, following line of code may help you

 
Marshal
Posts: 77926
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I was able to get this working somewhat. From the code above, you see that there is a first and a last name. I'm trying out how to remove the first name and ONLY print the list of last names. Right now, something like this prints to the console. (I know that I need to move my first println() statement. I'll fix that at the end.) I do not know how to get started on this.

The players' last names are: Bryce
Harper

>
 
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
So you want to print the last name to the console but not with the first name ?
You have for-each loop on temp (which is array of names - first name followed by last name)
You normally use for-each loop when you want to iterate all elements of Iterable
If you just want a particular element, you could simply access that element (you know the index)

I think it may worth to spend some minutes to read the java tutorial, especially collections
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I'd just like to print each last name to the console. If I can do this a completely different way if there's any easier way to do this. I do not need to do it the way I'm doing. Please let me know if there's any easier way to do this.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:Yes, I'd just like to print each last name to the console. If I can do this a completely different way if there's any easier way to do this. I do not need to do it the way I'm doing. Please let me know if there's any easier way to do this.


Sam, it's really difficult to help when you don't listen. I believe I already told you in your other thread that:
1. Code will NOT solve your problems for you. You have to think.
2. Trying out solutions at random won't work either. It'll just get you more frustrated.

Raymond has given you the best piece of advice you can get right now (and it would have been mine too) - READ THE TUTORIALS.
If you don't understand something, come back here telling us exactly what it is that you don't follow, and we'll try to help. But while you're just flailing around aimlessly, you will never succeed. Or if you do - by accident - you probably won't understand why you did.

The code you've just posted contains ALL the elements you need for a solution. Now you just need to put your thinking cap on.

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to read those. I was just wondering if there was a different way to do it.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam

When you say a different way, different from what? You have not attempted a solution to your problem of removing the first name from the output. Make an attempt (use the tutorials as a guide) and then post it here. At that point, others can guide you towards an alternative solution if necessary.
reply
    Bookmark Topic Watch Topic
  • New Topic