The add method does not work I dont know where is the mistak
The add() method does not work. I dont know where is the mistake, please fix it, here is the codes.
public class SymbolTable
 {
 private IdentifierNode[] symbolTable;
 IdentifierNode identifier;
 LineNumNode ptr;
 int i;
 public SymbolTable()
 {
 symbolTable = new IdentifierNode[50];
 for (int i=0;i<symbolTable.length;i++)
 {
 symbolTable[i]=null;
 }
 }
 public void add()
 {
 boolean found = false;
 i = 0;
 while(!found && symbolTable[i]!=null && i<symbolTable.length)
 {
 if(symbolTable[i].getIdentifier().equals(identifier))
 {
 found = true;
 }
 else
 {
 i++;
 }
 }
 if(found)
 {
 ptr = symbolTable[i].getNext();
 while(ptr.getNext()!=null)
 {
 ptr = ptr.getNext();
 }
 ptr.setNext(new LineNumNode(10,null));
 }
 else
 {
 symbolTable[i]=identifier;
 }
 }
 public void display ()
 {
 for (IdentifierNode symbolTable1 : symbolTable)
 {
 System.out.println(symbolTable1);
 }
 }
 }
----------
the rest of node classes are here.
public class LineNumNode
 {
 private int lineNum;
 private LineNumNode next;
 public LineNumNode(int lineNum, LineNumNode next)
 {
 this.lineNum = lineNum;
 this.next = next;
 }
 public int getLineNum()
 {
 return lineNum;
 }
 public LineNumNode getNext()
 {
 return next;
 }
 public void setNext(LineNumNode next)
 {
 this.next = next;
 }
 }
--------
public class IdentifierNode
 {
 String identifier;
 private LineNumNode next;
 public IdentifierNode(String identifier, LineNumNode next)
 {
 this.identifier = identifier;
 this.next = next;
 }
 public String getIdentifier()
 {
 return identifier;
 }
 public LineNumNode getNext()
 {
 return next;
 }
 public void setNext(LineNumNode next)
 {
 this.next = next;
 }
 }
-------
public static void main(String[] args) {
         // TODO code application logic here
 Scanner input = new Scanner(System.in);
 SymbolTable table = new SymbolTable();
 String identifier1;
 int num;
 boolean quit = false;
 IdentifierNode identifier;
 int choice;
 while(!quit)
 {
 System.out.println(\"1 - Add identifier\");
 System.out.println(\"2 - Display Table\");
 System.out.println(\"3 - Quit\");
 choice = input.nextInt();
 switch(choice)
 {
 case 1:
 System.out.println(\"Enter the identifier\");
 System.out.println(\"Enter the line number:\");
 table.add();
 break;
 case 2:
 table.display();
 break;
 case 3:
 quit = true;
 break;
 default:
 System.out.println(\"Invalid Entry\");
 break;
 }
 }
 }
 }
Solution
The program had to many logical errors. Find below updated code
import java.util.*;
class SymbolTable
 {
 private IdentifierNode[] symbolTable;
 IdentifierNode identifier;
 LineNumNode ptr;
 int i;
 public SymbolTable()
 {
 symbolTable = new IdentifierNode[50];
 for (int i=0;i<symbolTable.length;i++)
 {
 symbolTable[i]=null;
 }
 }
 public void add(String iden,int n)
 {
 boolean found = false;
 i = 0;
 while(!found && symbolTable[i]!=null && i<symbolTable.length)
 {
 if(symbolTable[i].getIdentifier().equals(identifier))
 {
 found = true;
 }
 else
 {
 i++;
 }
 }
 if(found)
 {
 ptr = symbolTable[i].getNext();
 while(ptr.getNext()!=null)
 {
 ptr = ptr.getNext();
 }
 ptr.setNext(new LineNumNode(n,null));
   
 
 }
 else
 {
 identifier = new IdentifierNode(iden,new LineNumNode(n,null));
 symbolTable[i]=identifier;
 }
 }
 
 public void display ()
 {
 for (IdentifierNode symbolTable1 : symbolTable)
 {
 if(symbolTable1!=null) System.out.println(symbolTable1);
 else break;
 }
 }
 
 public static void main(String[] args) {
 // TODO code application logic here
 Scanner input = new Scanner(System.in);
 SymbolTable table = new SymbolTable();
 String identifier1;
 int num;
 boolean quit = false;
 IdentifierNode identifier;
 int choice;
 while(!quit)
 {
 System.out.println(\"1 - Add identifier\");
 System.out.println(\"2 - Display Table\");
 System.out.println(\"3 - Quit\");
 choice = input.nextInt();
 switch(choice)
 {
 case 1:
 System.out.println(\"Enter the identifier\");
 identifier1=input.next();
 System.out.println(\"Enter the line number:\");
 num= input.nextInt();
 table.add(identifier1,num);
 break;
 case 2:
 table.display();
 break;
 case 3:
 quit = true;
 break;
 default:
 System.out.println(\"Invalid Entry\");
 break;
 }
 }
 }
 }
 class LineNumNode
 {
 private int lineNum;
 private LineNumNode next;
 public LineNumNode(int lineNum, LineNumNode next)
 {
 this.lineNum = lineNum;
 this.next = next;
 }
 public int getLineNum()
 {
 return lineNum;
 }
 public LineNumNode getNext()
 {
 return next;
 }
 public void setNext(LineNumNode next)
 {
 this.next = next;
 }
 }
 class IdentifierNode
 {
 String identifier;
 private LineNumNode next;
 public IdentifierNode(String identifier, LineNumNode next)
 {
 this.identifier = identifier;
 this.next = next;
 }
 public String getIdentifier()
 {
 return identifier;
 }
 public LineNumNode getNext()
 {
 return next;
 }
 public void setNext(LineNumNode next)
 {
 this.next = next;
 }
 
 public String toString()
 {
 return next.getLineNum()+\" \"+identifier;
   
 }
 }
![The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa](/WebImages/37/the-add-method-does-not-work-i-dont-know-where-is-the-mistak-1110189-1761588498-0.webp)
![The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa](/WebImages/37/the-add-method-does-not-work-i-dont-know-where-is-the-mistak-1110189-1761588498-1.webp)
![The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa](/WebImages/37/the-add-method-does-not-work-i-dont-know-where-is-the-mistak-1110189-1761588498-2.webp)
![The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa](/WebImages/37/the-add-method-does-not-work-i-dont-know-where-is-the-mistak-1110189-1761588498-3.webp)
![The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa The add() method does not work. I dont know where is the mistake, please fix it, here is the codes. public class SymbolTable { private IdentifierNode[] symbolTa](/WebImages/37/the-add-method-does-not-work-i-dont-know-where-is-the-mistak-1110189-1761588498-4.webp)
