【发布时间】:2020-09-10 00:09:28
【问题描述】:
在编译之前我没有收到任何错误,一切都运行了,但我的输出应该是文本文件中的几个整数,而不是一堆随机字符和数字。我不确定出了什么问题,但我的任务是将整数文件读取到数组中,然后使用bubblesort方法进行排序。输出结果如下: Sorted Array order: [I@42a57993Sorted Array order: [I@42a57993Sorted Array order: [I@42a57993.就我而言,任何帮助都是很好的帮助。谢谢。
import java.io.File;
import java.io.FileNotFoundException;
public class Array {
public static void main(String[] args) {
int[] arr = new int[10];
int i = 0;
Scanner input = new Scanner(System.in);
try {
System.out.print("Enter the file name:");
String fileName = input.next();
Scanner readFile = new Scanner(new File(fileName));
while (readFile.hasNext()) {
try {
arr[i] = readFile.nextInt();
i++;
} catch (InputMismatchException e) {
readFile.next();
}
bubbleSort(arr);
System.out.print("Sorted Array order: " + arr);
input.close();
}}catch(FileNotFoundException e) {
System.out.println("Error occured");
e.printStackTrace();
}
}
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}}
【问题讨论】:
标签: java arrays file bubble-sort