3 Replies - 1462 Views - Last Post: 01 March 2011 - 04:01 PM Rate Topic: -----

#1 calumdt  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 28-January 11

Counting vowels in a text file

Posted 01 March 2011 - 06:10 AM

Hi, I'm trying to count and print all occurences of vowels from a file - please let me know if I can amend this method to work. I appreciate any feedback!

    public static void countVowelOccurrences(File file) throws Exception {
        Scanner scannerInput = new Scanner(file);
        int[] counts = new int[26];
        for (Scanner input = new Scanner(file); input.hasNext();)/> {
            String s = input.nextLine();
            for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)))
            counts[s.charAt(i) - 'a']++;
            }
            //Remove vowels

      }
       scannerInput.close();
System.out.print(counts[0] + "\t" + counts[4] + "\t" + counts[8] + "\t" + counts[14] + "\t" + counts[20]);

    }


Is This A Good Question/Topic? 0
  • +

Replies To: Counting vowels in a text file

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Counting vowels in a text file

Posted 01 March 2011 - 06:27 AM

You can check my snippet to count vowels in a String.
you can use the method on each line from the file.
http://www.dreaminco...snippet4522.htm
Was This Post Helpful? 1
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Counting vowels in a text file

Posted 01 March 2011 - 07:10 AM

I would definitely look into going with a File Reader which reads one char at a time. Instead of having to dissect every string, you would just read it one character at a time. Just my two cents. Also, I believe why you are getting an error is because you are mixing Scanner.hasNext() and Scanner.nextLine(). hasNext() only looks at the next character while Scanner.nextLine() takes in the next Line. Try using Scanner.next() instead :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#4 calumdt  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 28-January 11

Re: Counting vowels in a text file

Posted 01 March 2011 - 04:01 PM

Thanks for the help guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1