Java 如何使用Arrays.sort()方法对学生姓名进行排序?
我想使用Java 如何使用Arrays.sort()方法对学生姓名进行排序?,java,arrays,sorting,for-loop,text-files,Java,Arrays,Sorting,For Loop,Text Files,我想使用Arrays.sort()方法按字母顺序对学生姓名进行排序,并将其写入名为StudentNames的新文本文件中。我试着这么做,但它会给我一个错误。我不知道我是否正确地使用了它或将其放置在正确的位置 样本输入: Select Your Class Size! A 6x5 Classroom or a 3X10 classroom? Enter '6x5' or '3x10' please! 6x5 Ok, so you have selected 6x5 Your classro
Arrays.sort()
方法按字母顺序对学生姓名进行排序,并将其写入名为StudentNames
的新文本文件中。我试着这么做,但它会给我一个错误。我不知道我是否正确地使用了它或将其放置在正确的位置
样本输入:
Select Your Class Size!
A 6x5 Classroom or a 3X10 classroom?
Enter '6x5' or '3x10' please!
6x5
Ok, so you have selected 6x5
Your classroom size looks like this:
XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX
Now Enter The Number Of Students!
4
Enter the names of the 4 students!
Hussain
User
Jacob
Bob
The Student Names Have Been Sorted In An Alphabetical Order
The Names And Seat Location Of The Student Are As Follows:
Hussain Seat Location: (1)(1)
User Seat Location: (1)(2)
Jacob Seat Location: (1)(3)
Bob Seat Location: (1)(4)
样本输出:
Select Your Class Size!
A 6x5 Classroom or a 3X10 classroom?
Enter '6x5' or '3x10' please!
6x5
Ok, so you have selected 6x5
Your classroom size looks like this:
XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX
Now Enter The Number Of Students!
4
Enter the names of the 4 students!
Hussain
User
Jacob
Bob
The Student Names Have Been Sorted In An Alphabetical Order
The Names And Seat Location Of The Student Are As Follows:
Bob Seat Location: (1)(1)
Hussain Seat Location: (1)(2)
Jacob Seat Location: (1)(3)
User Seat Location: (1)(4)
我实际得到的输出:
Select Your Class Size!
A 6x5 Classroom or a 3X10 classroom?
Enter '6x5' or '3x10' please!
6x5
Ok, so you have selected 6x5
Your classroom size looks like this:
XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX
Now Enter The Number Of Students!
4
Enter the names of the 4 students!
Hussain
The Student Names Have Been Sorted In An Alphabetical Order
The Names And Seat Location Of The Student Are As Follows:
它没有输出任何东西,我对我的catch
print语句进行了注释,这样它就可以显示我将得到的错误,但它没有输出任何东西
代码:
import java.util.*;
导入java.io.*;
//创建一个类和方法
公共班机{
公共静态void main(字符串[]args){
//清除屏幕
系统输出打印(“\033[H\033[2J”);
System.out.flush();
//创建扫描仪对象
扫描仪inp=新扫描仪(System.in);
//创建打印语句
System.out.println(“选择您的班级大小!\n”);
System.out.println(“6x5教室或3X10教室?\n请输入'6x5'或'3X10'!\n”);
字符串Class1=“6x5”;
字符串Class2=“3x10”;
双输入[]=新双输入[1];
字符串selectClassSize=inp.next();
int indexOfx=selectClassSize.indexOf('x');
int xcount=0;
布尔containsx=indexOfx==0 | | indexOfx==(选择classsize.length()-2);
if(containsx){
input[xcount]=Double.parseDouble(选择classsize.replace(“x”,即“));
System.out.println(“\nOk,所以您选择了“+Class1”);
System.out.println(“您的教室大小如下:\n”);
int行=6;
int列=5;
int classSize[][]=新的int[行][列];
对于(int i=0;i=6){
列++;
行=0;
}
}
否则{
如果(行>=3){
列++;
行=0;
}
}
数组。排序(名称);
席位[x]=“(“+Integer.toString(列)+”)”+“(“+Integer.toString(++row)+”)”;
write(Arrays.toString(name)+“Seat Location:”+seats[x]);
换行符();
}
bw.close();
fw.close();
//抓住任何错误
}捕获(例外e){
//System.out.println(“发生错误!”);
}
试一试{
//初始化新对象
FileReader fr=新的FileReader(“学生姓名”);
BufferedReader br=新的BufferedReader(fr);
String line=br.readLine();
//启动while循环以从文件中输出数据
System.out.println(“\n学生姓名已按字母顺序排列”);
System.out.println(“学生的姓名和座位位置如下:\n”);
while(行!=null){
系统输出打印项次(行);
line=br.readLine();
}
br.close();
fr.close();
//抓住任何错误
}捕获(异常e1){
System.out.println(“发生错误!”);
}
}
}
我没有阅读您的所有代码,但您正在使用循环,您首先需要做的一件事是将数组.sort()
放在循环之外-它只需要运行一次
下面是一些示例代码:
String[]names=新字符串[]{“Bob”、“User”、“Hussian”、“Gandalf”};
数组。排序(名称);
System.out.println(Arrays.toString(name).replace(“,”,“,\n”);
输出:
[Bob,
甘道夫,
侯赛因,
用户]
进程已完成,退出代码为0
注意:如果您想忽略按字母大小写排序,您可能还需要将所有内容转换为小写/大写。您当前的输出很好,但更重要的是您得到了什么错误。粘贴它/用它更新此问题。此外,它看起来像是
数组。在粘贴的代码中,sort
不存在。其中案例-不,你没有正确使用它。好的,我会用我更新的@rzwitserroot更新我的帖子,请查看
import java.util.*;
import java.io.*;
// Create a class and method
public class Main {
public static void main(String[] args) {
// Clear the screen
System.out.print("\033[H\033[2J");
System.out.flush();
// Create scanner object
Scanner inp = new Scanner(System.in);
// Create a print statement
System.out.println("Select Your Class Size!\n");
System.out.println("A 6x5 Classroom or a 3X10 classroom?\n Enter '6x5' or '3x10' please!\n");
String Class1 = "6x5";
String Class2 = "3x10";
Double input[] = new Double[1];
String selectClassSize = inp.next();
int indexOfx = selectClassSize.indexOf('x');
int xcount = 0;
boolean containsx = indexOfx == 0 || indexOfx == (selectClassSize.length() - 2);
if (containsx) {
input[xcount] = Double.parseDouble(selectClassSize.replace("x", ""));
System.out.println("\nOk, so you have selected " + Class1);
System.out.println("Your classroom size looks like this:\n");
int rows = 6;
int columns = 5;
int classSize[][] = new int [rows][columns];
for(int i = 0; i < classSize[0].length; i++){
for(int j = 0; j < classSize.length; j++){
System.out.print("X");
}
System.out.println();
}
xcount++;
} else {
System.out.println("\nOk, so you have selected " + Class2);
System.out.println("Your classroom size looks like this:\n");
int rows2 = 3;
int columns2 = 10;
int classSize2[][] = new int [rows2][columns2];
for(int x = 0; x < classSize2[0].length; x++){
for(int y = 0; y < classSize2.length; y++){
System.out.print("X");
}
System.out.println();
}
}
// Create a scanner variable
System.out.println("\nNow Enter The Number Of Students!");
int numOfStudents = inp.nextInt();
// Create a counter variable to count upto the numOfStudents and break the loop
int counter = 0;
System.out.println("\nEnter the names of the " + numOfStudents + " students!\n");
try {
// Initialize the new objects
FileWriter fw = new FileWriter("StudentNames");
BufferedWriter bw = new BufferedWriter(fw);
String[] names = new String[numOfStudents];
String[] seats = new String[numOfStudents];
int row = 0, column = 1;
// Output the first names in the
for (int x = 0; x < numOfStudents; x++) {
names[x] = inp.next();
if(containsx) {
if(row >= 6) {
column++;
row = 0;
}
}
else {
if(row >= 3) {
column++;
row = 0;
}
}
Arrays.sort(names);
seats[x] = "("+Integer.toString(column)+")" + "("+Integer.toString(++row)+")";
bw.write(Arrays.toString(names)+" Seat Location: "+seats[x]);
bw.newLine();
}
bw.close();
fw.close();
// Catch any errors
} catch (Exception e) {
// System.out.println("An Error Occured!");
}
try {
// Initialize the new objects
FileReader fr = new FileReader("StudentNames");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
// Start a while loop to output the data from the file
System.out.println("\nThe Student Names Have Been Sorted In An Alphabetical Order");
System.out.println("The Names And Seat Location Of The Student Are As Follows:\n");
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
// Catch any errors
} catch (Exception e1) {
System.out.println("An Error Occured!");
}
}
}