2016-01-24 99 views
0

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

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

已格式化的代码现在可以读取。将来,请为我们做这个。请注意,这个答案完全取决于你的输入文件的内容和你的charCount方法。 –

+0

输入文件是纯文本文件。 charCount方法是一种递归方法,用于计算特定字符在文档中出现的次数。 – Keith

回答

1

既然你举报的问题似乎是与IDE,我建议该问题可能是与被作为参数传递的文件的路径。 NetBeans甚至可以识别该文件?

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

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

我推测这里的路径可能是个问题。也许我们可以更好地理解,如果你张贴一些关于这方面的信息。

+0

3个数据文件data.txt。 data2.txt和data3.txt都在ide的项目文件夹中。我可以在IDE中使用前一个方法访问数据文件,只需在netbeans中进行特定调用时,它就会返回false。 – Keith

+0

netbeans中声明的路径是当前相对路径是:E:\ My Documents \ NetBeansProjects \ AlphaCount – Keith

+0

您可以报告在先前访问解决问题后,是否通过调用close()方法关闭扫描器?我正在努力寻找这种行为的解释。 –