如何在java中从命令行按姓氏排序名称

时间:2017-02-22 20:36:51

标签: java sorting

我正在尝试编写一个程序,根据它的名字对名称进行排序,在"排序"条件适用。输入将从命令行提供。我已经开始这样做了,但不知道如何让它发挥作用。

输入:sort" john main" "抢班" " bob ram"

输出:class,rob main,john ram,bob

public static void main(String[] args)
{
    String[] args_name = new String[args.length-1];
    System.arraycopy(args, 1, args_name, 0, args.length-1);

    if(args[0].equals("sort"))
    {
        String firstName = name.substring(args_name,name.indexOf(" "));
        String lastName = name.substring(name.indexOf(" ")+1);
        String slst = lastName + ", " + firstName;

        Arrays.sort(slst);

        for (String s: slst) System.out.print(s);
    }
}

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么这是解决这个问题的代码

public static void shortName(String shortName){

    System.out.println("please enter yout name : ");
    Scanner scanner = new Scanner(System.in);
    String fullName = scanner.nextLine();

    if( shortName.equals("sort")){
        String [] nameArr = fullName.split(" ");
        int lastNameIndex = nameArr.length - 1;
        String name = nameArr[0] +" "+ nameArr[lastNameIndex];
        System.out.println("welcome : "+name);
    }
    else {
        System.out.println("welcome : "+fullName);

    }

}
public static void main(String[] args) {

    shortName("sort");
}

答案 1 :(得分:0)

尝试使用比较器对输入进行排序。为简单起见,我删除了 args 相关代码。您仍需要正确地重新添加它。

必需的进口

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

名称

class Name{
    public static final String FULL_NAME_DELIMITER = " ";
    public static final String PRINT_DELIMITER = ", ";

    private String firstName;
    private String lastName;

    public Name(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public static String[] getName(String fullName){
        return fullName.split(FULL_NAME_DELIMITER);
    }

    public String getLastName(){
        return this.lastName;
    }

    @Override
    public String toString(){
        return String.join(PRINT_DELIMITER , this.lastName, this.firstName);
    }

    public void print(){
        System.out.println(this);
    }
}

比较器

class NameComparator implements Comparator<Name>{
    @Override
    public int compare(Name a, Name b) {
        return a.getLastName().compareTo(b.getLastName());  // ASC: a to b or DESC: b to a
    }
}

<强>用法

public class Main {
    // Holds the sorted names
    private static List<Name> names = new ArrayList<>();

    public static void main(String[] args) {
        // The input names
        String[] input = new String[]{
                "john main",
                "rob class",
                "bob ram"
        };

        // Prepare list for sorting
        String firstName;
        String lastName;
        String[] completeName;

        for (String fullName : input) {
            completeName = Name.getName(fullName);
            firstName = completeName[0];
            lastName = completeName[1];
            names.add(new Name(firstName, lastName));
        }

        // Actually sort
        names.sort(new NameComparator());

        // Print
        names.forEach(System.out::println);
    }
}

修改

要使用命令行中的输入, args 参数就是您要使用的参数。

使用命令行输入

public class Main {
    // Holds the sorted names
    private static List<Name> names = new ArrayList<>();

    public static void main(String[] args) {
        // The method (in case something else than "sort" is desired)
        String method = args[0];

        // Check for valid method
        if(!method.equals("sort")) return;

        // The input names
        String[] input = Arrays.copyOfRange(args, 1, args.length);

        // Prepare list for sorting
        String firstName;
        String lastName;
        String[] completeName;

        for (String fullName : input) {
            completeName = Name.getName(fullName);
            firstName = completeName[0];
            lastName = completeName[1];
            names.add(new Name(firstName, lastName));
        }

        // Actually sort
        names.sort(new NameComparator());

        // Print
        names.forEach(System.out::println);
    }
}

编辑2

要使用名字而不是姓氏,只需更改比较器。

class NameComparator implements Comparator<Name>{
    @Override
    public int compare(Name a, Name b) {
        return a.getFirstName().compareTo(b.getFirstName());    // ASC: a to b or DESC: b to a
    }
}

当然,您还需要调整 Name 类,方法是添加:

public String getFirstName(){
    return this.firstName;
}