-1

I'm sorry for having to ask this question, and I do sincerely appreciate your assistance. I'm working with Java, and I need to count the amount of times a x letter word appears. The word x is basically a number, replace it with whatever number (i.e. 1/2/3). ;)

x letter word = 1 letter word/2 letter word/3 letter word and so on.

So if a text file had the following text:

Hello World! This is a sample file.

The following should be output from Java:

1 letter words: 1 2 letter words: 2 3 letter words: 2 4 letter words: 3 5 letter words: 0 .. 9 letter words: 1

Sorry for the hassle, but would that be possible? Please let me know if it doesn't make any sense. I sincerely appreciate the assistance! :D

Thank you ever so much!

Kara
  • 6,115
  • 16
  • 50
  • 57
aullah
  • 1,334
  • 3
  • 20
  • 28
  • 3
    What have you tried, so far? Provide your code & state where your problem is. – Thomas W Aug 06 '13 at 02:18
  • *mount of times a x letter word appears.* , what do you exactly mean by `x letter` because I don't see any `x` in the text you've posted. – Azad Aug 06 '13 at 02:20
  • The code I have at the moment is pretty much nothing. All it does at the moment is buffer a txt file, and that's it. I can't seem to do anything else. :( I've tried: http://stackoverflow.com/questions/16296105/find-the-number-of-every-letters-in-a-word and http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – aullah Aug 06 '13 at 02:20
  • @AUllah1 - Basically, you are asking us to write your code for you. This is not acceptable. – Stephen C Aug 06 '13 at 02:24
  • Apologies Stephen C, I'll post my current code. :) – aullah Aug 06 '13 at 02:36

2 Answers2

2

You need to do google searches and look other places before asking questions, we generally handle fixing codes with small problems or errors. However, since people may come looking now that google can find this page...

First to read a file, you'll need a few Streams - things that do input/output with data.

File file = new File("C:\\MyFile.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

You'll be using the one on the end to access your data. If the file contains multiple lines, you'll want to use a loop to get them all:

while (dis.available() != 0) { // Tests for more lines
    String s = dis.readLine(); // Get the line

Now you split the lines into different strings using the method sushain97 showed you:

String[] sParts = s.split(" "); // Splits the strings into an array using the 'SPACE' as a reference

We need an array of integers for all the letters. The length will be the longest string.

int longestLength = 1; // The longest length of word
for (String strx : sParts) { // Go through each sPart, the next one is called strx
    if (longestLength < strx.length()) // If it's got a longer length than the current record
        longestLength = strx.length(); // Set a new record
}
int[] counts = new int[longestLength + 1]; // Make the array with the length needed; +1 since arrays are 0-based

Loop through all the strings and add 1 to the corresponding array number.

for(String str : strings)
    counts[str.length()] += 1; // Add one to the number of words that length has

And output it:

for(int i = 1; i < counts.length; i++) // We use this type of loop since we need the length
    System.out.println(i + " letter words: " + counts[i]);
} // Close that while loop from before

In the future, please do as I suggested, but I hope this helps all googlers coming here for information. :)

snickers10m
  • 1,709
  • 12
  • 28
0

How about:

String string = "Hello World! This is a sample file.";
String[] strings = string.split(" ");
int[] counts = new int[30]; //Change this depending on how far you want to go
for(String str : strings)
     if(str.length() < counts.length) counts[str.length()] += 1;
for(int i = 1; i < counts.length; i++)
    System.out.println(i + " letter words: " + counts[i]);

Pretty basic, modify as needed.

sushain97
  • 2,752
  • 1
  • 25
  • 36
  • Thank you very much! I've been working on it for hours, haha. Thank you so much! :) – aullah Aug 06 '13 at 02:36
  • @AUllah1 No problem, if you want to remove non-alphabet/space characters: `string = string.replaceAll("[^a-zA-Z ]", "")` – sushain97 Aug 06 '13 at 03:09