Creating arrays and for loop
I am trying to set the size of an array by the longest word length in a file I have read.
I have got the longest word length but need to create an array so it starts from 0 to the longest word length but am having trouble.
I am trying to do it so it gets the longest word length and prints out the same number of stars as what is in the word length
The first part of the code gets the longest word and sets the array size but it's the for loop that i can't get right
//Get the longest word to create array
for (int i=0; i<numWords.length; i++)
WordLength = WordLength+numWords[i].length();
if(numWords[i].length() > longestWord)
longestWord = numWords[i].length();
int test = new int[longestWord];
//This is the bit that I can't get right
for (int a=0; a<longestWord.length;a++)
Is this the sort of thing you're after? You did say that you already know the length of the longest word....
// this is the length of the longest word
int longestWord = _____
//creates an int array the same length as the longest word
int longWordArray = new int[longestWord];
//prints out stars
for(int i = 0; i < longestWord; i++)
assuming that your variable "longestWord" is an int then just take off the .length from
for (int a=0; a<longestWord.length;a++) so it is just
for(int a = 0; a < longestWord; a++)and the program should work. .length is usually only used to determine the length of an array and the method .length() is used to determine the length of a String. for an int, you do not need to call .length since it is already an int.
Thanks. I have now come across a final problem
I want to search an array of text which is 62 words long and I want to put each word length into a seperate array depending on the length of each word.
The array size is 11 so if (i presume) the loop finds a word tht is 4 characters long it adds it to that part of the array.
The problem I'm having is that I don't know how to increment arrays
I don't know if i'm on the right track with this
numWords is the count of all the words which should be 62
longestWord is the 11 characters long and is how big the array should be
int sort = new int[longestWord];
for (int a=0; a<numWords; a++)
for (int b=0; b<longestWord; b++)
This is where I need to increment the array by one
Do you want to put the words in the array, or keep track of how many words there are that each contain the same number of characters?
ie, do you want a frequency table?
word length freq.
I need something like a frequency table but instead of the number i need to use *
and so on
I have a loop but i can't get it to display like above
at the moment its all on one line and if i do put a \n in there the number for the next row is at the end of the previous row
//Display the word length distribution
System.out.println("\nWord length frequency");
for(int i = 0; i <= longestWord; i++)
for (int s=0; s<result[i];s++)
Last edited by AdRock; 01-10-2007 at 06:58 PM.
You're trying to generate the table in real time.
Don't do that.
Make an in array that is one element longer than the longestWord. This is the array that you use to keep track of the length of your words.
ie the value in wordLength is the number of words of length 4
It should be easy enough to iterate through your array of words, find out each of their lengths and add one to the appropriate element in wordLength.
After you've done that, then you can generate the star display.
Post what code you've got, and I'll see if I can help you
I have got the star bit working fine but i want to find a better way, probably using nested for loops to create the word length frequency
I have this array at the minute but want to change it if possible
In theory the nested loop should work becuase it works with the stars
int result = new int[longestWord];
result[numWords[i].length()] = result[numWords[i].length()] + 1;
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL