![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
need help with code plz
hey guys, ive been constantly strugglying to get my code working but have had no luck. i have 2 classes, ListDictionary.java & wordbase1.java which are both linked lists that have an inner node class. ListDictionary.java is an array of linked lists, which reads words in from a text file called dictionary.txt, where for the array every element of the array represents a letter of the alphabet, and contains a linked list of words in alphabetically order starting with that letter. The wordbase1.java is suppose to be a linked list containing all the words in the dictionary which is of 4 letters or less in length, in alphabetically order, and i seem to be getting errors with my array varible. My code is below, any help would be great!
public class wordbase1 extends ListDictionary
{
private wordbase head;
private ListDictionary insert;
public wordbase1(ListDictionary insert)
{
super();
this.insert = insert;
head = null;
}
public void insertDictionary()
{
insert.addToDictionary();
wordbase p = new wordbase(array[0].getWord());
for(int i = 0; i < 26; i++)
{
p.getWord() = array[i].getWord();
if( (array[i].getWord()).length() <= 4)
{
if(head == null)
{
head = p;
}
else if(head.word.compareTo(p.word) >=0)
{
p.next = head;
head = p;
}
else
{
wordbase node = head;
while((node.next != null) && ((node.next).word).compareTo(p.word) < 0)
{
node = node.next;
}
p.next = node.next;
node.next = p;
}
}
}
}
public class wordbase extends NodeDictionary
{
private int flag;
private wordbase next;
public wordbase(String word)
{
super(word);
flag = 0;
next = null;
}
public String getWord()
{
super.getWord();
}
}
}import java.util.*;
import java.io.*;
public class ListDictionary
{
private NodeDictionary head;
private ListDictionary[] array;
public ListDictionary()
{
head = null;
array = new ListDictionary[26];
}
public NodeDictionary search(String word)
{
NodeDictionary p = head;
String dataAtPosition;
while(p != null)
{
dataAtPosition = p.word;
if( dataAtPosition.equals(word))
{
return p;
}
p = p.next;
}
return null;
}
public boolean retrieveWord(String word)
{
return (search(word) != null);
}
public void showList()
{
NodeDictionary p = head;
while(p !=null)
{
System.out.println(p.word);
p = p.next;
}
}
public ListDictionary[] getArray()
{
return array;
}
public void setArray(ListDictionary[] array)
{
this.array = array;
}
public void addToDictionary()
{
try
{
BufferedReader input = new BufferedReader(new FileReader("dictionary"));
String line = "";
char arrayLine; //is the words in dictionary.txt gonna be upper or lower case
while(line != null)
{
line = input.readLine();
arrayLine = (char)line.charAt(0);
switch(arrayLine)
{
case 'A':
case 'a':
array[0].insert(line);
break;
case 'B':
case 'b':
array[1].insert(line);
break;
case 'C':
case 'c':
array[2].insert(line);
break;
case 'D':
case 'd':
array[4].insert(line);
break;
case 'E':
case 'e':
array[5].insert(line);
break;
case 'F':
case 'f':
array[6].insert(line);
break;
case 'G':
case 'g':
array[7].insert(line);
break;
case 'H':
case 'h':
array[8].insert(line);
break;
case 'I':
case 'i':
array[9].insert(line);
break;
case 'K':
case 'k':
array[10].insert(line);
break;
case 'L':
case 'l':
array[11].insert(line);
break;
case 'M':
case 'm':
array[12].insert(line);
break;
case 'N':
case 'n':
array[13].insert(line);
break;
case 'O':
case 'o':
array[14].insert(line);
break;
case 'P':
case 'p':
array[15].insert(line);
break;
case 'Q':
case 'q':
array[16].insert(line);
break;
case 'R':
case 'r':
array[17].insert(line);
break;
case 'S':
case 's':
array[18].insert(line);
break;
case 'T':
case 't':
array[19].insert(line);
break;
case 'U':
case 'u':
array[20].insert(line);
break;
case 'V':
case 'v':
array[21].insert(line);
break;
case 'W':
case 'w':
array[22].insert(line);
break;
case 'X':
case 'x':
array[23].insert(line);
break;
case 'Y':
case 'y':
array[24].insert(line);
break;
case 'Z':
case 'z':
array[25].insert(line);
break;
default:
System.out.println("This word does not start with a alphabetically char");
break;
}
}
}
catch(FileNotFoundException e)
{
System.out.println("File opening problem");
}
catch(IOException e)
{
System.out.println("File reading problem");
}
}
public void insert(String word)
{
NodeDictionary p = new NodeDictionary(word);
if(head == null)
{
head = p;
}
else if( (head.word).compareTo(p.word) >= 0 )
{
p.next = head;
head = p;
}
else
{
NodeDictionary node = head;
while( (node.next != null) && ((node.next).word).compareTo(p.word) < 0 )
{
node = node.next;
}
p.next = node.next;
node.next = p;
}
}
public class NodeDictionary
{
protected String word;
private NodeDictionary next;
public NodeDictionary(String word)
{
this.word = word;
next = null;
}
public String getWord()
{
return word;
}
}
}the errors im getting are... wordbase1.java:27: cannot find symbol
symbol : method getWord()
location: class ListDictionary
wordbase p = new wordbase(array[0].getWord());
^
wordbase1.java:30: unexpected type
required: variable
found : value
p.getWord() = array[i].getWord();
^
wordbase1.java:30: cannot find symbol
symbol : method getWord()
location: class ListDictionary
p.getWord() = array[i].getWord();
^
wordbase1.java:31: cannot find symbol
symbol : method getWord()
location: class ListDictionary
if( (array[i].getWord()).length() <= 4)
^
4 errorsI have been trying to fix it for a while but no luck yet, any help would be great. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
public ListDictionary()
{
array = new ListDictionary[26];
} |
|
|
|
|
|
#3 | |
|
Expert Programmer
|
Quote:
Ok, so after looking over your code, I think the problem is with the first error you specified. Where is that array coming from? It's obviously having problems finding it. I'm not great at Java, so I can't say much. My advice to you is to start over with new clean empty files, recreating and copying over some code doing step by step, and see what you end up with. Anyways, good luck. |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
You have a number of problems you need to fix.
The first, and most obvious, is the omission of a return statement in the wordbase1.wordbase.getWord method, which can be easily corrected: public String getWord()
{
return super.getWord();
}protected ListDictionary[] array; In addition, you have a dilemma here: p.getWord() = array[i].getWord(); setWord(array[i].getWord()) Even after correcting the above errors, I believe it is safe to say you will have a long list of new problems to solve. If I were you, I would rewrite the code from scratch--it is certainly possible to do whatever you are attempting without two nested classes in two nested files, and worse, one of those nested classes extending the other nested class which is made possible by its outer class extending the class in which the class it is extending is nested. (Ugh!) No offense intended, but this may very well be the ugliest Java code I have ever seen. You might consider entering an obfuscated code contest. Good luck... |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
case 'A':
case 'a':
array[0].insert(line);
break;
...
case 'Z':
case 'z':
array[25].insert(line);
break; |
|
|
|
|
|
#6 |
|
Expert Programmer
|
Or better yet:
if (Character.isLetter(line.charAt(0)))
...EDIT: Sane, you are correct. The code I provided only determines whether the letter is a character, not which index of the array to use. char c = line.charAt(0);
int i;
if (Character.isLetter(c)) {
i = (int)c;
if (Character.isLowerCase(c))
i -= 32;
i -= 65;
// i is now set to the correct index value
}Last edited by titaniumdecoy; Oct 1st, 2006 at 9:43 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| EXECryptor software protection | Jean5 | C++ | 35 | Oct 10th, 2006 7:10 PM |
| Little help | whoawhoayoyo | Assembly | 8 | Apr 18th, 2006 7:10 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |