Having trouble with CSC assignment. Trying to make an array of Strings

In summary, the conversation discusses the task of writing a program that can count the number of unique words and their occurrences in a given text file. The attempted solution involves using arrays and a do-while loop, but the program encounters an error when trying to check if an element in the array is null. The solution is to use a comparison operator instead of the .equals() method when checking for a null element.
  • #1
richyw
180
0

Homework Statement



I need to write a program that takes a text file, and looks for how many unique words it has, as well as the number of times they occur in the file.

Homework Equations



None

The Attempt at a Solution

public static void main(String[] args) throws FileNotFoundException {

Scanner s = new Scanner(new File("file.txt"));

String[] wordlist = new String[10000];
int[] wordcount = new int[10000];
int i = 0;
do {
String word = s.next();
if (wordlist.equals(null)) {
wordlist = word;
wordcount ++;
} else if (wordlist.equals(word)) {
wordcount ++;
}
i++;
} while (s.hasNext());
}

when I try and do this, it compiles, but when I run it I get an error. how do I get java to check and see if an element of an array is null?
 
Physics news on Phys.org
  • #2
Is this the line that gives the error?
Code:
if (wordlist[i].equals(null)

The problem is, of course, calling .equals() on a null element. Here's the fix:
Code:
if(wordlist[i] == null)
 

Related to Having trouble with CSC assignment. Trying to make an array of Strings

1. How do I create an array of Strings in my CSC assignment?

To create an array of Strings, you can use the following syntax: String[] arrayName = new String[size]. This will create an array of Strings with the specified size.

2. Why am I having trouble with my array of Strings in my CSC assignment?

There could be a few reasons for this. You may have incorrect syntax in your code, your array may not be initialized properly, or there could be a logical error in your code. It's important to carefully check your code and debug if necessary to identify and fix the issue.

3. How can I access elements in my array of Strings?

To access elements in your array of Strings, you can use the index of the element. For example, arrayName[0] would access the first element in the array. Remember that arrays start at index 0, so the last element in your array would be at index size-1.

4. What is the purpose of using an array of Strings in my CSC assignment?

An array of Strings allows you to store and manipulate multiple Strings in a single data structure. This can be useful for tasks such as sorting, searching, and data organization.

5. How can I troubleshoot my array of Strings if it is not working properly?

If your array of Strings is not working as expected, you can try debugging your code to identify any errors. You can also try printing out the values of your array at different points in your code to see if they match your expectations. Additionally, seeking help from a classmate, tutor, or instructor can also be helpful in troubleshooting your array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top