DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Creating arrays and for loop

1. Registered User
Join Date
Jan 2007
Posts
60

## 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

Code:
//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++)
{
System.out.print("*");
}

2. Registered User
Join Date
Jul 2005
Posts
78
Is this the sort of thing you're after? You did say that you already know the length of the longest word....

Code:
// 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++)
{
System.out.print("*");
}

3. Registered User
Join Date
Dec 2005
Posts
97
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.

4. Registered User
Join Date
Jan 2007
Posts
60
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

Code:
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
}
}

5. Registered User
Join Date
Jul 2005
Posts
78
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?

Code:
word length  freq.
0                  0
1                  1
2                  3
3                  2
...
11                 1

6. Registered User
Join Date
Jan 2007
Posts
60
I need something like a frequency table but instead of the number i need to use *

1 **
2 *****
3 ***
4*
5 *********

and so on

I have a loop but i can't get it to display like above

Code:
//Display the word length distribution
System.out.println("\nWord length frequency");

for(int i = 0; i <= longestWord; i++)
{
System.out.print(i+" ");
for (int s=0; s<result[i];s++)
{
System.out.print("*");
}
}
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
Last edited by AdRock; 01-10-2007 at 06:58 PM.

7. Registered User
Join Date
Jul 2005
Posts
78
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[4] 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

8. Registered User
Join Date
Jan 2007
Posts
60
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

Code:
int[] result = new int[longestWord];

result[numWords[i].length()] = result[numWords[i].length()] + 1;
In theory the nested loop should work becuase it works with the stars

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise