问题描述
我想使用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
打印语句,这样它会显示我会得到的错误,但什么也没输出。
代码:
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!");
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)