ArrayIndexOutOfBounds异常,如何解决?有源程序

liuzhenjian 2006-04-16 07:22:55
mport java.io.*;
public class Scanner {
class OutArr{
String name;
String type;
OutArr(String n,String t){
name=n;
type=t;
}
OutArr(char n,char t){
name=String.valueOf(n);
type=String.valueOf(t);
}
OutArr(String n,char t){
name=n;
type=String.valueOf(t);
}
}
private static final String[] KEY_WORD = {
"enum",
"extern",
"float",
"for",
"goto",
"if",
"int",
"long",
"return",
"signed",
"static",
"switch",
"typedef",
"unsigned",
"void",
"while",
};
int lp=1,st=0;
int count=0;
char[] Inbuff=new char[240];
OutArr[] oa=new OutArr[1000];
String strToken;
FileReader fr;
FileWriter fw;
public Scanner(String filename) {
try{
fr =new FileReader(filename);
}catch(IOException e){
System.out.println("Error");
e.printStackTrace();
}
}
FileReader Get(FileReader f){
int k=1;
try{
do{
Inbuff[k]=(char)f.read();
k++;
}while(Inbuff[k]!=(char)-1 && k<239);
}catch(IOException e){
System.out.println("Error");
e.printStackTrace();
}
return f;
}
int nextChar(int a){
a++;
if(a==Inbuff.length){
Inbuff[0]=Inbuff[Inbuff.length-1];
fr=Get(fr);
a=0;
}
return a;
}
void readOneWord()
{
int len=Inbuff.length;
System.out.println(len+" "+lp);
while(Inbuff[lp]==' '||Inbuff[lp]=='\t'||Inbuff[lp]=='\n')
{
lp=nextChar(lp);
}
st=lp;
if(Character.isLetter(Inbuff[lp])){
lp=nextChar(lp);
}
strToken=new String(Inbuff,st,lp-st);
for(int i=0;i<16;i++)
if(strToken.equals(KEY_WORD[i])){
oa[count++]=new OutArr(strToken,"keyword");
break;
}
else if(i==15){
oa[count++]=new OutArr(strToken, "variable");
break;
}
if(Character.isDigit(Inbuff[lp])){
int st=lp;
boolean isFloat=false;
lp=nextChar(lp);
while(lp>0 ) {if(Inbuff[lp]=='.')
isFloat=true;
lp=nextChar(lp);
}
strToken=new String(Inbuff,st,lp-st);
if(isFloat)
oa[count++]=new OutArr(strToken, "float_digit");

else
oa[count++]=new OutArr(strToken, "int_digit");
}
if(Inbuff[lp]=='\"')
{char endc='\"';
lp=nextChar(lp);
int st=lp;
while(lp>0 ) lp=nextChar(lp);
}
strToken=new String(Inbuff,st,lp-st);
oa[count++]=new OutArr(strToken,"String");
switch(Inbuff[lp])
{
case ',':
lp=nextChar(lp);
oa[count++]=new OutArr(',','-');
break;
case ';':
lp=nextChar(lp);
oa[count++]=new OutArr(';','-');
break;
case ':':
lp++;
oa[count++]=new OutArr(':','-');
break;
case '{':
lp=nextChar(lp);
oa[count++]=new OutArr('{','-');
break;
case '}':
lp=nextChar(lp);
oa[count++]=new OutArr('}','-');
break;
case '[':
lp=nextChar(lp);
oa[count++]=new OutArr('[','-');
break;
case ']':
lp=nextChar(lp);
oa[count++]=new OutArr(']','-');
break;
case '(':
lp=nextChar(lp);
oa[count++]=new OutArr('(','-');
break;
case ')':
lp=nextChar(lp);
oa[count++]=new OutArr(')','-');
break;
case '+':
lp=nextChar(lp);
if(Inbuff[lp]=='+'){
oa[count++]=new OutArr("++",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('+','-');
}
break;
case '-':
lp=nextChar(lp);
if(Inbuff[lp]=='-'){
oa[count++]=new OutArr("--",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('-','-');
}
break;
case '*':
lp=nextChar(lp);
oa[count++]=new OutArr('*','-');
break;
case '/':
lp=nextChar(lp);
oa[count++]=new OutArr('/','-');
break;
case '%':
lp=nextChar(lp);
oa[count++]=new OutArr('%','-');
break;
case '=':
lp=nextChar(lp);
if(Inbuff[lp]=='='){
oa[count++]=new OutArr("==",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('=','-');
}
break;
case '>':
lp=nextChar(lp);
if(Inbuff[lp]=='='){
oa[count++]=new OutArr(">=",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('>','-');
}
break;
case '<':
lp=nextChar(lp);
if(Inbuff[lp]=='='){
oa[count++]=new OutArr("<=",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('<','-');
}
break;
case '!':
lp=nextChar(lp);
if(Inbuff[lp]=='='){
oa[count++]=new OutArr("!=",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('!','-');
}
break;
case '&':
lp=nextChar(lp);
if(Inbuff[lp]=='&'){
oa[count++]=new OutArr("&&",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('&','-');
}
break;
case '|':
lp=nextChar(lp);
if(Inbuff[lp]=='|'){
oa[count++]=new OutArr("||",'-');
lp=nextChar(lp);
}
else{
oa[count++]=new OutArr('|','-');
}
break;
default:
lp=nextChar(lp);}

}

void Out_result(){
try{
fw=new FileWriter("/liuzhenjian/a.txt");
PrintWriter output=new PrintWriter(fw);
for(int i=0;i<=count;i++)
output.println(oa[i].name+"\t"+oa[i].type+"\n");
count=0;
}catch(Exception e){
System.err.println("Error");
}
}
void Run(){
fr=Get(fr);
int k=0;
do{
readOneWord();
k++;
if(k==Inbuff.length)
k=0;
System.out.println(count);
if(count==999)
Out_result();
}while (Inbuff[k] !=(char) -1);
try{
Out_result();
fr.close();
fw.close();
}catch(IOException e){
System.out.println("Error");
e.printStackTrace();
}
}
public static void main(String[] args){
Scanner s=new Scanner("/liuzhenjian/a.txt");
s.Run();
}
}
...全文
570 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
凋零的老树 2006-04-17
数组越界 代码带多,你应该用一个小的数组做测试
  • 打赏
  • 举报
回复
zuoyangguang 2006-04-17
还是自己查吧,求人不如求己
  • 打赏
  • 举报
回复
powerman_lyj 2006-04-16
你是数组越界了,你给出的代码太长了,又没有异常的完整的信息,看起来太费劲了
  • 打赏
  • 举报
回复
liuzhenjian 2006-04-16
第87行for 循环中应为i=26
92行i=25
还有...........
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2006-04-16 07:22
社区公告
暂无公告