2014-09-19 30 views
-1

不知道我的編程錯誤。目標是加載一個文件並找到連續元音最多的單詞。這段代碼沒有給我正確的字。它給我「aalii」時它應該給我「cooeeing」任何人都可以請幫助我嗎?如何用最連續的元音打印單詞?

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class manyVowels { 

    public static void main(String[] args) { 

     Scanner fileIn = null; 
     try { 
      //locate and open file 
      fileIn = new Scanner(new FileInputStream("words.txt")); 
     } catch (FileNotFoundException e) { 
      //if the file cannot be found, the program prints the message and quits 
      System.out.println("File not found. "); 
      System.exit(0); 
     } 
     String word; 
     if (fileIn.hasNext()) //if there is another word do as shown below 
     { 
     //seek consecutive vowels 
     word = fileIn.next(); 
      for(int i=0;i <word.length();i++){ 
       if((word.charAt(i) == 'A') || 
        (word.charAt(i) == 'E') || 
        (word.charAt(i) == 'I') || 
        (word.charAt(i) == 'O') || 
        (word.charAt(i) == 'U') || 
       (word.charAt(i) == 'a') || 
        (word.charAt(i) == 'e') || 
        (word.charAt(i) == 'i') || 
        (word.charAt(i) == 'o') || 
        (word.charAt(i) == 'u')) { 
      //prints the final word with the most consecutive vowels 
      System.out.println("The word with the most consecutive vowels is: " + word); 
      System.exit(0); 
     } 

     fileIn.close(); 
    } 
}}} 

更新:我走到這一步,但現在它是說,34行有一個 「在線程異常 」主「 顯示java.lang.NullPointerException」

import java.io.FileInputStream; 
 
import java.io.FileNotFoundException; 
 
import java.util.Scanner; 
 

 
public class manyVowels { 
 

 
    public static final String wordList = "words.txt"; 
 

 
    public static void main(String[] args) { 
 
     Scanner fileIn = null; 
 
     try { 
 
      //locate and open file 
 
      fileIn = new Scanner(new FileInputStream("words.txt")); 
 
     } catch (FileNotFoundException e) { 
 
      //if the file cannot be found, the program prints the message and quits 
 
      System.out.println("File not found. "); 
 
      System.exit(0); 
 
     } 
 
     String word = null; 
 
     if (fileIn.hasNext()) //if there is another word continue 
 
     { 
 

 
      String finalWord = null; // defines the word with most consecutive vowels 
 
      int maxVowels = 0;//sets initial value to 0 
 
      while (fileIn.hasNext()) { 
 
       // for each word in the file 
 
       int vowels = 0; 
 
       for (int i = 0; i < word.length() && i < word.length() - maxVowels + vowels; i++) { 
 
        // for each character in the word, and exit early if the word is not long enough to beat maxVowels 
 
        if (hasVowels(word.charAt(i))) { 
 
         // consonants reset this to 0 
 
         vowels++; 
 
        } else { 
 
         // reached the end of the word so check if the count is higher than maxVowels 
 
         if (vowels > maxVowels) { 
 
          maxVowels = vowels; 
 
          finalWord = word; 
 
         } 
 
         vowels = 0; 
 
        } 
 
       } 
 
       // comparing vowels to maxVowels 
 
       if (vowels > maxVowels) { 
 
        maxVowels = vowels; 
 
        finalWord = word; 
 
       } 
 
      } 
 

 
      //seek vowels 
 
      word = fileIn.next(); 
 
      for (int i = 0; i < word.length(); i++) { 
 
       if 
 
         ((word.charAt(i) == 'A') 
 
         || (word.charAt(i) == 'E') 
 
         || (word.charAt(i) == 'I') 
 
         || (word.charAt(i) == 'O') 
 
         || (word.charAt(i) == 'U') 
 
         || (word.charAt(i) == 'a') 
 
         || (word.charAt(i) == 'e') 
 
         || (word.charAt(i) == 'i') 
 
         || (word.charAt(i) == 'o') 
 
         || (word.charAt(i) == 'u')) { 
 
        //prints the final word with the most consecutive vowels 
 
        System.out.println("The word with the most consecutive vowels is: " + word); 
 
        System.exit(0); 
 
       } 
 

 
      } 
 
     } 
 
    } 
 

 
    private static boolean hasVowels(char charAt) { 
 
     throw new UnsupportedOperationException("Inserted by template."); //NetBeans generated method 
 
    } 
 
}

+1

你不選什麼,只是印刷的話,因爲他們來...... – Sebas 2014-09-19 23:56:46

+0

'它應該給我「cooeeing」'爲什麼它應該給你這個? – 2014-09-19 23:56:49

+0

哦@Sebas。它應該給我cooeeing,因爲文件上的單詞(這是一個長長的單詞列表,它有這個單詞,它是大多數元音的單詞) – spoilmealwayz 2014-09-19 23:58:26

回答

-1

而不是

if(fileIn.hasNext()) 使用 while(fileIn.hasNext())

1

試着用簡單的語言寫出你的程序,你實際上並沒有檢查有多少連續的元音。

Open file 
If the file contains a word, continue 
For each letter in the first word do: 
    if the letter is a vowel, 
     print "The word with the most consecutive vowels is: " + word 
     exit the program 

所以你實際上沒有做多少個連續的元音有任何檢查,而檢查是否有在第一個字中的元音。

嘗試寫了你想要什麼一些僞做打散再像以前那樣嘗試寫程序

+0

操作員想要最連續的元音不只是找到一個 – 2014-09-20 00:05:57

+0

是的,我可能有點不清楚,但我想他最清楚他想要什麼。問題是他真正檢查的是第一個單詞是否包含元音 – 2014-09-20 00:07:02