需要一些幫助來解決這個問題。 我想從data.txt輸入數據到bsorted.txt中,並使用bubbleSort進行排序。將文件中的數據讀入數組,然後將數據輸出到另一個文件中
我需要在數據文件中生成隨機數字和字母,並將它們讀入數組並在排序到另一個文本文件後輸出這些數據。
我收到一個NoSuchElementException
:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at SortingExample.main(SortingExample.java:257)
代碼:
class dataFile {
public static void dataFile() {
try {
PrintWriter writer = new PrintWriter("data.txt");
String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
String t = "";
for (int i = 0; i < 2000; i++) {
t += alphaNumerics.charAt((int) (Math.random() * alphaNumerics.length()));
}
writer.println(t);
writer.close();
}
catch(Exception ex) {
System.out.println("Error accessing file.");
System.out.println("Reason was: "+ex.getMessage());
ex.printStackTrace();
}
}
}
class SortingExample {
public static void main(String[] args) throws IOException {
dataFile.dataFile();
Scanner bscan = new Scanner(new File("data.txt"));
PrintWriter bsorted = new PrintWriter("bsorted.txt");
StringType[] strings = new StringType[2000];
for (int i = 0; i < strings.length; i++){
strings[i] = new StringType(bscan.nextLine());
}
Sort.bubbleSort(strings);
bsorted.println("Bubble Sort number of comparisons: " + Sort.bubbleComp + "\r\n" + "Bubble Sort number of exchanges: " + Sort.bubbleEx + "\r\n");
for (int i = 0; i < strings.length; i++) {
bsorted.println(strings[i].toString());
}
bsorted.close();
}
}
任何人的一些想法幫助我嗎?
發佈你的例外,它會給人們一個很好的開始幫助你的地方。 –