不知道我的編程錯誤。目標是加載一個文件並找到連續元音最多的單詞。這段代碼沒有給我正確的字。它給我「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
}
}
你不選什麼,只是印刷的話,因爲他們來...... – Sebas 2014-09-19 23:56:46
'它應該給我「cooeeing」'爲什麼它應該給你這個? – 2014-09-19 23:56:49
哦@Sebas。它應該給我cooeeing,因爲文件上的單詞(這是一個長長的單詞列表,它有這個單詞,它是大多數元音的單詞) – spoilmealwayz 2014-09-19 23:58:26