Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need Help Urgently please

807606Jun 3 2007 — edited Jun 3 2007
i'm not a Java expert but i like to think i'm not a beginner.

I've been working on a University Project now for a week and it must be submitted tomorrow . It is a Lexical Analyzer of sorts but for some reason the program freezes and kicks out this error "java.lang.ClassNotFoundException <uncaught>"thread=main", " when i debug to find the problem. it gives no errors on compile and just freezes. i can't figure this out . if anyone can help me it would be greatly appreciated.
Below is the code
My email is : therealcarbil@gmail.com

I know the code might be a bit messy or cluttered, so i apologize ahead of time,
package components;

import java.io.Console;
import java.io.InputStreamReader; 
import java.util.regex.*;
import java.lang.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
import javax.swing.*;
import javax.swing.filechooser.*;

public class LA extends JPanel implements ActionListener 
{
    static private final String newline = "\n";
    JButton OpenButton, LexButton, ExitButton, ViewLexButton, ViewSymbolButton;
    JTextArea log;
    JFileChooser fc;
    String Filepath="";
    String Filedata="";
    String Scanloc="";
    int strlength=0;
    String temp="";
    String[] reswords={"abstract","do","if","package","synchronized","boolean",
    				   "double","implements","private","this","break","else","enum",
    				   "import","protected","throw","byte","extends","instanceof",
    				   "public","throws","case","false","int","return","transient",
    				   "catch","final","interface","short","true","char","finally",
    				   "long","static","try","class","float","native","strictfp",
    				   "void","const","for","new","super","volatile","continue",
    				   "goto","null","switch","while","default","assert"};
    String[] symbols={"+","-","%","/","*","_","(",")","[","]","{","}","||","&&",
    				  "=","==","++","--","!=",">",">=","<=","<",".",",",";",":","!","<>","=>","=<","+=","-="};
    String[] symident={"PLUS","MINUS","MODULUS","DIVIDE","MULTIPLY","UNDERSCORE",
    				   "OPENING ROUND BRACKET","CLOSING ROUND BRACKET",
    				   "OPENING SQUARE BRACKET","CLOSING SQUARE BRACKET","OPENING CURLY BRACE",
    				   "CLOSING CURLY BRACE","OR","AND","EQUALS","INSTANCE COMPARE","INCREMENT",
    				   "DECREMENT","NOT EQUAL","GREATER THAN","GREATER THAN OR EQUAL TO",
    				   "LESS THAN OR EQUAL TO","LESS THAN","DOT","COMMA","SEMI-COLON","COLON","NOT","NOT EQUAL",
    				   "EQUAL TO OR GREATER THAN","EQUAL TO OR LESS THAN","PLUS EQUALS","MINUS EQUALS"};
    boolean state=false;
    int i=0;
    int j=0;
    boolean match=false;
    String LexToken="C:\\LexToken.txt";
    String SymTable="C:\\SymbolT.txt";
    String Lexstring="";
    String SymbolTableString=" Symbol |   Kind  | Type \n";
    ArrayList<String> wordarray=new ArrayList<String>();
    String[] temparray1;
    String REPat="";
    
    public LA() 
    {
        super(new BorderLayout());
        log = new JTextArea(20,40);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);
        fc = new JFileChooser();
        OpenButton = new JButton("Open a File");
        OpenButton.addActionListener(this);
        ExitButton = new JButton("Exit");
        ExitButton.addActionListener(this);
        LexButton = new JButton("Analyze Lexically");
        LexButton.addActionListener(this);
        ViewLexButton = new JButton("View Token/Lexeme Table");
        ViewLexButton.addActionListener(this);
        ViewSymbolButton = new JButton("View Symbol Table");
        ViewSymbolButton.addActionListener(this);
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(OpenButton);
        buttonPanel.add(LexButton);
        buttonPanel.add(ViewLexButton);
        buttonPanel.add(ViewSymbolButton);
        buttonPanel.add(ExitButton);
        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);
     
    }
    
    public static String StoreAsString(String FileName)throws Exception
   	{
   		FileInputStream fis = new FileInputStream(FileName);
		int x= fis.available();
		byte b[]= new byte[x];
		fis.read(b);
		String content = new String(b);
		return content;
   		
   	}

	public static void FileWrite(String filename,String Message)throws IOException
	{
	
		try
		{
		FileWriter fileWriter = new FileWriter(filename);
		BufferedWriter buffWriter = new BufferedWriter(fileWriter);
		buffWriter.write(Message);
		buffWriter.close();
		fileWriter.flush();
		fileWriter.close();
		}
		catch(IOException e)
		{
		System.out.println("Exception "+ e.getMessage());
		}
	}
	
	public boolean TestForResWord(int val)
	{
		char marker=' ';
		for(i=0;i<reswords.length;i++)
           {
            	if(reswords==wordarray.get(val))
{
i=reswords.length+1;
marker='Y';
}
else
{
marker='N';
}
}
if(marker=='Y')
{
return true;
}
else
{
return false;
}
}

public boolean patmatch(String patstr)
{
Pattern pattern= Pattern.compile(patstr);
Matcher matcher= pattern.matcher(temparray1[j]);
boolean temp=false;
temp=matcher.find();
if (temp=true)
{
return true;
}
else
{
return false;
}
}

public void actionPerformed(ActionEvent e)
{

try
{
if (e.getSource() == OpenButton)
{
int returnVal = fc.showOpenDialog(LA.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
Filepath = new String(file.getPath());
log.append("Opening: " + file.getName() + "." + newline);
log.append(Filepath + newline);
Filedata=StoreAsString(Filepath);
log.append(Filedata);
}
else
{
log.append("Open command cancelled by user." + newline);
}
}
else if (e.getSource() == LexButton)
{
//int answer = JOptionPane.showConfirmDialog(null,"Are you ready to analyze Lexically?");
// if (answer == JOptionPane.YES_OPTION) // User clicked YES.
// {
log.setText("");
try
{
FileReader rd = new FileReader(Filepath); // Create the tokenizer to read from a file
StreamTokenizer st = new StreamTokenizer(rd);
int linenum=0;
int colnum=0;
st.parseNumbers(); // Prepare the tokenizer for Java-style tokenizing rules
st.wordChars('_', '_');
st.eolIsSignificant(true);

// If whitespace is not to be discarded, make this call
st.ordinaryChars(0, ' ');

// These calls caused comments to be discarded
st.slashSlashComments(true);
st.slashStarComments(true);

// Parse the file
int token = st.nextToken();
while (token != StreamTokenizer.TT_EOF)
{
token = st.nextToken();
switch (token)
{
case StreamTokenizer.TT_NUMBER:
// A number was found; the value is in nval
double num = st.nval;
linenum=st.lineno();
temp=String.valueOf(num);
strlength=temp.length();
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+num+" |Lexeme: NUMBER";
colnum=colnum+strlength;
break;
case StreamTokenizer.TT_WORD:
// A word was found; the value is in sval
//String[] wordarray;
boolean match=false;
String word = st.sval;
int z=0;
if(z==0)
{
wordarray.add(0,word);
z++;
}
linenum=st.lineno();
char[] characters=word.toCharArray();
for(int x=0;x<characters.length;x++)
{
for (int y=0;y<symbols.length;y++)
{
String tempword="";
tempword=tempword+characters[x];
while((z>=1)&&(x<=characters.length))
{
if (tempword==symbols[y])
{
if((((characters[x]=='+')||(characters[x]=='-')||(characters[x]=='='))&&((wordarray.get(0+z-1)=="+")||(wordarray.get(0+z-1)=="-")||(wordarray.get(0+z-1)=="=")))||(tempword==wordarray.get(0+z-1))||(((tempword=="=")&&(wordarray.get(0+z-1)=="<"))||((tempword=="=")&&(wordarray.get(0+z-1)==">"))||((tempword==">")&&(wordarray.get(0+z-1)=="="))||((tempword=="<")&&(wordarray.get(0+z-1)=="="))||((tempword==">")&&(wordarray.get(0+z-1)=="<")) ) )
{
wordarray.add(0+z,wordarray.get(0+z)+characters[x]);
}
else
{
z++;//NOT SURE ABOUT THIS INCREMENT
wordarray.add(0+z,""+characters[x]);
z++;
}
}
}
}
wordarray.add(0+z,wordarray.get(0+z)+characters[x]);

}

while(state=false)
{
overhere:
for(j=0;j<wordarray.size();j++)
{
String[] temparray1=(String[])wordarray.toArray();
temp=temparray1[j];
strlength=temp.length();
if(strlength>1)
{
REPat="[\\W]";
match=patmatch(REPat);
if(match=false)
{
for(i=0;i<reswords.length;i++)
{
if(reswords[i]==wordarray.get(j))
{
String lexemestr=reswords[i].toUpperCase();
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+wordarray.get(j)+" |Lexeme: OP_"+lexemestr;
colnum=colnum+strlength;
i=reswords.length+1;
j++;
break overhere;
}
}
REPat="[(?i)[a-z][\\w*]*]";
match=patmatch(REPat);
if(match=true)
{
boolean test1=false;
test1=TestForResWord(j-1);
boolean test2=false;
test2=TestForResWord(j-2);
boolean test3=false;
test3=TestForResWord(j-3);
String[] temparray2=(String[])wordarray.toArray();
temp=temparray2[j];
strlength=temp.length();
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+temp+" |Lexeme: VARIABLE";
if(((test1==true)&&(test2==true)&&(test3==true))||((test1==true)&&(test2==true)&&(test3==false)))
{
SymbolTableString=SymbolTableString+" "+temp+" | Method | "+wordarray.get(j-1)+"\n";
}
else if((test1==true)&&(test2==false)&&(test3==false))
{
SymbolTableString=SymbolTableString+" "+temp+" | Variable | "+wordarray.get(j-1)+"\n";
}
colnum=colnum+strlength;
i=reswords.length+1;
}
else if(match=false)
{
String[] temparray3=(String[])wordarray.toArray();
temp=temparray3[j];
strlength=temp.length();
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+temp+" |Lexeme: INVALID_WORD";
colnum=colnum+strlength;
i=reswords.length+1;
}
}
else
{
for(i=0;i<symbols.length;i++)
{
if(symbols[i]==wordarray.get(j))
{
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+wordarray.get(j)+" |Lexeme: OP_"+symident[i];
colnum=colnum+strlength;
i=symbols.length+1;
}
}
}
}
else if(strlength==1)
{
for(i=0;i<symbols.length;i++)
{
if(symbols[i]==wordarray.get(j))
{
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+wordarray.get(j)+" |Lexeme: OP_"+symident[i];
colnum=colnum+strlength;
i=symbols.length+1;
}
else
{
boolean test1=false;
test1=TestForResWord(j-1);
boolean test2=false;
test2=TestForResWord(j-2);
boolean test3=false;
test3=TestForResWord(j-3);
Lexstring=Lexstring+"\nLine : "+linenum+" |Column : "+colnum+" |Token: "+wordarray.get(j)+" |Lexeme: VARIABLE";
if(((test1==true)&&(test2==true)&&(test3==true))||((test1==true)&&(test2==true)&&(test3==false)))
{
SymbolTableString=SymbolTableString+" "+temp+" | Method | "+wordarray.get(j-1)+"\n";
}
else if((test1==true)&&(test2==false)&&(test3==false))
{
SymbolTableString=SymbolTableString+" "+temp+" | Variable | "+wordarray.get(j-1)+"\n";
}
colnum=colnum+strlength;
i=symbols.length+1;
}
}
}
}
}
if(wordarray.size()>=j)
{
state=true;
}

//colnum=colnum+strlength;
break;
case '"':
// A double-quoted string was found; sval contains the contents
String dquoteVal = st.sval;
linenum=st.lineno();
strlength=dquoteVal.length();
colnum=colnum+strlength;
break;
case '\'':
// A single-quoted string was found; sval contains the contents
String squoteVal = st.sval;
linenum=st.lineno();
strlength=squoteVal.length();
colnum=colnum+strlength;
break;
case StreamTokenizer.TT_EOL:
// End of line character found
colnum=0;
break;
case StreamTokenizer.TT_EOF:
// End of file has been reached

break;
default:
// A regular character was found; the value is the token itself
char ch = (char)st.ttype;

break;
}
}
rd.close();
}
catch (IOException ex)
{
System.err.println("Exception with file! "+ex.getMessage());
}
log.append("Lexical Analysis Complete");
// }
// else if (answer == JOptionPane.NO_OPTION) // User clicked NO.
// {
// log.append("Analyze command decided against by user." + newline);
// }
// else if (answer == JOptionPane.CANCEL_OPTION) // User clicked CANCEL.
// {
// log.append("Analyze command cancelled by user." + newline);
// }
FileWrite(LexToken,Lexstring);
FileWrite(SymTable,SymbolTableString);
}
else if (e.getSource() == ViewLexButton)
{
log.setText("");
Filedata=StoreAsString("C:\\LexToken.txt");
log.append(Filedata);
log.append("LexToken.txt has been stored at C:\\LexToken.txt");
}
else if (e.getSource() == ViewSymbolButton)
{
log.setText("");
Filedata=StoreAsString("C:\\SymbolT.txt");
log.append(Filedata);
log.append("SymbolT.txt has been stored at C:\\SymbolT.txt");

}
else if (e.getSource() == ExitButton)
{
System.exit (0);
}
}
catch(FileNotFoundException ex)
{
System.err.println("File not found! "+ex);
}
catch(IOException ex)
{
System.err.println("Exception with file! "+ex);
}
catch(Exception ex)
{
System.err.println("Exception with file! "+ex);
}
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path)
{
java.net.URL imgURL = LA.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("LA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new LA();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args)throws IOException
{
// javax.swing.SwingUtilities.invokeLater(new Runnable()
// {
// public void run()
// {
createAndShowGUI();
// }
// });
}
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2007
Added on Jun 3 2007
21 comments
170 views