Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 1st, 2006, 1:10 AM   #1
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
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 errors

I have been trying to fix it for a while but no luck yet, any help would be great.
ELHEK is offline   Reply With Quote
Old Oct 1st, 2006, 7:31 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
   public ListDictionary()
   {
      array = new ListDictionary[26];
   }
I don't know Java, but wouldn't that cause an infinite loop?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 1st, 2006, 8:06 PM   #3
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by Ooble View Post
   public ListDictionary()
   {
      array = new ListDictionary[26];
   }
I don't know Java, but wouldn't that cause an infinite loop?
Yeah I think it would, but it won't actually run unless it compiles:p It's a user error:p

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.
Booooze is offline   Reply With Quote
Old Oct 1st, 2006, 8:55 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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();
}
Now things get a little more complicated. In the wordbase1.insertDictionary method, you reference array[0].getWord(). Presumably, you are attempting to access the variable named array in the ListDictionary superclass, which is declared private. Changing this variable to protected (or better, creating an accessor method for it) will solve this problem. To make it crystal clear:

protected ListDictionary[] array;
Your next problem is that you are attempting to call getWord() on index 0 of the array variable, which is a ListDictionary object; as per your definition of the class, ListDictionary does not contain a getWord method. I can only guess at your intentions here; but since you have such a method defined in the wordbase1.wordbase class, perhaps it was your intent to work with objects of that class. If that is the case, you need to entirely rethink your class relationships.

In addition, you have a dilemma here:

p.getWord() = array[i].getWord();
On the left side of the equals sign, p.getWord() (ostensibly) returns a String object as an lvalue. You cannot assign a new value to an lvalue; rather, you assign a new value to a variable that points to one. You should create a setter method, setWord(String word) in wordbase, then call it as follows:

setWord(array[i].getWord())
Ooble asked whether the statement array = new ListDictionary[26] inside the ListDictionary class would cause an infinite loop. The answer is no, as it turns out, because each index of the array is initialized to null.

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...
titaniumdecoy is offline   Reply With Quote
Old Oct 1st, 2006, 9:01 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
               case 'A':
               case 'a':
                  array[0].insert(line);
                  break;
...
               case 'Z':
               case 'z':
                  array[25].insert(line);
                  break;
Could be generalized by taking the ordinal value of the character, and subtracting a defined amount to evaluate which array index is going to be used. If you don't understand how that would be done, give out a hollar and someone can explain.
Sane is offline   Reply With Quote
Old Oct 1st, 2006, 9:20 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC