使用Java在文本文件中查找具有最多连续元音的单词

时间:2014-09-16 00:07:02

标签: java

我有一个Java问题必须解决,除了最基本的代码之外什么都没有。它不能包含数组,除了我的代码中显示的内容之外,我无法真正导入任何内容。问题是:

本书网站上的文字words.txt包含87,314个英语单词。 编写一个读取这个文件的程序,找到连续元音最多的单词。

我对编程很陌生,所以我有一些想法,但不知道如何将它们放在一起。我真的坚持这个问题。任何帮助将不胜感激。

这是我提出的,但它显然是不正确的,我已经花了很多时间,包括研究这里和其他地方,并尝试我找到的代码。我不希望任何人为我做家庭作业,但如果你能给我一些指导,我将非常感激。这是我到目前为止所做的:

package vowels;

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

public class Vowels
{
    public static void main(String[] args)
    {
        Scanner fileIn = null;
        try
        {
            fileIn = new Scanner(new FileInputStream("words.txt")); 
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found");
            System.exit(0);
        }
        String eachWord = fileIn.next();
        String mostConsecutiveVowels = "";
        int w = 0;
        int z;
        int consecutiveVowels = 0;
        int mostConsecutiveVowelsInWord = 0;
        int wordWithMostConsecutiveVowels = 0;
        boolean vowel;
        boolean previousVowel;
        boolean mostVowels;

        while (fileIn.hasNext())
        {
            while(consecutiveVowels >= mostConsecutiveVowelsInWord)
            {
                mostVowels = true;
            }

            char a = eachWord.charAt(w);
            if (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
            {
               consecutiveVowels++;
               consecutiveVowels = mostConsecutiveVowelsInWord;
            }


            for(z = 1; z <= eachWord.length(); z++)
            {
                char b = eachWord.charAt(z);
                char c = eachWord.charAt(z-1);

                while (b=='a'||b=='e'||b=='i'||b=='o'||b=='u')
                {
                    vowel = true;
                }

                while (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
                {
                    previousVowel = true;
                }

                if (vowel = false && previousVowel = true && mostVowels = false;)
                {
                    consecutiveVowels = 0;
                }
                else if (vowel = false && previousVowel = true && mostVowels = true;)
                {
                    consecutiveVowels = mostConsecutiveVowelsInWord;
                }
                else if (vowel = true && previousVowel = false)
                {
                    consecutiveVowels = 1;   
                }
                else if (vowel = true && previousVowel = true && mostVowels = true;)
                {
                    consecutiveVowels++;
                    consecutiveVowels = mostConsecutiveVowelsInWord;
                }
                else if (vowel = true && previousVowel = true && mostVowels = false;)
                {
                    consecutiveVowels++;
                }
            }
        }
        if (mostVowels)
        {   
            if(eachWord.length()>mostConsecutiveVowels.length())
            {
                mostConsecutiveVowels = eachWord;
            }
        }
            System.out.println("The word in words.txt with the most consecutive vowels is " + mostConsecutiveVowels);
                fileIn.close();
    } 
}

1 个答案:

答案 0 :(得分:1)

这是我的解决方案。但是,您应该尝试使用自己的练习,如果您想使用我的代码中的注释作为建议。

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

public class Vowels {

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

  public static void main(String[] args) {
    try (Scanner fileScanner = new Scanner(new FileInputStream(WORD_FILE))) {
      String targetWord = null; // word with most consecutive vowels
      int maxConsecutiveVowels = 0;
      while (fileScanner.hasNext()) {
        // for each word in the file
        String word = fileScanner.next().toLowerCase();
        int consecutiveVowels = 0;
        for (int i = 0; i < word.length() && i < word.length() - maxConsecutiveVowels + consecutiveVowels; i++) {
          // for each character in the word, and exit early if the word is not long enough to beat maxConsecutiveVowels
          if (isVowel(word.charAt(i))) {
            // consonants reset this to 0
            consecutiveVowels++;
          } else {
            // reached the end of the vowels so check if we beat maxConsecutiveVowels
            if (consecutiveVowels > maxConsecutiveVowels) {
              maxConsecutiveVowels = consecutiveVowels;
              targetWord = word;
            }
            consecutiveVowels = 0;
          }
        }
        // reached the end of the vowels at the end of the word so check if we beat maxConsecutiveVowels
        if (consecutiveVowels > maxConsecutiveVowels) {
          maxConsecutiveVowels = consecutiveVowels;
          targetWord = word;
        }
      }
      if (targetWord == null) {
        System.out.println("there are no words with vowels in " + WORD_FILE);
      } else {
        System.out.println("the word in " + WORD_FILE + " with the most consecutive vowels is '" + targetWord + "'");
        System.out.println("it has " + maxConsecutiveVowels + " consecutive vowels");
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  private static boolean isVowel(char c) {
    switch (c) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      return true;
    }
    return false;
  }
}