Extending the code  
Author Message
Matt





PostPosted: 2007-1-2 3:48:00 Top

java-programmer, Extending the code
I have the following code but i want to add a function which will allow
me to count the number of vowels in the text file. I was wondering if
anyone would be willing to give me some help with this


[CODE]
import java.io.*;
import java.util.*;
public class App {
//Create BufferedReader class instance
public static InputStreamReader input = new
InputStreamReader(System.in);
public static BufferedReader keyboardInput = new BufferedReader
(input);
public static void main(String[] args) {
int[] lengths = new int[100]; //Runtime error if the file contains a
word of more than 100 characters
int longestWord = 0;
for(int i = 0; i < lengths.length; i++) {
lengths[i] = 0;
}
try {
System.out.println("Enter the name of the file you wish to read:
");
String fileName = keyboardInput.readLine();
FileReader file = new FileReader(new File(fileName));
BufferedReader myFile = new BufferedReader(file);
int numTokens = 0;
int numWords = 0;
int numChar= 0;
String line;
while ((line = myFile.readLine()) != null ) {
String[] words = line.split(" ");
numWords = numWords + words.length;
for (int i = 0; i < words.length;i++) {
numChar = numChar + words[i].length();
if(words[i].length() > longestWord) {
longestWord = words[i].length();
}
lengths[words[i].length()] = lengths[words[i].length()] + 1;
}
}
System.out.println("\nThere are " + numWords + " words in the text
file");
System.out.println("\nThere are " + numChar + " characters in the
text file");
System.out.println("\nThere length frequency of the words is as
follows:");
System.out.println("\nLength : Frequency ");
for(int i = 0; i <= longestWord; i++) { //Print frequencies only up
to the longest word
System.out.println(i + " :"+lengths[i]);
}
myFile.close();
}
catch(IOException iO) {
iO.printStackTrace();
}
}
}
[/CODE]

 
Matt





PostPosted: 2007-1-2 3:52:00 Top

java-programmer >> Extending the code I have the following code but i want to add a function which will allow
me to count the number of vowels in the text file. I was wondering if
anyone would be willing to give me some help with this


[CODE]
import java.io.*;
import java.util.*;
public class App {
//Create BufferedReader class instance
public static InputStreamReader input = new
InputStreamReader(System.in);
public static BufferedReader keyboardInput = new BufferedReader
(input);
public static void main(String[] args) {
int[] lengths = new int[100]; //Runtime error if the file contains a
word of more than 100 characters
int longestWord = 0;
for(int i = 0; i < lengths.length; i++) {
lengths[i] = 0;
}
try {
System.out.println("Enter the name of the file you wish to read:
");
String fileName = keyboardInput.readLine();
FileReader file = new FileReader(new File(fileName));
BufferedReader myFile = new BufferedReader(file);
int numTokens = 0;
int numWords = 0;
int numChar= 0;
String line;
while ((line = myFile.readLine()) != null ) {
String[] words = line.split(" ");
numWords = numWords + words.length;
for (int i = 0; i < words.length;i++) {
numChar = numChar + words[i].length();
if(words[i].length() > longestWord) {
longestWord = words[i].length();
}
lengths[words[i].length()] = lengths[words[i].length()] + 1;
}
}
System.out.println("\nThere are " + numWords + " words in the text
file");
System.out.println("\nThere are " + numChar + " characters in the
text file");
System.out.println("\nThere length frequency of the words is as
follows:");
System.out.println("\nLength : Frequency ");
for(int i = 0; i <= longestWord; i++) { //Print frequencies only up
to the longest word
System.out.println(i + " :"+lengths[i]);
}
myFile.close();
}
catch(IOException iO) {
iO.printStackTrace();
}
}
}
[/CODE]

 
IchBin





PostPosted: 2007-1-2 8:28:00 Top

java-programmer >> Extending the code Matt wrote:
> I have the following code but i want to add a function which will allow
> me to count the number of vowels in the text file. I was wondering if
> anyone would be willing to give me some help with this
>
>
[Snip CODE]

>

Here is a vowel method inserted in your code. That works. You can test
the rest like the length frequency.

[CODE]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App
{
// Create BufferedReader class instance
public static InputStreamReader input = new
InputStreamReader(System.in);
public static BufferedReader keyboardInput = new
BufferedReader(input);
private static int numOfVouls = 0;

public static void main(String[] args)
{
int[] lengths = new int[100]; // Runtime error if the file
contains a
// word of more than 100 characters
int longestWord = 0;
for(int i = 0; i < lengths.length; i++)
{
lengths[i] = 0;
}
try {
System.out.println("Enter the name of the file you wish to
read: ");
String fileName = keyboardInput.readLine();
FileReader file = new FileReader(new File("C:\\"
+fileName));
BufferedReader myFile = new BufferedReader(file);
int numWords = 0;
int numChar= 0;
String line;

while ((line = myFile.readLine()) != null )
{
String[] words = line.split(" ");
numWords = numWords + words.length;
//
// Get number of vowels
vowelCount(line);

for (int i = 0; i < words.length;i++)
{
numChar = numChar + words[i].length();
if(words[i].length() > longestWord)
{
longestWord = words[i].length();
}
lengths[words[i].length()] =
lengths[words[i].length()] + 1;
}
}
System.out.println("\nThere are " + numWords + " words in
the text file");
System.out.println("\nThere are " + numOfVouls + " vowels
in the text file");
System.out.println("\nThere are " + numChar + " characters
in the text file");
System.out.println("\nThere length frequency of the words
is as follows:");
System.out.println("\nLength : Frequency ");

for(int i = 0; i <= longestWord; i++)
{
// Print frequencies only up to the longest word
System.out.println(i + " :"+lengths[i]);
}

myFile.close();
}
catch(IOException iO)
{
iO.printStackTrace();
}
}
public static void vowelCount(String target)
{
String key = "AEIOU";;
target = target.toUpperCase();

for (int i = 0; i < 5; i++)
{
int lastpos = 0;
while (lastpos > -1)
{
lastpos = target.indexOf( key.charAt(i), lastpos);
if (lastpos > -1)
{
lastpos += 1;
numOfVouls += 1;
}

}

}
}
}
[/CODE]

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
 
Lew





PostPosted: 2007-1-2 14:40:00 Top

java-programmer >> Extending the code John O'Conner wrote:
> By the way, you or someone like you

with, coincidentally, the same name,

> just recently posted a similar question just days ago...
> maybe you should google the question and responses?

- Lew
 
 
IchBin





PostPosted: 2007-1-4 4:14:00 Top

java-programmer >> Extending the code IchBin wrote:
> Matt wrote:
>> I have the following code but i want to add a function which will allow
>> me to count the number of vowels in the text file. I was wondering if
>> anyone would be willing to give me some help with this
>>
>>
> [Snip CODE]
>
>>
>
> Here is a vowel method inserted in your code. That works. You can test
> the rest like the length frequency.
>
> [CODE]
> import java.io.BufferedReader;
> import java.io.File;
> import java.io.FileReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
>
> public class App
> {
> // Create BufferedReader class instance
> public static InputStreamReader input = new
> InputStreamReader(System.in);
> public static BufferedReader keyboardInput = new
> BufferedReader(input);
> private static int numOfVouls = 0;
>
> public static void main(String[] args)
> {
> int[] lengths = new int[100]; // Runtime error if the file
> contains a
> // word of more than 100 characters
> int longestWord = 0;
> for(int i = 0; i < lengths.length; i++)
> {
> lengths[i] = 0;
> }
> try {
> System.out.println("Enter the name of the file you wish to
> read: ");
> String fileName = keyboardInput.readLine();
> FileReader file = new FileReader(new File("C:\\"
> +fileName));
> BufferedReader myFile = new BufferedReader(file);
> int numWords = 0;
> int numChar= 0;
> String line;
>
> while ((line = myFile.readLine()) != null )
> {
> String[] words = line.split(" ");
> numWords = numWords + words.length;
> //
> // Get number of vowels
> vowelCount(line);
>
> for (int i = 0; i < words.length;i++)
> {
> numChar = numChar + words[i].length();
> if(words[i].length() > longestWord)
> {
> longestWord = words[i].length();
> }
> lengths[words[i].length()] =
> lengths[words[i].length()] + 1;
> }
> }
> System.out.println("\nThere are " + numWords + " words in
> the text file");
> System.out.println("\nThere are " + numOfVouls + " vowels in
> the text file");
> System.out.println("\nThere are " + numChar + " characters
> in the text file");
> System.out.println("\nThere length frequency of the words is
> as follows:");
> System.out.println("\nLength : Frequency ");
>
> for(int i = 0; i <= longestWord; i++)
> {
> // Print frequencies only up to the longest word
> System.out.println(i + " :"+lengths[i]);
> }
>
> myFile.close();
> }
> catch(IOException iO)
> {
> iO.printStackTrace();
> }
> }
> public static void vowelCount(String target)
> {
> String key = "AEIOU";;
> target = target.toUpperCase();
>
> for (int i = 0; i < 5; i++)
> {
> int lastpos = 0;
> while (lastpos > -1)
> {
> lastpos = target.indexOf( key.charAt(i), lastpos);
> if (lastpos > -1)
> {
> lastpos += 1;
> numOfVouls += 1;
> }
>
> }
>
> }
> }
> }
> [/CODE]
>
Just wondering if the OP found this code to his needs?

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
 
John Ersatznom





PostPosted: 2007-1-4 23:03:00 Top

java-programmer >> Extending the code Matt wrote:
> I have the following code but i want to add a function which will allow
> me to count the number of vowels in the text file. I was wondering if
> anyone would be willing to give me some help with this
>
>
> [CODE]
> import java.io.*;
> import java.util.*;
> public class App {
> //Create BufferedReader class instance
> public static InputStreamReader input = new
> InputStreamReader(System.in);
> public static BufferedReader keyboardInput = new BufferedReader
> (input);
> public static void main(String[] args) {
> int[] lengths = new int[100]; //Runtime error if the file contains a

Please excuse me while I go throw up.

*BLETCH!*

Ah. Much better.

Okay. Where were we?

Ah yes, we were right where I'm supposed to recommend you use a
Map<Integer, Integer> in place of that array to get a nice, sparse
representation that doesn't ever blow up just because one day you use a
really big integer as a key. Hell, you can even use Long if Integer
isn't big enough, or BigInteger if you want it to work with a really
REALLY really big integer!

Actually, I've generally only got two uses for arrays except in very low
level heavily encapsulated code. One is a literal array fed to a
collection constructor. The other is a one-element array, a useful trick
for turning anything mutable whatever the class designer originally had
in mind. This includes turning things mutable that the *language*
designer didn't plan on --

class MyRuntimeException extends RuntimeException { }

public String fooBar () {

final int[] counter = new int[1];
counter[0] = 0;

try {
doSomething(new someInterface() {
public void someMethod() {
System.out.println("Yes! An honest-to-God" +
closure -- in Java!");
counter[0]++; // Did I really just increment
// that counter?
throw new MyRuntimeException("Let's" +
" return this string from fooBar!");
}
}
} catch (MyRuntimeException e) {
return e.getMessage(); // Could have had exception wrap any
// object.
}
// do other stuff
return someOtherString;

}


(OK, the "closure" return-from-lexical-scope only works if nothing
between doSomething and someMethod catches a generic RuntimeException,
Exception, or Throwable without rethrowing, which would be bad practise
anyway. So sue me. It's still damn close, albeit liberally sprinkled
with Java's own unique brand of syntactic salt.)