2017-10-22 142 views
0

更新:在大家和一些朋友的帮助下找出它!我需要捕获字符串变量中的\ n字符以及空字符串。感谢所有评论和帮助的人!在Java中使用while循环来计算句子

这是给Java Intro的一个任务,我们还没有找到方法,并且当用谷歌搜索帮助时,这是唯一弹出的东西,所以我想我可能会试图问自己。

在此先感谢!

对于我们的项目,我们必须创建一个计算一些简单文本的FRI的程序,我有两个问题。

第一个问题是,使用Scanner对象和while循环从文件中计算句子时,我的计数始终是错误的+1。我可以在控制台上看到它打印出的数字是空的字符串,\ n打印出来(我正在打印到控制台,所以我可以看到变量正在被给出,而不是因为它是必需的),即使我拥有它设置为只增加我的积累变量,如果我的字符串不是空的。

任何帮助,将不胜感激! 我不知道需要什么样的我的部分代码,但是...

  /* Count Sentences */ 
     //Scanner object to read inputText 
     Scanner countSentences = new Scanner(inputText); 

     // While loop to count sentences 
     while(countSentences.hasNext()){ 
      String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next(); 
      System.out.println(sentence); 
      if(sentence.compareTo("") != 0){ 
       numSentences++; 
      } 
     } 

...就是一句计数发生在位。

再次感谢!

整个代码!编辑: ////////////////////////////////

描述: 一个程序来计算计算Flesch可读性索引 */

public class Readability { 

    public static void main(String[] args) throws IOException{ 
     final double FIRST_CONSTANT = -1.015;    //First constant of FRI formula 
     final double SECOND_CONSTANT = 84.6;    //Second constant of FRI formula 
     final double THIRD_COSTANT = 206.835;    //Third constant of FRI formula 
     final String SENTENCE_DELIMITERS = "[.:;?!]";  //Sentence delimiters 
     final String WORD_DELIMITERS = "[.:;?! ,\t,\n]"; //Word delimiters 

     int numSentences = 0;        //Number of sentences in inputText 
     int numWords = 0;         //Number of words in inputText 
     int numSyllables = 0;        //Number of syllables in each word in inputText 

     String standardText = "This is just a test.";  //Standard Text for testing 
     String inputText = "";        //Holds the text being tested currently 

     /* Ask user if they want to run a text analysis */ 
     int choice = JOptionPane.showConfirmDialog(null, "Do you want to run a text analysis?", "Start the Flesch Reading Ease Test", JOptionPane.YES_NO_OPTION); 

     /* Message Displayed if 'No' selected, and program exits */ 
     if(choice == JOptionPane.NO_OPTION) { 
      JOptionPane.showMessageDialog(null, "The program exits. Good Bye!"); 
      System.exit(0); 
     } 

     /* If user selects 'Yes' */ 
     // Ask user if they have a text file or wish to use the standard text 
     choice = JOptionPane.showConfirmDialog(null, "Answer \"Yes\" if you want to read the text from a file!\nAnswer \"No\" if you want a test run on a standard text.", "Select the input!", JOptionPane.YES_NO_OPTION); 

      /* If user selects 'Yes' */ 
      if(choice == JOptionPane.YES_OPTION){ 
       // Ask user for filename 
       String inputFileName = JOptionPane.showInputDialog("Enter input file name with extension!\nInclude the path if file is not in the project folder!"); 

        // User presses cancel, set inputText to standardText 
        if(inputFileName == null){ 
         inputText = standardText; 
        } 

        // User presses enter, create file object using user input as filename 
        else{ 
         File file = new File(inputFileName); 

        // Check if file exists, if file exists create scanner object to read text from file 
        if(file.exists()){ 
         Scanner readFile = new Scanner(file); 

         // Read the file line by line and collect the concatenated lines in the variable inputText 
         while(readFile.hasNext()){ 
          String fileSentences = readFile.nextLine(); 
          inputText += fileSentences + "\n"; 
         } 
        } 
        // If file doesn't exist, set inputText to the standardText 
        else { 
         inputText = standardText; 
        } 
        } 
      } 

      /* If user selects 'No' */ 
      else{ 
       inputText = standardText; 
      }  

      /* Count Sentences */ 
      //Scanner object to read inputText 
      Scanner countSentences = new Scanner(inputText); 

      // While loop to count sentences 
      while(countSentences.hasNext()){ 
       String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next(); 
       System.out.println(sentence); 
       if(sentence.length() != 0){ 
        numSentences++; 
       } 
       System.out.println(numSentences); //This println prints the correct number to the console 
      } 
      System.out.println(numSentences); //This println prints +1 

      //Close countSentences scanner 
      countSentences.close(); 

      /* Count Words */ 
      //Scanner object to read inputText 
      Scanner countWords = new Scanner(inputText); 

      // While loop to count words and increment syllable count per word (rule #3: each word has at least one syllable) 
      while(countWords.hasNext()){ 
       String word = countWords.useDelimiter(WORD_DELIMITERS).next(); 

       if(word.compareTo("") != 0){ 
        numWords++; 
        //numSyllables++; 
       } 

       // For loop to count syllables as each word is read 
       for(int k = 0; k < word.length(); k++){ 
        char letter = word.charAt(k); 


       } 
      } 

    //Test// 
    System.out.println(inputText); 
    System.out.println(numSentences); 
    System.out.println(numWords); 
    System.out.println(numSyllables); 
    } 

} 
+0

猜测:尝试更换'sentence.compareTo( “”)。'和'sentence.trim()的compareTo( “”)' – alfasin

+0

不能:(这是不是一个我们只能使用我们在课上学到的知识以及本书给我们的东西 – Cheyko

+0

什么是SENTENCE_DELIMITERS? –

回答

0

这是工作程序。它给了我作为导致2

import java.util.Scanner; 
    public class Scannerd { 

     public static String inputText = "What!? That is awful!"; 

     public static String SENTENCE_DELIMITERS = "[.:;?!]"; 

      public static void main(String... arg) { 
       int numSentences = 0; 

       Scanner countSentences = new Scanner(inputText); 

       // While loop to count sentences 
       while (countSentences.hasNext()) { 
       String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next(); 
       System.out.println(sentence); 
       if(sentence.compareTo("") != 0){ 
        numSentences++; 
       } 

       } 
       System.out.println("#ofSentences"+numSentences); 
      } 
    } 
0

看起来它使用在一开始默认的分隔符,因为自定义分隔符在循环内部使用。 试试这个。

import java.util.Scanner; 公共类TestScanner {

public static String inputText =“What!?That is awful!”;

public static String SENTENCE_DELIMITERS =“[。:;?!]”;

public static void main(String... arg) { 
     int numSentences = 0; 

     Scanner countSentences = new Scanner(inputText); 
     countSentences.useDelimiter(SENTENCE_DELIMITERS); 
     // While loop to count sentences 
     while (countSentences.hasNext()) { 
     //String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next(); 
      String sentence = countSentences.next(); 
     System.out.println(sentence); 
     if(sentence.compareTo("") != 0){ 
      numSentences++; 
     } 

     } 
     System.out.println("#ofSentences"+numSentences); 
    } 

}

+0

Nope :(,仍然没有为我工作,继续在循环内打印正确的值,然后在循环外面输入错误的值+1。 – Cheyko