水晶石建筑教程:what's wrong with my java program?

来源:百度文库 编辑:高校问答 时间:2024/04/25 00:57:57
import java.io.*;
public class chapter4{
public static void main(String args[])
{
char c = ' ';
int index = 0;
int i = 0;
StackArray[] stack = new StackArray[20];
boolean condition = true;
System.out.println("please input one sentence end with '#':");
while (condition) {
try {
c = (char) System.in.read();

}
catch (IOException e) {}
if (c >= 'A' && c <= 'Z' && c >= 'a' && c <= 'z')
stack[index].push(c);
if (c == ' ')
index++;
if (c == '#')
condition = false;
}
for (i = 0; i <= index; i++)
stack[i].print();
}
}
class StackArray
{
int maxSize=20;
char[] aStack=new char[maxSize];
int top=-1;
public void push(char c)
{
if (top >= maxSize)
System.out.println("this word is too long!!");
else {
top++;
aStack[top] = c;
}
}
public void print()
{
for(int i=top;i>=0;i++)
System.out.print(aStack[i]);
System.out.print(" ");
}
}
i can compile this program successfully,but when i execute it,i was told that "exception in thread"main" java.lang.nullpointerException at chapter4.main<chapter4.java:25>. any ideas? thanks in advance!
this is an error in the last 3 sentence.it should be for(int i=top;i>=0;i--),not for(int i=top;i>=0;i++).i am so sorry!

调试后的,自己对照下吧,给不给分无所谓了,吗的再也不看别人写的程序了,太遭罪了,既然看了搞不好,觉得自己水平不行,搞完了发现不是自己水平不行,就是不能看别人代码!死也不看了!

import java.io.*;
public class chapter4{
public static void main(String args[])
{
char c = ' ';
int index = 0;
int i = 0;
StackArray stack = new StackArray();
boolean condition = true;
System.out.println("please input one sentence end with '#':");
//if (condition==true) System.out.println("1" );
while (condition) {
try {
c = (char) System.in.read();
//System.out.println("1" +c);
}
catch (IOException e) {}
if (c >= 'A' && c <= 'Z')
//System.out.println("1" +c);
stack.push(c);
//System.out.println("11111" +c);
if (c == ' ')
index++;
else if (c == '#')
condition = false;
}
for (i = 0; i <= index; i++)
//System.out.println("!!!!!!!!");
stack.print();
//System.out.println("!2222222");
}
}
class StackArray
{
int maxSize=20;
char[] aStack=new char[maxSize];
int top=0;
public void push(char c)
{
if (top >= maxSize)
System.out.println("this word is too long!!");
else {
top++;
//System.out.println("________________"+top);
aStack[top]=c;
//System.out.println("________________"+aStack[top]);
}
}
public void print()
{
//System.out.println(top);
for(int i=0;i<top;i++)
{System.out.print(aStack[i]);
System.out.print(" ");}
}
}

I once met your problem in a java program, I don't know what to do.
Maybe lack a header file.