Question:
Java Programming Question Relating to the String Class?
?
2010-04-07 07:36:00 UTC
Here's the problem I need to code for:

A String variable, fullName, contains a name in one of two formats:
last name,  first name (comma followed by a blank), or
first name  last name (single blank)

Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables.

Now here's the code I wrote:

int location1 = 0;
int location2 = 0;

location1 = fullName.indexOf(',');
location2 = fullName.indexOf(' ');

if( location1 > 0 )
{
lastName = fullName.substring(0, location1);
firstName = fullName.substring((location2 + 1), (fullName.length() - 1) );
}

else
{
firstName = fullName.substring(0,location2);
lastName = fullName.substring((location2 + 1 ), (fullName.length() - 1) );
}

Shouldn't this work? If someone could tell me what I'm doing wrong that would be greatly appreciated. This one has been giving me problems for quite some time -_-
Three answers:
JavaIntermediate
2010-04-07 08:33:07 UTC
It's your lasts index your subtracting one and you shouldn't. You have



firstName = fullName.substring((location2 + 1), (fullName.length() - 1) );



and



lastName = fullName.substring((location2 + 1 ), (fullName.length() - 1) );



Should be



firstName = fullName.substring((location2 + 1), (fullName.length()) );



and

lastName = fullName.substring((location2 + 1 ), (fullName.length() ) );



for:



int location1 = 0;

int location2 = 0;



location1 = fullName.indexOf(',');

location2 = fullName.indexOf(' ');



if( location1 > 0 )

{

lastName = fullName.substring(0, location1);

firstName = fullName.substring((location2 + 1), (fullName.length()) );

}



else

{

firstName = fullName.substring(0,location2);

lastName = fullName.substring((location2 + 1 ), (fullName.length()) );

}
?
2016-10-15 06:19:33 UTC
confident, a char isn't a variable-length merchandise. it is likewise a numeric variety, and the apostrophes (or inverted commas) utilized in a char consistent basically ask Java to generate the style that corresponds to the quoted character in accordance the the Unicode code series. in case you desire to initialize a char variable to a minimum of a few thing that's not a clean area or the different value, attempt: taxcode=0; and dispense with the apostrophes altogether. For greater char vs. String vs. int exciting, attempt: public classification CharTest { public static void important(String[] args) { char 0 = 40 8; int forty_eight = '0'; gadget.out.println("0 = " + 0); gadget.out.println("forty_eight = " + forty_eight); gadget.out.println("0+a million = " + (0 + a million)); gadget.out.println("forty_eight+a million = " + (forty_eight + a million)); } } /* classification attempt */
deonejuan
2010-04-07 09:45:51 UTC
public class NameParts {



public static void main(String[] args) {

new NameParts().init();

}



private void init() {

String [] fullName = {

"Joan Bastisa",

"Escobar, Pablo",

"Simeon de la Toya Benavillas" };

for (int i = 0; i < fullName.length; i++) {

proofOfConcept( nameParts( fullName[ i ] ) );



}

}



private String[] nameParts(String name) {

String[] parts = new String[2];

if (name.indexOf(",") > -1) {

String[] n = name.split(",");

parts[1] = n[0];

parts[0] = n[1];

} else if (name.length() - name.replaceAll(" ", "").length() == 1) {

parts = name.split(" ");

} else {

int cloven = name.indexOf(" ");

StringBuilder sb = new StringBuilder( name );

sb.setCharAt(cloven, '%');

parts = sb.toString().split("%");

}



for (int i = 0; i < parts.length; i++) {

parts[i] = parts[i].trim();

}



return parts;

}

private void proofOfConcept( String [] s ) {

for (int i = 0; i < s.length; i++) {

System.out.printf("The person's %s name is: %s%n",

(i > 0 ? "last" : "first"), s[i] );



}

System.out.println();

}

}

// substring( 0, xxx); is just too wicked when using logic.

// String.split( RegEx ); is the power tool

// I don't have for the situation of 'Mary Jane Val Juan'; 2 first names with 2 last surnames


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...