0

We are trying to tokenize a line of input from a file into an ADTList "tokens". It is reading the information ok, but the first object in the list is being set to NULL. We have tried many changes but cannot figure out where this is occurring. Here is the code:

    //method to covernt lines of input from a file to individual tokens
public static ADTList tokenizer(String input){

    String aToken;                                                      //defining variable to create token
    ADTList tokens = new ADTList();                                     //defining return variable

for (int i = 0; i < input.length(); i++){                           //loop to iterate through input string
        aToken = "";                                                    //clearing variable to hold next string
        if (input.charAt(i) == '(' || input.charAt(i) == ')' ||
                operator(input.charAt(i))){                             //testing for parenthesis or operator
            aToken += input.charAt(i);                                  //concatenating character to string
            tokens.add(aToken);                                         //adding new string to token list
        }else if (Character.isLetter(input.charAt(i))){                 //testing for variable letter
            aToken += input.charAt(i);
    i++;                                                        //moving to next character in string
            if (i < input.length() && Character.isLetterOrDigit(input.charAt(i))){   
                do{                                                     //loop to test for end of variable
                    aToken += input.charAt(i);
                    i++;
                }while (i < input.length() && Character.isLetterOrDigit(input.charAt(i)));    //end of variable condition
            }
            tokens.add(aToken);
            i--;                                                        //decrementing counter variable
        }else if (Character.isDigit(input.charAt(i))){                  //testing for number
    aToken += input.charAt(i);
    i++;
                if (i < input.length() && Character.isDigit(input.charAt(i))){   
                do{                                                     //loop to test for end of variable
                    aToken += input.charAt(i);
                    i++;
                }while (i < input.length() && Character.isDigit(input.charAt(i)));    //end of digit condition
            }
            tokens.add(aToken);
            i--;
        }else{
    //do nothing
        }//end if
}//end loop

return tokens;                                                      //returns tokens list to main
}//endFunction

Here is a snapshot of the output showing the NULL object in the first position:

  • Original function:a1 * ( b + c )
  • Tokenized: null, a1, *, (, b, +, c, ),
  • Infix to Postfix:
  • Postfix to Infix:

  • Original function:( a * b + c

  • Tokenized: null, (, a, *, b, +, c,
  • Infix to Postfix:
  • Postfix to Infix:

Here is the ADTList Class. This is not all, but it does show the add(T item) method that is called by our main project:

public class ADTList<T> implements ListInterface<T>{

//defining internal variables to be used in interface class
private int numItems;
private T array[];

public ADTList(){
    //create an empty array
    array = (T[]) new Object [25];
    //assign 0 to numItems
    numItems=0;
}


public boolean isEmpty(){
// Determines whether a list is empty.
// Precondition: None.
// Postcondition: Returns true if the list is empty,
//                otherwise returns false.
    if (numItems==0){
        return true;
    }else{
        return false;
    }
}

public boolean isFull(){
// Determines whether a list is full.
// Precondition: None.
// Postcondition: Returns true if the list is full,
//                otherwise returns false.
    return numItems==array.length;
}

public int size(){
// Determines the length of a list.
// Precondition: None.
// Postcondition: Returns the number of items that are
//                currently in the list.
    return numItems;
}


public boolean add(T item){
// Adds an item to the end of list.
// Precondition: the item should be inserted in the list.
// Postcondition: If insertion is successful, it returns true,
//               item is at the end of the list,
//               Returns false if the list is full
    if (isFull()){
        return false;
    }else {
        array[numItems]=item;
        numItems++;
        return true;
    }
}
5
  • 2
    I suspect the problem is in the ADTList class, could you post code for that class too? Commented Dec 12, 2012 at 21:15
  • 2
    Can you use an ArrayList instead? Commented Dec 12, 2012 at 21:16
  • @PeterLawrey: no, we have to utilize the ADTList class that was discussed and coded in class. Commented Dec 12, 2012 at 21:59
  • Have you stepped through it with a debugger? I'd put a break point in ADTList::add and observe what happens on the first pass when numItems should be 0. Commented Dec 12, 2012 at 22:11
  • You may want to take a look at the way you are printing out the array. The add( ) looks fine. Commented Dec 13, 2012 at 0:01

4 Answers 4

1

Sidenote: You have several places where you write code like this:

if (big_long_test) {
    do {
        something();
    } while(big_long_test);
}

replace these with

while (big_long_test) {
    do_something();
}

The code above does not add a null to the list. Your problem is probably in the ADTList class.

Sign up to request clarification or add additional context in comments.

Comments

0

Not knowing the how ADTList is implemented (using an ArrayList internally?) if the object in the first position is an empty string then trim() the String input variable you read from the file to ensure there is no leading whitespace. If input has leading whitespace you will add an empty value to the ADTList.

1 Comment

there are no leading whitespaces on the input text file, I checked.
0

I think the problem is that the ADTList class makes the first member of the list null when the constructor is called. There is no problem with the code you provided.

Comments

0

Thank you all for the input. The error was in the printing out of the information. The get() function pulls array(position-1), therefore, we were pulling a -1 position. I do appreciate the suggestions and the code streamlining that was provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.