如何使用Java查找文件中的字符,元音和行数?

时间:2018-11-08 11:05:31

标签: java

程序接受输入,然后询问用户是否要输入更多内容。如果是,则给定输入将存储在下一行中。

我编写的以下代码显示除字符数以外的所有内容均为零。

有人可以帮忙吗?

import java.io.*;
import java.util.*;
public class Ex_3 {
public static void main(String args[])
{
    Scanner sc = new Scanner(System.in);
    Scanner sc2 = new Scanner(System.in);
    try
    {
        FileOutputStream fout = new FileOutputStream("file4.txt");
        FileInputStream fin = new FileInputStream("file4.txt");
        while(true)
        {
            System.out.println("Enter info in file:");
            String s = sc.nextLine();
            fout.write(s.getBytes());
            System.out.println("Would you like to enter more data?");
            char c = sc2.next().charAt(0);
            if(c=='n')
            {
                break;
            }
            fout.write("\r\n".getBytes());
        }
        fout.close();
        int chars=0,vowels=0,lines=0,words=0;
        int i=0;
        while((i=fin.read())!=-1)
        {
            if((char)i!='\n'||(char)i!=' ')
            {
                chars++;
            }
            else if((char)i==' ')
            {
                words++;
            }
            else if((char)i+48=='a'||(char)i+48=='e'||(char)i+48=='i'|| 
            (char)i+48 =='o'||(char)i+48=='u')
            {
                vowels++;
            }
            else if((char)i=='\n')
            {
                lines++;
            }
        }
        System.out.println("Number of characters:"+chars);
        System.out.println("Number of vowels:"+vowels);
        System.out.println("Number of words:"+words);
        System.out.println("Number of lines:"+lines);
        fin.close();
        sc.close();
        sc2.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
}

1 个答案:

答案 0 :(得分:1)

我检查了您的代码,并进行了一些更改和注释。其中有一些错误。

//Use concrete imports! 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

public class Main {

public static void main(String args[])
{
//You only need one scanner
Scanner sc = new Scanner(System.in);
try
{
    FileOutputStream fout = new FileOutputStream("file4.txt");
    FileInputStream fin = new FileInputStream("file4.txt");
    while(true)
    {
        System.out.println("Enter info in file:");
        fout.write(sc.nextLine().getBytes());
        //sometimes the line break is not recognized from the shell
        fout.write('\n');

        System.out.println("Would you like to enter more data?");
        if( sc.nextLine().charAt(0) == 'n' ) 
        {
            break;
        }
    }
    fout.close();
    sc.close();

    int chars=0,vowels=0,lines=0,words=2;
    int chars=0,vowels=0,lines=0,words=1;

    // With fin.read() you read 32 Bit integers from your input stream, 
    // which means that you have 4 chars "boxed" in one integer.
    int sizeOfStaticArray = 1024;
    //(ugly solution btw)
    byte[] arr = new byte[sizeOfStaticArray];
    fin.read(arr);
    byte i;
    for( int j = 0; j < sizeOfStaticArray; j++)
    {   
        i = (byte)arr[j];

        if(i == 0)
            break; 

        //You don't need casts to char 
        if( i != '\n' && i != ' ')
        {
            chars++;
        }
        //not else if. This is the main issue
        if( i == ' ' )
        {
            words++;
        }
        else if(i == 'a'||i  =='e'|| i == 'i'||  i =='o' || i == 'u')
        {
            vowels++;
        }
        else if( i == '\n' )
        {
            lines++;
            //increment word because the start of a line has no space
            words++;
        }
    }
    fin.close();
    System.out.println("Number of characters:"+chars);
    System.out.println("Number of vowels:"+vowels);
    System.out.println("Number of words:"+words);
    System.out.println("Number of lines:"+lines);

}
catch(Exception e)
{
    System.out.println(e);
}
}
}

IO:

Enter info in file:
 aaaaa hhhhh zzzzz eeeee
Would you like to enter more data?
 y
Enter info in file:
 uuuuu ggggg ggggg iiiii
Would you like to enter more data?
 n
 Number of characters:40
 Number of vowels:20
 Number of words:8
 Number of lines:2

希望有帮助, 祝好运!