我是java新手,不明白为什么我的代码不能同时打印文本文件和字母

我是java新手,不明白为什么我的代码不能同时打印文本文件和字母,java,Java,我的代码只打印文件的第一行,行数为1。 但是,数组会打印正确数量的字母。我怎样才能改变 我的代码可以同时打印文本和字母。我还想在 数组的第27位非字母字符,如符号和 谢谢 import java.io.*; public class Test { public static void main(String[] args)throws Exception { int lineCount=0; // Variale used to count the numbers of li

我的代码只打印文件的第一行,行数为1。 但是,数组会打印正确数量的字母。我怎样才能改变 我的代码可以同时打印文本和字母。我还想在 数组的第27位非字母字符,如符号和 谢谢

 import java.io.*;
 public class Test { 
 public static void main(String[] args)throws Exception {

     int lineCount=0; // Variale used to count the numbers of line. 
     int nextChar;
     char c;

     File file = new File("file.txt");  //  file in same folder

     BufferedReader readFile = new BufferedReader(new FileReader(file)); 
     String lines= " "; 
     int[] count = new int[27];
     char ch;

     while ((lines = readFile.readLine()) != null){

        System.out.println(lines);
        while ((nextChar = readFile.read()) != -1) {
        ch = ((char) nextChar);

            // Does this code count even uppercase or shall i  
            // convert it the text to lower case.

            if (ch >= 'a' && ch <= 'z'){
                count[ch - 'a']++;

            }
        }
        lineCount++;
    }

    System.out.println("file.txt containes " + lineCount + " lines.");

    for (int i = 0; i < 26; i++) {
        System.out.printf("%c %d", i + 'A', count[i]);
    }        
} 
} 
import java.io.*;
公共类测试{
公共静态void main(字符串[]args)引发异常{
int lineCount=0;//用于计算行数的变量。
int nextChar;
字符c;
File File=新文件(“File.txt”);//同一文件夹中的文件
BufferedReader readFile=新BufferedReader(新文件读取器(文件));
字符串行=”;
int[]计数=新的int[27];
char ch;
而((lines=readFile.readLine())!=null){
系统输出打印项次(行);
而((nextChar=readFile.read())!=-1){
ch=((char)nextChar);
//这个代码算大写还是我算
//将文本转换为小写。

如果(ch>='a'&&ch每个字母在ASCII标准中的映射方式不同。使用字母字符:

  • A-Z
    映射到
    65-90
    十进制
  • a-z
    映射到
    97-122
    decimal
所以,为了解决这个问题,您可以简单地以忽略大小写模式阅读字母,或者全部小写,或者全部大写


PS:您有一个完整的ASCII表格供进一步参考。

您与原始答案非常接近

主要问题是嵌套的
while
循环一直读取到文件的末尾-这就是为什么打印第一行,计数正确,但其他行没有打印的原因。 原因是
BufferedReader
由一个缓冲区支持(顾名思义)。对
readLine
的第一次调用返回一个
String
,其中包含缓冲区中直到第一个换行符的所有字符。在下面的while循环中,对
read
方法的每次调用都会将缓冲区的位置移动一个字符,直到到达文件的末尾,此时
read
方法返回-1,循环退出。第二次调用
readLine
时,缓冲区已经在结束位置,因此返回一个
null

您可以通过迭代readLine方法调用返回的行中的字节来解决此问题

下面是一个工作示例:

public static void main(String[] args) throws Exception {
    int lineCount = 0;// Variale used to count the numbers of line.
    File file = new File("file.txt");  //  file in same folder

    BufferedReader readFile = new BufferedReader(new FileReader(file));
    String lines;
    int[] count = new int[27];
    char ch;

    while ((lines = readFile.readLine()) != null) {
        System.out.println(lines);
        for (byte charByte : lines.getBytes()) {
            ch = (char) charByte;

            // Does this code count even uppercase or shall i convert
            // it the text to lower case.

            if (ch >= 'a' && ch <= 'z') {
                count[ch - 'a']++;
            // Count non-alpha characters here. Node: this will count numeric values also...
            } else if (ch < 'A' || ch > 'Z') {
                count[26]++;
            }
        }
        lineCount++;
    }

    System.out.println("file.txt containes " + lineCount + " lines.");

    for (int i = 0; i < 26; i++) {
        System.out.printf("%c: %d\n", i + 'A', count[i]);
    }
    System.out.printf("Special characters: %d\n", count[26]);
}
publicstaticvoidmain(字符串[]args)引发异常{
int lineCount=0;//用于计算行数的变量。
File File=新文件(“File.txt”);//同一文件夹中的文件
BufferedReader readFile=新BufferedReader(新文件读取器(文件));
弦线;
int[]计数=新的int[27];
char ch;
而((lines=readFile.readLine())!=null){
系统输出打印项次(行);
for(byte charByte:lines.getBytes()){
ch=(char)charByte;
//这个代码是大写还是我要转换
//将文本改为小写。
如果(ch>='a'&&ch'Z'){
计数[26]++;
}
}
lineCount++;
}
System.out.println(“file.txt包含“+lineCount+”行”);
对于(int i=0;i<26;i++){
System.out.printf(“%c:%d\n”,i+'A',count[i]);
}
System.out.printf(“特殊字符:%d\n”,计数[26]);
}

在内部while循环中,它读取所有字符,如“\n”等,因此当内部循环达到EOF时,外部循环只执行一次,因此linecount为1。我认为您希望从lines变量而不是内部循环中的reader读取,因为条件将读取到EOF,readline将只读取一次How以读取小写和大写字母等效。例如“a”和“a”将计数增加到2。我尝试将所有文件转换为小写,但无法。您可以在while look中将行转换为小写。因此,在打印行之前,您可以执行以下操作:line=line.toLowerCase();字符串是不可变的,因此您需要将其分配回其自身或新的字符串变量。Java的文本数据类型不使用ASCII。但您已经知道了,对吗?(
String wink=“@TomBlodget让我们假设它都是从ASCII开始的。为了这个例子,我举了它,因为我相信答案仍然适用,并且在这个变体中更容易讨论。但是,是的,你很好地指定了细节。