Help with Arrays - Counting elements
Hello everyone hows it going :)
I've only recently been introduced to arrays and require a bit of help with a small exercise I was doing earlier. Here is the question :
Write a program which reads a sequence of words, and prints a count of the number of distinct words. An example of input/output is
should I stay or should I go
5 distinct words
I have solved the question below, without using an array but I presume an array is what the question requires as the exercises are related to the sections being studied. Could anybody show me how I would use one in my solution instead?
Code Java:
public class Ex1
{
public static void main(String[] args)
{
System.out.println("Enter Sentence:");
int count=0;
while(!Console.endOfFile())
{
String word = Console.readToken();
if(word.length()> 1)
{
count++;
}
}
System.out.println("Number of distinct words is " + count);
} // end of main
} // end of class
Re: Help with Arrays - Counting elements
Quote:
I have solved the question below
You can't solve the problem without saving what has been read previously to compare against.
What is your count for this: This cat and This dog and This other dog
To use an array, you have to assume a maximum number of possible words will be entered.
Create an array of that size.
Have an index to the array for adding new elements to the array.
when you get a new word, scan the array to see if its there, if not add it.
At the end all the unique words will be in the array.
Re: Help with Arrays - Counting elements
Quote:
Originally Posted by
Norm
You can't solve the problem without saving what has been read previously to compare against.
What is your count for this: This cat and This dog and This other dog
Count is 9. Its a distinct word if there is more then one letter. Im aware i havnt completely solved the question as i need an array. But how do I use one in a loop to check each word I enter to see if its length is greater than 1?
Re: Help with Arrays - Counting elements
Quote:
Its a distinct word if there is more then one letter
Are you sure about that definition? It doesn't make sense as an assignment.
I would think distinct meant the same as unique.
Re: Help with Arrays - Counting elements
Quote:
Originally Posted by
Norm
Are you sure about that definition? It doesn't make sense as an assignment.
I would think distinct meant the same as unique.
haha it would seem ive totally misinterpreted the question. I'm such a noob. Sorry for wasting your time
Im gonna attempt it now, knowing what a distinct word actually is. I'll keep this thread unsolved incase i encounter another problem because im still trying to get my head around arrays :(
Cheers :)
Re: Help with Arrays - Counting elements
Creating an array:
Code Java:
// an array of strings, with 10 elements
String[] list = String[10];
Getting/setting the element of an array:
Code Java:
// set
list[0] = "Hello world!";
// get
String theString = list[0];
Getting the length of an array:
Code Java:
int length = list.length;
You can compare two strings using the equals method.
Code Java:
String string1 = "Hello world";
String string2 = "not equal";
String string3 = "Hello world";
boolean same = string1.equals(string2); // same = false
same = string1.equals(strin3); // same=true
Once you create an array, it's size becomes fixed and can never be changed. To "change" the size of an array, you must create a new array of the size you want, then copy over the elements to the new array.
Re: Help with Arrays - Counting elements
Sorry but I'm still unsure as to how to keep going through the array and checking if a word is unique or not. All i know is that it's probably done in the for loop below with the question marks. Could anybody help me, i'm still confused with these :(
Code Java:
public class Ex1
{
public static void main(String[] args)
{
String[] words = new String[10];
System.out.println("Enter Sentence:");
for(int i=0; i<words.length; i++)
{
words[i] = Console.readToken();
}
for(int i=0; i<words.length; i++)
{
???
}
}// end main
}// end class
Re: Help with Arrays - Counting elements
Your posted code shows that you read words one after another into an array.
How would you do it if you were to do it manually with words written on a piece of paper?
Look at the next piece of paper, have I seen this word yet?
No, count it
Yes skip it
The trick here is to remember that you have seen the word before. Think about ways to do that