Reputation: 917
I wrote a program that prompts a user to input his/her full name First , middle , last. I'm attempting to break that name down into three separate pieces using the substring method by locating each white space in the string. Once each white space is found the following portion of the name is stored in a new string variable.
The issue I'm having is that the middle name is storing both middle and last because the counter isn't stopping correctly as you will see in my below code so my question is how can I fix this.
Please note, I do not want to split the string, I do not want to use a StringTokenizer, I do not want to use Arrays, I already know how to do it that way. All I need to know is how to fix my counter, what I am trying to do will work this way I'm not looking for a new way to do this; I'm only working with substring and charAt.
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
String name = "",
firstName = "",
middleName = "",
lastName = "";
Scanner in = new Scanner(System.in);
System.out.print("Please enter your name 'First Middle Last': ");
name = in.nextLine();
int i = 0;
while(i < name.length())
{
if(name.charAt(i) == ' ')
{
firstName = name.substring(0, i); // Starts at 0 ends at first occurence of ' '
/* middleName = name.substring(i + 1, i); Should start at i + 1,
or one postion after the previously located white space and
then end at the next occurence of ' '
^ The above does not work, and this is where the issue is
happening, how ever if I do the following */
middleName = name.substring(i + 1, name.length());
/* This does work but because the endIndex is name.length() it
runs the length of name and grabs both the middle and last name.*/
i = name.length();
}
++i;
}
System.out.print("\nDisplayed with last name first: " + "\nLast Name: " + lastName + "\nFisrt Name: " + firstName + "\nMiddle Name: " + middleName);
}
}
The output this produces looks like this
Please enter your name 'First Middle Last': First Middle Last
Displayed with last name first:
Last Name:
First Name: First
Middle Name: Middle Last
As you can see first Name is displayed correctly. Middle name is not because it included "Last" and not just "middle"
Upvotes: 0
Views: 735
Reputation: 747
do it in the following way: no need to run the while loop.
firstName=name.substring(0,name.indexOf(' '));
middleName=name.substring(name.indexOf(' ')+1,name.lastIndexOf(' '));
lastName=name.substring(name.lastIndexOf(' ')+1,name.length());
Upvotes: 0
Reputation: 11
var mode = 0;
var last = 0;
for(i=0;i<name.length();i++)
{
if(name.charAt(i)==' '||i==(name.length()-1))
{
mode++;
var text = name.substring(last, i-last);
last = i+1;
if(mode==1) { firstName = text; }
else if(mode==2) { middleName = text; }
else if(mode==3) { lastName = text; break; }
}
}
Upvotes: 0
Reputation: 6284
Instead of running through a loop with charAt
, if you really want to use substring
you could just do:
int firstSpace = fullName.indexOf(' ');
String firstName = fullName.substring(0, firstSpace);
int secondSpace = fullName.indexOf(' ', firstSpace+1);
String middleName = fullName.substring(firstSpace+1, secondSpace);
String lastName = fullName.substring(secondSpace+1);
Upvotes: 0
Reputation: 4825
You could use indexOf(' ')
and lastIndexOf(' ')
to find first space and last space
firstname
lastname
middlename
Pseudocode:
firstName = name.substring(0, name.indexOf(' '));
middleName = name.substring(name.indexOf(' ') + 1, name.lastIndexOf(' '));
lastName = name.substring(name.lastIndexOf(' ') + 1);
Upvotes: 3
Reputation: 13199
Instead of substring
you can use split
function:
String string = "Peter Ralph Joseph"; //Here the string
String[] parts = string.split(" "); //Here the string it is divide taking as reference the space
System.out.println(parts[0]); //Peter
System.out.println(parts[1]); //Ralph
System.out.println(parts[2]); //Joseph
In my opinion, it is easier and faster to do it with split
function.
Upvotes: 2