Java 读取整数的文本文件并使用扫描器和异常存储到数组
我试图在一个文本文件中循环查找整数,并将找到的整数存储到数组中 使用try-catch确定哪些字是整数,哪些字没有使用inputmaschException,从输入流中删除非整数字符串。以及文件中空行的NoTouchElementException。 我的主要问题是在第二种方法中存储整数并在数组中打印这些整数:o。我的循环似乎也将非整数记录为null。它们不会被存储到数组中Java 读取整数的文本文件并使用扫描器和异常存储到数组,java,arrays,exception,file-io,java.util.scanner,Java,Arrays,Exception,File Io,Java.util.scanner,我试图在一个文本文件中循环查找整数,并将找到的整数存储到数组中 使用try-catch确定哪些字是整数,哪些字没有使用inputmaschException,从输入流中删除非整数字符串。以及文件中空行的NoTouchElementException。 我的主要问题是在第二种方法中存储整数并在数组中打印这些整数:o。我的循环似乎也将非整数记录为null。它们不会被存储到数组中 public static void main(String[] commandlineArgument) {
public static void main(String[] commandlineArgument) {
Integer[] array = ReadFile6.readFileReturnIntegers(commandlineArgument[0]);
ReadFile6.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
public static Integer[] readFileReturnIntegers(String filename) {
Integer[] array = new Integer[1000];
// connect to the file
File file = new File(filename);
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
}
// If file not found-error message
catch (FileNotFoundException Exception) {
System.out.println("File not found!");
}
// if connected, read file
if (inputFile != null) {
// loop through file for integers and store in array
while (inputFile.hasNextLine()) {
for(int i = 0; i<array.length; i++)
{
try{
array[i] = inputFile.nextInt();
}
catch(InputMismatchException excep1)
{
String word = inputFile.next();
}
catch(NoSuchElementException excep2){
}
}
}
}
return array;
}
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
//prints number of integers from file
//prints each integer in array
}
}
publicstaticvoidmain(String[]commandlineArgument){
整数[]数组=ReadFile6.readFileReturnIntegers(commandlineArgument[0]);
ReadFile6.PrintArrayandIndegerCount(数组,commandlineArgument[0]);
}
公共静态整数[]readFileReturnIntegers(字符串文件名){
整数[]数组=新整数[1000];
//连接到文件
文件=新文件(文件名);
扫描仪输入文件=空;
试一试{
inputFile=新扫描仪(文件);
}
//如果未找到文件,则显示错误消息
捕获(FileNotFoundException异常){
System.out.println(“未找到文件!”);
}
//如果已连接,则读取文件
if(inputFile!=null){
//在文件中循环查找整数并存储在数组中
while(inputFile.hasNextLine()){
对于(int i=0;i假设您从文件中读取整数的所有逻辑都是正确的,也希望这是一种家庭作业。虽然以下实现不是正确的方法,但它只是解决了您的问题。我们在这里所做的就是迭代数组中的所有元素,直到它达到null
,并将它们写入一个缓冲区
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
StringBuilder sb = new StringBuilder();
int count = 0;
for(Integer i : array) {
if(i != null) {
count++;
sb.append("index = ").append(i).append(", element = ").append(array[i]).append("\n");
} else {
break;
}
}
System.out.println("number of integers in file \""+filename+"\" = "+count);
System.out.println(sb);
}
将catch语句替换为:
catch(InputMismatchException excep1)
{
String word = inputFile.next();
i-=1;
}
如果数组计数器发现一个单词,您将增加它的值。我已经运行了自己的测试,这对我解决您的问题起到了作用
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
String message = "";
int i = 0;
while(i < array.length && array[i]!=null){
message = message + "index = "+i+", element = "+array[i]+"\n";
i+=1;
}
System.out.println("number of integers in file \""+filename+"\" = "+i);
System.out.println(message);
}
public静态void printArrayAndIntegerCount(整数[]数组,字符串文件名){
字符串消息=”;
int i=0;
while(i
第一种方法中采用的方法有点缺陷,因为无论是否读取整数,都要递增i
变量
例如,如果文件如下所示:
4
Hello
5
e
7
数组的开头如下所示
[4, null, 5, null, 7...]
因此,您将得到一个大小为1000的数组,其中不可预测的地方有空值
更好的方法是:
- 保留一个单独的
count
变量,说明实际读取的整数数
- 在索引
count
处向数组中添加项,而不是在i
处添加项(因为i
只说明您查看了多少行,而count
将告诉您遇到了多少个整数)
- 当你读完它们的时候
- 将
count
变量传递给打印数组的方法(这样它只知道查看第一个count
项),或者
- 只需将整个数组复制到大小为
count
的新数组中
将其合并到代码中的示例:
if(inputFile != null) {
// the number of integers we've read so far
int count = 0;
// loop through file for integers and store in array
while(inputFile.hasNextLine()) {
for(int i = 0; i < array.length; i++) {
try {
array[count] = inputFile.nextInt();
count++;
} catch(InputMismatchException excep1) {
String word = inputFile.next();
} catch(NoSuchElementException excep2) {
}
}
}
}
这可能是更好的方法,因为您仍然可以保持所有方法签名相同,并且不需要处理返回多个变量或更改全局状态的问题。
或者,如果您不想将相关位复制到新数组中,那么您可以将count
变量以某种方式传递给第二个方法,然后执行如下操作
for(int i = 0; i < count; i++) {
System.out.println("\tindex = " + i + ", element = " + array[i]);
}
您是否可以使用List
而不是Array
,因为您的数组长度在这里始终是1000,并且很难判断您在该数组中实际存储了多少整数。不幸的是,没有列表,尽管这会使事情变得更容易。我需要使用数组:oHmm,我尝试实现了您和Sowrinator的建议但是我最终得到了一个ArrayIndexOutOfBoundsException。我可以对我的循环进行调整以不将空值存储到我的数组中吗?当您初始化整数[]
时,默认情况下数组将初始化为空值。您可以使用int[]
默认情况下是0并遵循peter方法。嗯,我理解你的过程。但在我的第二种方法中似乎找不到计数?嗯,比如我如何计算“计数”=到一个新数组并返回该数组,这样我就可以在我的第二个方法中打印它:o?只需将count作为参数添加到第二个方法中,然后从第一个方法将其传递到那里。但是如果使用第一个方法,效果会更好(请参阅上面的编辑)我尝试用returnnewarray代替returnarray,但是我的print方法会是什么样子呢?因为我不会使用“count”?抱歉,所有的麻烦您的打印方法将完全相同-它不会知道任何有关计数或新数组的信息。它所关心的是,它将给出的数组的长度将显示其中有多少实际数字。更新的答案反映了这一点。这似乎与我所寻找的输出相匹配。不过,您知道吗你知道我可以做什么,不存储任何东西,但整数(空)到我的数组?
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
for(int i = 0; i < array.length; i++) {
// print the number at array[i], and whatever else you want to print
}
}
for(int i = 0; i < count; i++) {
System.out.println("\tindex = " + i + ", element = " + array[i]);
}
public static void printArrayAndIntegerCount(Integer[] array, int count, String filename) {
...
}