Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何考虑新段落的第一个单词?_Java_String_Character - Fatal编程技术网

Java 如何考虑新段落的第一个单词?

Java 如何考虑新段落的第一个单词?,java,string,character,Java,String,Character,我正在尝试构建一个程序,它接收文件并输出文件中的字数。当所有内容都在一整段中时,它就完美地工作了。但是,当有多个段落时,它不会考虑新段落的第一个单词。例如,如果文件读取“我的名字是约翰”,程序将输出“4个单词”。但是,如果一个文件读取“My Name Is John”,并且每个单词都是一个新段落,则程序将输出“1个单词”。我知道这一定与我的if语句有关,但我假设在新段落之前有空格,可以考虑新段落中的第一个单词。 以下是我的一般代码: import java.io.*; public class

我正在尝试构建一个程序,它接收文件并输出文件中的字数。当所有内容都在一整段中时,它就完美地工作了。但是,当有多个段落时,它不会考虑新段落的第一个单词。例如,如果文件读取“我的名字是约翰”,程序将输出“4个单词”。但是,如果一个文件读取“My Name Is John”,并且每个单词都是一个新段落,则程序将输出“1个单词”。我知道这一定与我的if语句有关,但我假设在新段落之前有空格,可以考虑新段落中的第一个单词。 以下是我的一般代码:

import java.io.*;
public class HelloWorld
{
    public static void main(String[]args)
    {
        try{
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("health.txt");
            // Use DataInputStream to read binary NOT text.
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

            int word2 =0;
            int word3 =0;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                // Print the content on the console
                ;
                int wordLength = strLine.length();
                System.out.println(strLine);
                for(int i = 0 ; i < wordLength -1 ; i++)
                    {
                        Character a = strLine.charAt(i);
                        Character b= strLine.charAt(i + 1);
                        **if(a == ' ' && b != '.' &&b != '?' && b != '!' && b != ' ' )**
                            {
                                word2++;
                                //doesnt take into account 1st character of new paragraph
                            }
                    }
                word3 = word2 + 1;
            }



            System.out.println("There are " + word3 + " "
                               + "words in your file.");
            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }


    }
}
import java.io.*;
公共类HelloWorld
{
公共静态void main(字符串[]args)
{
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(“health.txt”);
//使用DataInputStream读取二进制而不是文本。
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
int-word2=0;
int-word3=0;
//逐行读取文件
而((strLine=br.readLine())!=null){
//在控制台上打印内容
;
int-wordLength=strLine.length();
系统输出打印LN(斯特林);
for(int i=0;i