2016-02-27 32 views
0

问题我写了一个程序,提示用户输入他/她的全名首先,中,最后一次。我试图通过在字符串中定位每个空格来使用子字符串方法将该名称分解为三个单独的部分。一旦找到每个空格,名称的以下部分将存储在新的字符串变量中。与Java子方法

我遇到的问题是,中间名是存储中间和最后一个,因为计数器没有正确停止,因为你会看到我的下面的代码,所以我的问题是我该如何解决这个问题。

请注意,我不想分割字符串,我不想使用StringTokenizer的,我不想使用数组,我已经知道如何做到这一点的方式。所有我需要知道的是如何修复我的柜台,我试图做会以这种方式工作,我不是在寻找一种新的方式来做到这一点;我只使用substring和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); 

    } 

} 

这会产生输出看起来像这样

Please enter your name 'First Middle Last': First Middle Last 

Displayed with last name first: 
Last Name: 
First Name: First 
Middle Name: Middle Last 

正如你所看到的第一个名字是正确显示。 中间名是不是因为它包括了“最后”,而不只是“中等”

+0

请考虑[谜底使用比什么是问其他不同的技术(http://meta.stackoverflow.com/questions/268190/answers-that-use-a-different-technology值-than - 什么 - 是 - 问/ 268228#268228)。 – ChiefTwoPencils

回答

3

你可以使用indexOf(' ')lastIndexOf(' ')找到第一空间和最后空间

  • 前一日的空间一切都是firstname
  • 一切的最后一个空格之后lastname
  • 中间剩下的就是middlename

伪代码:

firstName = name.substring(0, name.indexOf(' ')); 
middleName = name.substring(name.indexOf(' ') + 1, name.lastIndexOf(' ')); 
lastName = name.substring(name.lastIndexOf(' ') + 1); 
+0

感谢您的回复,当您最初提出您的评论时,我直接转到了java文档,并得出结论,您是正确的!感谢您的回复,并感谢您作为唯一一个真正决定向我提供所需信息的人,而不是我特别说的我不需要的信息! – Afflicted

+0

没问题,很乐意帮忙 – radoh

2

相反的substring您可以使用split功能:

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 

在我看来,它更容易和更快地做到这一点split功能。

+0

你错过了一点,它与效率无关,或者更容易。我使用子串我不是我说的使用拆分,也没有stringTokenizer等。我问了一个简单的问题。如何解决反笑 – Afflicted

+0

@Afflicted哦,对不起。然后你可以使用'的indexOf(“‘);'采取参照第一空间和'lastIndexOf(’”);'找到最后空间@radoh他回答说。无论如何,我认为你应该使用其他函数来获得这种行为而不是'substring'。 –

0

不是通过循环与charAt运行,如果你真的想使用substring你可能只是这样做的:

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); 
0
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; } 
    } 
} 
0

做以下列方式: 无需运行while循环。

firstName=name.substring(0,name.indexOf(' ')); 
middleName=name.substring(name.indexOf(' ')+1,name.lastIndexOf(' ')); 
lastName=name.substring(name.lastIndexOf(' ')+1,name.length());