Computing.Net > Forums > Programming > Java woes

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java woes

Reply to Message Icon

Name: Dreyfus
Date: March 4, 2008 at 18:46:50 Pacific
OS: xp
CPU/Ram: 2 gb
Comment:

I am having a bit of trouble working on a program in Java. The objective I am trying to complete here is to count all the occurrences of a text file, and assign the number of each occurrence into a one dimensional integer array. Here is my code.

-----------------
public static int countLetters(String token, char letter)
{
int counter = 0;
for (int n = 0; n < token.length(); n++)
if (token.charAt(n) == letter)
counter++;
return counter;
}

public static int[] frequency() throws IOException
{
int [] letters = new int[26];
String alphabet = "abcdefghijklmnopqrstuvwxyz";
Scanner inFile = new Scanner(new File("text.txt"));
String token = "";

while (inFile.hasNext())
{
token = inFile.next();
token = token.toLowerCase();

for (int n = 0; n < alphabet.length(); n++)
{
int counter = countLetters(token, alphabet.charAt(n));
letters[n] = counter;
}
}
return letters;
}



Sponsored Link
Ads by Google

Response Number 1
Name: heykiran
Date: March 6, 2008 at 13:44:35 Pacific
Reply:

Change the following statement

From
letters[n] = counter;

to
letters[n] = letters[n]+counter;


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java woes

Programming in java www.computing.net/answers/programming/programming-in-java/7510.html

Java SDK www.computing.net/answers/programming/java-sdk/8718.html

Sine wave generation using Java www.computing.net/answers/programming/sine-wave-generation-using-java/12012.html