M
mailtumengyu
when you input a sentence,this program will reverse every word to
output your sentence,for example,when you input"tom and jenny",this
program will output"mot dna ynnej".
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.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);
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!
output your sentence,for example,when you input"tom and jenny",this
program will output"mot dna ynnej".
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.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);
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!