Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据姓氏按字母顺序对用户输入的姓名进行排序_Java_Sorting_Indexoutofboundsexception_Selection Sort - Fatal编程技术网

Java 根据姓氏按字母顺序对用户输入的姓名进行排序

Java 根据姓氏按字母顺序对用户输入的姓名进行排序,java,sorting,indexoutofboundsexception,selection-sort,Java,Sorting,Indexoutofboundsexception,Selection Sort,我自己完成了大部分代码(在谷歌的帮助下),但遇到了意想不到的问题。首先,我必须使用选择排序对用户输入的姓名列表按姓氏的字母顺序进行排序。这是我的密码: import java.util.*; class Name_Sort { public static void main (String args[]) { Scanner in = new Scanner (System.in); System.out.print ("Enter the

我自己完成了大部分代码(在谷歌的帮助下),但遇到了意想不到的问题。首先,我必须使用选择排序对用户输入的姓名列表按姓氏的字母顺序进行排序。这是我的密码:

    import java.util.*;
class Name_Sort
{
    public static void main (String args[])
    {
        Scanner in = new Scanner (System.in);
        System.out.print ("Enter the number of names you wish to enter: ");
        int n = in.nextInt();
        String ar[] = new String [n];
        for (int i = 0; i<ar.length; i++)
        {
            System.out.print("Please enter the name: ");
            ar[i]= in.nextLine();
        }
        String temp;
        for (int b = 0; b<n; b++)
        {
            for (int j=b+1; j<n; j++)
            {
                if ((compareLastNames(ar[b], ar[j]))>0)
                {
                    temp = ar[b];
                    ar[b] = ar[j];
                    ar[j] = temp;
                }
            }
        }
        System.out.println ("The names sorted in alphabetical order are: ");
        for (int a = 0; a<n; a++)
            System.out.print (ar[a]+"\t");
    }

    private static int compareLastNames(String a, String b) 
    {
        int index_a = a.lastIndexOf(" ");
        String surname_a = a.substring(index_a);
        int index_b = b.lastIndexOf(" ");
        String surname_b = b.substring(index_b);
        int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
        return lastNameCmp;
    }
}
这不是它应该展示的。我可能做错了什么?我考虑了一会儿,但什么也没想到

而且,尽管存在上述错误,但即使我继续前进并输入了一些名称,我在代码的这一部分中也会遇到另一个错误:

private static int compareLastNames(String a, String b) 
{
    int index_a = a.lastIndexOf(" ");
    String surname_a = a.substring(index_a);// This is the line the compiler highlights.
    int index_b = b.lastIndexOf(" ");
    String surname_b = b.substring(index_b);
    int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
    return lastNameCmp;
}
错误是:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (injava.lang.String)
这是否意味着空白字符
不存在?但是为什么呢

这是终端窗口的屏幕截图:

问题是,如果我只是先用名称初始化数组(而不从用户那里获取任何输入),代码运行良好,并生成所需的结果。需要帮忙吗

另外,因为我知道这里的一些人对此非常挑剔,是的,这是一个家庭作业,是的,我自己做了所有的代码,我在谷歌上搜索如何按字母顺序排序,因为我无法准确地写出我最初的想法。 这是比较两个姓氏的每个字符的ASCII值,看哪个应该先出现。比如:
如果((int)姓氏1.charAt(0)>(int)姓氏2.charAt(0))
那么姓氏2应该在姓氏1之前,否则如果它们都有相同的第一个字符,则取第二个字符,依此类推


感谢您花时间阅读此内容。

问题在于.nextInt()中的
命令只读取int值。因此,当您继续使用in.nextLine()阅读时,您将收到
“\n”
Enter键。因此,要解决这个问题,您必须在进入循环之前在.nextLine()
中添加一个额外的
。或者,使用另一个
扫描仪

    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.

    in.nextLine(); // < --- an extra next Line

    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }
int n=in.nextInt();
字符串ar[]=新字符串[n]//数组中存储名称。
in.nextLine();//<--额外的下一行

对于(int i=0;i而言,问题在于
in.nextLine()
命令只读取int值。因此,当您继续使用in.nextLine()读取时,您会收到
“\n”
回车键。因此,要解决这个问题,您必须在进入循环之前在.nextLine()
中添加一个额外的
。或者,使用另一个
扫描仪

    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.

    in.nextLine(); // < --- an extra next Line

    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }
int n=in.nextInt();
String ar[]=新字符串[n];//用于存储名称的数组。
in.nextLine();//<---额外的下一行

对于(int i=0;i而言,问题在于
in.nextLine()
命令只读取int值。因此,当您继续使用in.nextLine()读取时,您会收到
“\n”
回车键。因此,要解决这个问题,您必须在进入循环之前在.nextLine()
中添加一个额外的
。或者,使用另一个
扫描仪

    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.

    in.nextLine(); // < --- an extra next Line

    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }
int n=in.nextInt();
String ar[]=新字符串[n];//用于存储名称的数组。
in.nextLine();//<---额外的下一行

对于(int i=0;i而言,问题在于
in.nextLine()
命令只读取int值。因此,当您继续使用in.nextLine()读取时,您会收到
“\n”
回车键。因此,要解决这个问题,您必须在进入循环之前在.nextLine()
中添加一个额外的
。或者,使用另一个
扫描仪

    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.

    in.nextLine(); // < --- an extra next Line

    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }
int n=in.nextInt();
String ar[]=新字符串[n];//用于存储名称的数组。
in.nextLine();//<---额外的下一行

对于(int i=0;iOr,请使用另一个
扫描仪
。感谢您的帮助!另外,您的答案中有一点输入错误:
in.nexrLine()
。但非常感谢!或者使用另一个
扫描仪
。感谢您的帮助!另外,您的答案中有一点输入错误:
in.nexrLine()
。但是非常感谢!或者使用另一台
扫描仪
。谢谢你的帮助!还有,在你的答案中有一点输入错误:
在.nexrLine()
。但是非常感谢!或者使用另一台
扫描仪
。谢谢你的帮助!还有,在你的答案中有一点输入错误:
在.nexrLine()
。但是非常感谢!“根据姓氏-JAVA按字母顺序排序用户输入的姓名”无需对我们大喊大叫,因此应为“根据姓氏-JAVA按字母顺序排序用户输入的姓名”。然后,无需在问题标题中添加主标记,因此应为”根据姓氏按字母顺序排列用户输入的姓名。“如果你认为我在叫喊,我很抱歉。我想通过将Java放在问题标题中,人们不必点击我的问题来查看它使用的是哪种语言。”根据姓氏按字母顺序排列用户输入的姓名-Java“没有必要对我们大喊大叫,所以应该是“根据姓氏按字母顺序排列用户输入的名称-Java”。然后,同样,没有必要在问题标题中添加主标记,所以应该是“根据姓氏按字母顺序排列用户输入的名称”。如果你认为我在叫喊,我很抱歉。我想,通过将Java放在问题标题中,人们就不必点击我的问题来查看它使用的是哪种语言。“按照姓氏的字母顺序排列用户输入的名称-Java”没有必要对我们叫喊,所以应该是这样的“根据姓氏-Java按字母顺序排序用户输入的姓名”。然后,同样,不需要在问题标题中添加主标记,因此它应该是“根据姓氏按字母顺序排序用户输入的姓名”“。如果你认为我在大喊大叫,我很抱歉。我想通过将Java放在问题标题中,人们就不必点击我的问题来查看它使用的是哪种语言。”根据姓氏按字母顺序排列用户输入的名称-Java“我在那里