2017-06-19 73 views
-5

我的代码:的Java从.txt文件中读取数据,并将其输出到output.txt的文件

import java.io.*; 
public class ProgramSix { 

    public int count(String fileName, char charToSerach) throws FileNotFoundException, IOException { 

     char lower = Character.toLowerCase(charToSerach); 
     char upper = Character.toUpperCase(charToSerach); 

     int count = 0; 

     try (BufferedReader reader = new BufferedReader(new FileReader("xanadu.txt"))) { 
      int ch = 0; 
      while ((ch = reader.read()) != -1) { 
       if (lower == ch || upper == ch) { 
        count++; 
       } 
      } 
     } 
     System.out.println("The character '" + charToSerach + "' appears: " + count + " times in the .txt file"); 
     return count; 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     try { 
      Program6 program6 = new Program6(); 

      String fileName = "xanadu"; 
      int numAs = programSix.count(fileName, 'a'); 
      int numBs = programSix.count(fileName, 'b'); 
      int numCs = programSix.count(fileName, 'c'); 

     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
    } 
} 

所以现在我的代码正确地给出了输出:给予时代的人物的一个'数b'和'c'出现在我的.txt文件中。现在读取相同的.txt文件并在output.txt文件中发出三个不同的字母以及它们发生的次数的最佳方式是什么?

+0

你得到什么作为输出? –

+0

该档案是否存在? –

+3

也许一个异常正在发生......但是你可以通过给你的代码添加一个断点和_debugging_来自己回答这个问题。 –

回答

-1

您使用了错误的类

看到

class ProgramSix 

VS

Program6 program6 = new Program6(); 
+0

你是绝对正确的,谢谢。但是这会在输出中产生什么不同? – New2AllThis

+0

如果此代码编译然后在某个阶段,您必须有一个名为'Program6'类 - 它不是上述代码 –

+0

我改变了他们,并得到正确的输出,谢谢你承认我愚蠢的错误 – New2AllThis

相关问题