如何从全名中提取中间名?

时间:2011-11-04 08:47:00

标签: java

我的程序应该提取全名的中间名。我有另一种方法,我不能在我的主方法中正确调用它。如果代码在main方法中,则代码正在工作,但在其他方法中不起作用;我假设问题是在调用方法时。我正在使用Eclipse。

import java.util.Scanner;
public class MiddleNames {
  public static void main(String[] args) {
    getMiddleName(" ");
    System.out.println();
  }

  public static String getMiddleName(String middleName) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your full name: ");
    String fullName = input.nextLine();
    int firstSpace = fullName.indexOf(" ");
    int lastSpace = fullName.lastIndexOf(" ");
    middleName = fullName.substring(firstSpace,lastSpace);
    return middleName;  
  } 
}

7 个答案:

答案 0 :(得分:2)

您的问题是,在您的java中,您不应该将任何内容传递给方法。试试这个:

public static void main(String[] args) {
    String middleName = getMiddleName();
    System.out.println(middleName);
}

public static String getMiddleName() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your full name: ");
    String fullName = input.nextLine();
    int firstSpace = fullName.indexOf(" ");
    int lastSpace = fullName.lastIndexOf(" ");
    String middleName = fullName.substring(firstSpace,lastSpace);
    return middleName;  
}

实际上,我会使用split来提取名称部分:

String[] names = fullName.split(" ");
return names[1]; // fyi names[0] is the first name, names[2] is the last name

或简单地说:

return fullName.split(" ")[1];

答案 1 :(得分:2)

在我看来,您将main方法中的所有代码复制粘贴到新方法,而不是提取功能本身。

这是一个开始:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your full name: ");
    String fullName = input.nextLine();

    getMiddleName(" ");      <----- Here you should send your string instead of an empty string.
    System.out.println();    <----- You want to print the name returned by the methodcall from the previous line. This line will only print an empty line now.
}
public static String getMiddleName(String middleName) {    <----- The string you get is named "middleName" here
    int firstSpace = fullName.indexOf(" ");                <----- And here it's called fullName.
    int lastSpace = fullName.lastIndexOf(" ");             <----- Here too
    middleName = fullName.substring(firstSpace,lastSpace); <----- And here..
    return middleName;  
} 

答案 2 :(得分:0)

如果不使用参数,为什么要读取参数? 试试这个

import java.util.Scanner;
public class MiddleNames {
    public static void main(String[] args) {
        System.out.println(getMiddleName());
    }
        public static String getMiddleName()
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter your full name: ");
            String fullName = input.nextLine();
            int firstSpace = fullName.indexOf(" ");
            int lastSpace = fullName.lastIndexOf(" ");
            String middleName = fullName.substring(firstSpace,lastSpace);
            return middleName;  
        } 
}

答案 3 :(得分:0)

试试这个:

public static void main(String[] args) {
    String yourMiddleName = getMiddleName(" ");
    System.out.println(yourMiddleName);
}

答案 4 :(得分:0)

static放在getMiddleName的定义上会强制您通过static方式MiddleNames.getMiddleName()使用该方法,或者在其他static中使用该方法方法。它适用于main,因为它已经是static方法。

解决方案:

  • 更改为static您的其他方法
  • 从getMiddleName
  • 的声明中删除static
  • 像这样MiddleNames.getMiddleName()
  • 调用getMiddleName

答案 5 :(得分:0)

问题是你不处理输入中只有一个空格的情况。在这种情况下,firstSpace和lastSpace是相同的,结果将是一个emty字符串

答案 6 :(得分:0)

String fullName = "FirstName MiddleName LastName"; 
String[] s = fullName.split(" ");
    
// System.out.println("Get the length of the array middle position "+ s.length / 2);
    
String middleName = s[s.length/2];
System.out.println(middleName);