Netbeans的回报值不同

时间:2016-01-24 03:27:22

标签: java eclipse netbeans

处理java家庭作业项目并遇到一个小问题。当我在eclipse中编译并运行应用程序时,我得到了预期的结果。但是,当我在Netbeans中运行它时,我得到了不同的结果。当我检查inputFile.hasNext()的值时会出现问题。在eclipse上它返回true,结果是预期的,但是在Netbeans上,调用返回false。知道会导致这个问题的原因吗?我如何解决它 ?问题发生在while(inputFIle.hasNext())。

public int charCountHelper(File handle, Character x) throws IOException {
    int count = 0;
    String data;
    int index;
    Character[] contents;
    Scanner inputFile = new Scanner(handle);
    while (inputFile.hasNext()) {
        data = inputFile.nextLine();
        index = data.length() - 1;
        contents = new Character[data.length()];
        for (int i = 0; i < data.length(); i++) {
            contents[i] = data.charAt(i);

        }
        count += charCount(contents, x, index);
    }
    return count;
}

这是被调用的递归方法。

public int charCount(Character[] content, Character x, int index) {


    if(index < 0){
        return 0; // this value represents the character count if the program reaches the beginning of the array and has not found a match.
    }
    if (content[index].equals(x)) {
        return 1 + charCount(content, x, index - 1);
    }
        return charCount(content, x, index - 1); // this is the value that gets returned to the charCountHelper method.

}

这是班级的主要方法。

    private void Run() throws IOException {
        Scanner input = new Scanner(System.in);
        String fileName = null;
        Character letter;
        File handle;
        Integer selection;
        boolean isNumber;
                String path = System.getProperty("user.dir");
                System.out.println(path);

            do{
            System.out.println("Enter the number for the type of file to be read.");
            System.out.println("Press 1 to read from a file.");
            System.out.println("Press 2 to read from a file with no entries.");
            System.out.println("Press 3 to read from a file with excessive entries.");
            System.out.println("Press 0 to exit from the program.");

//This "if" statement is used for data type checking to makes sure that the user is entering valid data.
// if invalid data is entered, the "else" statement presents the user with the problem and then recycles the loop until valid data is entered.
            if(input.hasNextInt()){
                selection = input.nextInt();
                isNumber = true;

// This "if" statement manages the type of file to read from.               
                if(selection == 0){
                    System.out.println("Program exited.");
                    System.exit(0);
                    }
                    else if(selection == 1){
                        fileName = path+"/data.txt";
                        break;
                    }else if(selection == 2){
                        fileName = path+"/data2.txt";
                        break;
                    }else if(selection == 3){
                        fileName = path+"/data3.txt";
                        break;
                    }else{
                        System.out.println("Invalid input.");
                        System.out.println("Valid inputs are numers 0 - 3. Please try again.");
                        System.out.println(" ");
                        isNumber = false;
                    }
                }else{
                    System.out.println("Invalid input.");
                    System.out.println("Valid input are the numbers 0 - 3, Please try again.");
                    isNumber = false;
                    input.next();
                }

        }while(!(isNumber));

        handle = new File(fileName); // file handle is created using the previous loop to determine which file to read from.
        System.out.println("Enter a character to check.");
        letter = (char) System.in.read();
        input.close();// closes the open Scanner(good house keeping).

        System.out.println("There are "+charCountHelper(handle,letter)+" "+letter+"\'s");

    }

1 个答案:

答案 0 :(得分:1)

由于您报告的问题似乎与IDE有关,因此我建议问题可能在于作为参数传递的文件的路径。 NetBeans是否甚至识别该文件?

在Eclipse中,当前相对路径设置为项目目录。以下代码段将更好地解释这一点。

Path currentRelativePath = Paths.get("");
String myPath = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + myPath);

我推测路径可能是一个问题。如果你发布一些相关的信息,也许我们可以更好地理解。