在一个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");
}
已格式化的代码现在可以读取。将来,请为我们做这个。请注意,这个答案完全取决于你的输入文件的内容和你的charCount方法。 –
输入文件是纯文本文件。 charCount方法是一种递归方法,用于计算特定字符在文档中出现的次数。 – Keith