英文:
Reading from a file and inserting it into an array, Java
问题
我在运行这段代码时遇到了运行时错误。
我从文件中读取数据,将数据放入数组中,然后使用插入排序算法对其进行排序并打印结果。
是什么导致了我的运行时错误?这是运行时错误:java.util.InputMismatchException
以下是文件包含的内容:
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class InsertionSortExample {
private static int [] arr1;
public static void main(String[]args){
String fileName = "Data.txt"; //将文件名作为字符串添加
try{
Scanner fileScan = new Scanner(new File(fileName)); //查看文件
while(fileScan.hasNextLine()){ //当文件中还有另一行时
String inputLine = fileScan.nextLine(); //将该行添加到字符串中
Scanner scan = new Scanner(inputLine); //查看每一行
for(int i = 0; i < inputLine.length(); i++){ //逐行阅读
arr1[i] = scan.nextInt(); //将每个元素添加到数组中
}
}
}
catch(FileNotFoundException e){
System.out.println("文件未找到。请检查文件名和位置。"); //如果没有文件,它会显示
System.exit(1);
}
insertionSort(arr1); //使用插入排序对数组进行排序
for(int member:arr1){
System.out.print(member + " "); //打印数组的每个元素
}
}
public static void insertionSort(int array[]) {
int n = array.length; //数组的长度
for (int j = 1; j < n; j++) { //对数组的每个成员
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
}
英文:
I am getting a run time error this code.
I am reading from a file into putting the data into an array and then using an insertion sort algorithm to sort it and print the result
What is causing my run time error? This is the run time error:java.util.InputMismatchException
The following is what the file contains:
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class InsertionSortExample {
private static int [] arr1;
public static void main(String[]args){
String fileName = "Data.txt"; //adds file as string
try{
Scanner fileScan = new Scanner(new File(fileName));//looks at the file
while(fileScan.hasNextLine()){//while there is another line in the file
String inputLine = fileScan.nextLine(); //adds that line to a string
Scanner scan = new Scanner(inputLine); //looks at each line
for(int i = 0; i < inputLine.length(); i++){ //reads through the line
arr1[i] = scan.nextInt(); //adds each element to an array
}
}
}
catch(FileNotFoundException e){
System.out.println("File not found. Check file name and location."); // if theres no dile itll say
System.exit(1);
}
insertionSort(arr1);//sorting array using insertion sort
for(int member:arr1){
System.out.print(member +" "); //prints out each element of the array
}
}
public static void insertionSort(int array[]) {
int n = array.length; //length of the array
for (int j = 1; j < n; j++) { //for each member of the array
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
}
答案1
得分: 0
我想我知道为什么... 对于整型(int),数字的限制大约是20亿,或者是10位数字。你的数字要大得多。即使是长整型(long)也不能那么大... 也许可以使用BigInteger?但是你的排序函数需要进行更改。
英文:
I think I know why.. For int, the limit of number is around 2 billion.. or 10 digits. Your numbers are much bigger. Even long cannot be that big.. Maybe using BigInteger instead? but your sort function will need to be changed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论