【发布时间】:2014-01-28 16:43:50
【问题描述】:
需要一些帮助来解决这个问题。 我想将 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();
}
}
作为任何人有什么想法可以帮助我吗?
【问题讨论】:
-
也发布您的例外情况,它将为人们提供一个很好的起点来帮助您。
标签: java arrays file printwriter