Need Help Urgently please

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.*;

publicclass LAextends JPanelimplements ActionListener

{

staticprivatefinal 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);

}

publicstatic String StoreAsString(String FileName)throws Exception

{

FileInputStream fis =new FileInputStream(FileName);

int x= fis.available();

byte b[]=newbyte[x];

fis.read(b);

String content =new String(b);

return content;

}

publicstaticvoid 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());

}

}

publicboolean TestForResWord(int val)

{

char marker=' ';

for(i=0;i<reswords.length;i++)

{

if(reswords[i]==wordarray.get(val))

{

i=reswords.length+1;

marker='Y';

}

else

{

marker='N';

}

}

if(marker=='Y')

{

returntrue;

}

else

{

returnfalse;

}

}

publicboolean patmatch(String patstr)

{

Pattern pattern= Pattern.compile(patstr);

Matcher matcher= pattern.matcher(temparray1[j]);

boolean temp=false;

temp=matcher.find();

if (temp=true)

{

returntrue;

}

else

{

returnfalse;

}

}

publicvoid 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);

}

}

elseif (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";

}

elseif((test1==true)&&(test2==false)&&(test3==false))

{

SymbolTableString=SymbolTableString+" "+temp+" | Variable | "+wordarray.get(j-1)+"\n";

}

colnum=colnum+strlength;

i=reswords.length+1;

}

elseif(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;

}

}

}

}

elseif(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";

}

elseif((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);

}

elseif (e.getSource() == ViewLexButton)

{

log.setText("");

Filedata=StoreAsString("C:\\LexToken.txt");

log.append(Filedata);

log.append("LexToken.txt has been stored at C:\\LexToken.txt");

}

elseif (e.getSource() == ViewSymbolButton)

{

log.setText("");

Filedata=StoreAsString("C:\\SymbolT.txt");

log.append(Filedata);

log.append("SymbolT.txt has been stored at C:\\SymbolT.txt");

}

elseif (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. */

protectedstatic ImageIcon createImageIcon(String path)

{

java.net.URL imgURL = LA.class.getResource(path);

if (imgURL !=null){

returnnew ImageIcon(imgURL);

}else{

System.err.println("Couldn't find file: " + path);

returnnull;

}

}

privatestaticvoid 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);

}

publicstaticvoid main(String[] args)throws IOException

{

// javax.swing.SwingUtilities.invokeLater(new Runnable()

// {

// public void run()

// {

createAndShowGUI();

// }

// });

}

}

[33981 byte] By [Carbila] at [2007-11-27 6:15:34]
# 1
try commenting out your LA (mark-=BAD_NAMING_PENALTY) constructor and running... then uncomment it line by line. Welcome to life before debuggers.
corlettka at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 2
i'll give it a try
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 3
And dude, that code is seriously shocking. I was gonna try and see if I could find the problem by eyeball, but it's just too much of a mess.You're gonna need to spend a couple of hours tidying up if you want good marks for this.
corlettka at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 4
right now i just want it to work i'm not really worried about how it looks, as if i can't get it running i'll get NO MARKS at all., so till then looks has to take a back seat
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 5
This isn't something as simple as a classpath not being set properly, is it? The reason I ask, is that your code works on my system as written.
petes1234a at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 6
when you click the analyze lexically button does it frees?first open a .java file with the open file buttonthen click the analyze lexically buttonif it freezes then same problem if it doesn't freezethen tell me why?
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 7
yep, it freezes if trying to analyze even a small simple java file.
petes1234a at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 8

I started to look at that code and saw that massive if statement and decided this was far too messy for me to waste my time on. That being said, I still have some recommendations.

First, I have a question: How are you debugging this? What are you doing to try to identify where your hang-up is occurring? Have you tried anything yourself yet, or is this your first attempt?

Second, my recommendation would be to add a few System.out.println() statements in that mess of for/while loops that you have to see how far into them you're getting and when/if you're ever exiting them. Once you don't get to one of those print statements, then you will know where you're buggered.

So why don't you take a little time and try to narrow your code down to a block or two to see where you're problems are coming from. Then you can come back and give us a place to look, because - quite frankly - no one here is going to want to sift through that mess of code.

Good luck.

Navy_Codera at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 9

I found one instance of = where == is needed:

while (state = false)

not relevant to the freeze tho (yet).

the infinite loop is somewhere in here:

while ((z >= 1) && (x <= characters.length))

{

......

}

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 10
I don't see that you are ever decrementing z or incrementing x!x always == 0z always == 1This is an infinite loop.Message was edited by: petes1234
petes1234a at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 11
ok thanks that helps a bit now let me change that and then try to get that loop problem fixed
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 12
z increment with the z++; in the if in the for loop in the while and x increments in the 1st for loop in the while
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 13
oh and the z increments once before the while loop
Carbila at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 14

> right now i just want it to work i'm not really

> worried about how it looks, as if i can't get it

> running i'll get NO MARKS at all., so till then looks

> has to take a back seat

I'm afraid that this attitude is part of your problem. You have ugly code, code that is not tested as it is added and that is difficult to read. This will have errors and then be very hard to debug.

petes1234a at 2007-7-12 17:26:21 > top of Java-index,Java Essentials,Java Programming...
# 15
> z increment with the z++; in the if in the for loop> in the while and x increments in the 1st for loop in> the whileputting in a System.out.println... will prove you wrong. x and z never change.
petes1234a at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 16

> wordarray.get(0+z-1)=="+"

1. equals !!!!!! not ==

2. WTF is 0+z if it's not z ?

3. get wordarray(z-1) into a string instead of repeating

4. break up that freakin if statement... you get -10 from for the whole program for that, ESPECIALLY if it works... it's not maintainable, WHICH IS A LOT WORSE!

corlettka at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 17
> oh and the z increments once before the while loopa hll of a lot of good that does within the while loop!
petes1234a at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 18
yeah i know the code is ugly, and theres no real excuse for that.but now i'm stuck as i gotta get this bad boy working somehowi appreciate the help and as i said in my first post apologize for the messy code
Carbila at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 19

okay thanks. change the stupid mistake wordarray(0+z-1) to a string and

removed the 0. -Man I feel stupid for that.

Put in the system.out print line in the nested fors of x and y and x=0 and y=0 then nothing happens so the freeze point is located

Thanks again for that. now to try get this infinite loop to stop

Carbila at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 20

hooray got the freezing stopped by removing the one while loop on line 242

got y incrementing definately

got z staying at 1 - no hooray there

and x seems to be stuck at 1 too due to error on end of loop i think - no hooray there either

the error it kicks out in the console is : java.lang.IndexOutOfBounds Exception. so clearly the loop is going beyond it's limit or the if

oh well more searching for me

Carbila at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...
# 21

When you're testing the value of a boolean variable, all you need is the variable itself: while (booleanVariable)

if (!booleanVariable)

Adding '== true' or '== false' clutters up your code and creates the possibility that you'll leave out one of the '=' characters, thereby completely changing the meaning of expression. I count six places where you did just that.

Also, didn't your instructor tell you?

NEVER USE == TO COMPARE STRINGS!!!

I know both of those points have been made before, but you didn't specifically acknowledge them, and there's no point going forward until those problems are addressed. (And please don't anyone bring up the cases when it's okay to use == with strings; the OP doesn't need that complication right now. ^_^)

Also, your regexes are wrong. I think the first one is supposed to be "\\W+" and the second, "(?i)[a-z]\\w*". And in the patmatch() method, use matches(), not find().

uncle_alicea at 2007-7-21 21:48:47 > top of Java-index,Java Essentials,Java Programming...