简体   繁体   English

为什么我的函数不能在文件中保留int值的总数?

[英]why cant my function keep a total amount of int values within file?

I am making a program that reads a file of mixed values (int and string), prints only the integer values and keeps a running total of the amount of integer values within the file. 我正在编写一个程序,该程序读取混合值(int和字符串)的文件,仅打印整数值,并保持文件中整数值的运行总和。 Everything is working except for my running total of integer values within a given file and i am very confused on why it keeps printing 0 when i know there are more then 0 integer values within the file. 除了我在给定文件中运行的整数值的总数之外,所有东西都在工作,当我知道文件中的整数值超过0时,为什么我仍然打印0令我非常困惑。

Here is my code: 这是我的代码:

package davi0030_a03;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MyFile {
    private String fileName; // name of the file
    private int count = 0; // number of valid integers in the file
    private final int MAX_SIZE = 10; // the size of the array
    private Scanner inputStream = null;
    private int[] theArray = new int[MAX_SIZE];
    private boolean strangeInt = false;
    private int total = 0;

// constructor to set the file name

public MyFile(String theName) { // constructor to set the file name
    this.fileName = new String(theName);

    // you may or may not want to do other stuffs here
}

public void openFile() {
    System.out.println("opening file: " + fileName);
    try {
        inputStream = new Scanner(new FileInputStream("src/davi0030_a03/"
                + fileName));
    } catch (FileNotFoundException e) {
        System.out.println("File was not found or could not be opened");

    }
}

// log a message on whether two ints in the file add to target
public void findPair(int target) {
    openFile();
    fileToArray();
    findStrangeInt();
    findTotal();


}
public void findTotal(){
    inputStream.reset();
    while(inputStream.hasNext()){
        if(inputStream.hasNextInt()){           
            total +=1;
        }
        inputStream.next();
    }
    System.out.println(total);

}

public void findStrangeInt() {
    inputStream.reset();
    while (inputStream.hasNext()) {

        try {

            Integer.parseInt(inputStream.next());
        } catch (NumberFormatException nfe) {
            strangeInt = true;
        }
    }
    if (strangeInt = true) {
        System.out.println("File contains an incorrectly written int");
    }
}

public void fileToArray() {
    inputStream.reset();
    while (inputStream.hasNext() && count < MAX_SIZE) {
        if (inputStream.hasNextInt()) {
            theArray[count] = inputStream.nextInt();
            count++;
        }

    }

}

// print the content of the file
public void printFile() {
    openFile();
    inputStream.reset();
    System.out.println("Printing content of file " + fileName);
    while (inputStream.hasNext()) {

        try {

            int convert = Integer.parseInt(inputStream.next());
            System.out.println(convert);
        } catch (NumberFormatException nfe) {
            System.out.println("xxx");
        }
    }

}

} }

content of file: 文件内容:

1 1个

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

10 10

11 11

12 12

13 13

14 14

a 一种

b b

d d

EDIT: My solution below most likely solves the logic issue regarding your searching the file. 编辑:下面我的解决方案最有可能解决有关您搜索文件的逻辑问题。 However, I suggest you read some of the other comments the question has gotten. 但是,我建议您阅读该问题得到的其他一些评论。 The IO handling here especially needs some improvement. 此处的IO处理特别需要改进。

I believe your issue lies in your while statement. 我相信您的问题出在您的while陈述中。 According to java documentation on the Scanner class, here , the method .hasNextInt() is returning based on the next element scanned. 根据Scanner类的Java文档, 此处 ,.hasNextInt()方法基于下一个扫描的元素返回。 I think you are assuming it will return true as long as there are ints in the file. 我认为您假设只要文件中有整数,它就会返回true。 This is not the case. 不是这种情况。 If the first thing the scanner hits is not an int, it is returning false and exiting your while loop. 如果扫描程序遇到的第一件事不是int,则返回false并退出while循环。 This would explain the returned 0, the value you initialize your variable 'total' to. 这将说明返回的0,即您将变量“总计”初始化为的值。 I would suggest you do something like the following: 我建议您执行以下操作:

while(scanner.hasNext()){
    if(scanner.hasNextInt()){           
        total +=1;
    }
    scanner.next();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM