4 Replies - 4096 Views - Last Post: 11 March 2012 - 02:26 PM Rate Topic: -----

#1 jag4205  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 04-March 12

String first and last name

Posted 11 March 2012 - 11:39 AM

Write a program that takes a string of someone's name and prints it out last name first. Your program must use pointers, not array subscripts. You may use the available string manipulation functions if you find an opportunity.
Example:
"George Washington"
"Washington, George"

I need help on how to start this off
Is This A Good Question/Topic? 0
  • +

Replies To: String first and last name

#2 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: String first and last name

Posted 11 March 2012 - 11:49 AM

In short, we're not here to do you work for you, first try something.

If you're in a Java Programming class then chances are it's only using what you've done already. If you're reading a book, go back and read parts that confused you over again.


If that doesn't work, I'd (and a lot of other people on the forum) would be happy to help you start off.


Thank you!
~Crockeo
Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: String first and last name

Posted 11 March 2012 - 11:49 AM

Use the .indexOf method in the String class to search for any spaces in the name.

When you have this index of the space, use the .subString method to get the the portion of the String before the space index (a.k.a the first name). You can then use the same concepts to get the last name (subString from the index to the end of the String).

Once you have both names in two strings or you can print out/whatever.

This post has been edited by Ryano121: 11 March 2012 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 jag4205  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 04-March 12

Re: String first and last name

Posted 11 March 2012 - 12:14 PM

public class NameBreak {
public static String convertName(String name) 
{
	String lastname = "";
	String firstname = "";
	for(int i = 0; i < name.length(); i++)
	{
		if(name.charAt(i) == ' ')
		{
			lastname = name.substring(i + 1);
			firstname = name.substring(0, i);
			
		}
	}
	return lastname + ", " + firstname;
}
    

}


This is what i have so far. Any suggestions

This post has been edited by macosxnerd101: 11 March 2012 - 03:31 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String first and last name

Posted 11 March 2012 - 02:26 PM

would be even easier with the String split() method
split on " " and just print the produced array in reverse order adding the "," betweeen the two
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1