Java 根据姓氏的字母顺序对用户输入的姓名进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20028671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting names entered by the user in alphabetical order according to the last name
提问by DarkIceDragon
I have completed most of the code by myself (with the help of a bit of Googling) but I have run into an unexpected problem. First-off, I have to sort a user entered list of names in aplhabetical order of their last names using selection sort. Here is my code:
我自己完成了大部分代码(在谷歌搜索的帮助下),但我遇到了一个意想不到的问题。首先,我必须使用选择排序按姓氏的字母顺序对用户输入的姓名列表进行排序。这是我的代码:
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;
}
}
The problem (I think) is arising when I'm taking the names from the user, specifically, this part:
当我从用户那里获取名称时,问题(我认为)出现了,特别是这部分:
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]; //Array to store the names in.
for (int i = 0; i<ar.length; i++)
{
System.out.println("Please enter the name: ");
ar[i]= in.nextLine();
}
The output on the terminal window of BlueJ shows up as
BlueJ 终端窗口上的输出显示为
Name_Sort.main({ });
Enter the number of names you wish to enter: 5
Please enter the name:
Please enter the name:
That is not what it's supposed to display. What could I be doing wrong? I've pondered over it for a while, but nothing comes to mind.
那不是它应该显示的内容。我可能做错了什么?我想了一会儿,但没有想到。
And, even if I do move forward and enter a few names despite the error above, I get another error in this part of my code here:
而且,即使我确实向前推进并输入了一些名字,尽管有上述错误,但我在这部分代码中遇到了另一个错误:
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;
}
the error is :
错误是:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (injava.lang.String)
Does this mean that the white-space character " "
is not present? But why?
这是否意味着" "
不存在空白字符?但为什么?
This is a screenshot of the terminal window: http://imgur.com/l7yf7Xn
这是终端窗口的截图:http: //imgur.com/l7yf7Xn
The thing is, if I just initialize the array with the names first (and not take any input from the user) the codes runs fine and produces the desired result. Any help please?
问题是,如果我只是先用名称初始化数组(而不是从用户那里获取任何输入),则代码运行良好并产生所需的结果。请问有什么帮助吗?
Also, since I know some people here are very particular about this, yes, this is a homework assignment, yes, I did do all of the code by myself, I googled on how to sort the names in alphabetical order as I couldn't exactly code out the original idea I had.
Which was comparing the ASCII values of each character of two surnames to see which should come first. Like: if((int) surname1.charAt(0)>(int) surname2.charAt(0))
then surname2 should come before surname1, else if they both have the same first character, take the second character and so on.
另外,因为我知道这里有些人对此非常挑剔,是的,这是一项家庭作业,是的,我确实自己完成了所有代码,我在 google 上搜索了如何按字母顺序对名称进行排序,因为我不能准确地编码出我最初的想法。这是比较两个姓氏的每个字符的 ASCII 值,看看哪个应该先出现。如:if((int) surname1.charAt(0)>(int) surname2.charAt(0))
那么 surname2 应该在 surname1 之前,否则如果它们的第一个字符相同,则取第二个字符,依此类推。
Thanks for taking the time to read this.
感谢您抽时间阅读。
采纳答案by Sage
The problem is with the in.nextInt()
command it only reads the int value. So when you continue reading with in.nextLine() you receive the "\n"
Enter key. So to get around this you will have to add an extra in.nextLine()
before going into the loop. Or, use another scanner
.
问题在于in.nextInt()
它只读取 int 值的命令。因此,当您使用 in.nextLine() 继续阅读时,您会收到"\n"
Enter 键。所以为了解决这个问题,你必须in.nextLine()
在进入循环之前添加一个额外的。或者,使用另一个scanner
.
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();
}