不确定我的编程在哪里出错。目标是加载文件并找到连续元音最多的单词。这段代码没有给我正确的单词。它给了我" 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行在线程&#34;主要&#34;中有一个&#34;异常。显示java.lang.NullPointerException&#34;
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
}
}
&#13;
答案 0 :(得分: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
所以你实际上没有检查有多少个连续的元音,而是检查第一个单词中是否有元音。
尝试编写一些你想要分解的伪代码,就像我在你编写程序之前所做的一样
答案 1 :(得分:-1)
而不是
if(fileIn.hasNext()) 使用 而(fileIn.hasNext())