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[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!
Roedy Green - 11 Apr 2006 06:28 GMT
>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".
there is reverse method. I can't recall just where. Look in
StringBuilder, String or regex.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Saj - 11 Apr 2006 10:51 GMT
dude just try this program it should work.......
import java.io.*;
public class chapter4
{
private static String line, reversed;
public static void main(String args[])
{
chapter4 ch = new chapter4();
System.out.print("Please input one sentence end with '#': ");
try{
line = ch.readLine();
}
catch(IOException ioe){}
reversed = ch.reverse();
System.out.println("The reversed string is: " + reversed);
}
private String readLine() throws IOException
{
char c;
StringBuffer sb = new StringBuffer();
while((c = (char)System.in.read()) != '#')
sb.append(c);
return sb.toString();
}
private String reverse()
{
String[] temp = line.split(" ");
for(int i = 0; i < temp.length; i++)
{
temp[i] = new StringBuffer(temp[i]).reverse().toString();
}
StringBuffer sb = new StringBuffer();
for(int i = 0; i < temp.length; i++)
{
sb.append(temp[i] + " ");
}
return sb.toString().trim();
}
}
mailtumengyu@gmail.com - 12 Apr 2006 05:54 GMT
thanks a million!
mailtumengyu@gmail.com - 12 Apr 2006 06:01 GMT
many thanks,all of you,Roedy Green,Remon van
Vliet,Srikanth,Saj,especially Saj and Srikanth.
Remon van Vliet - 11 Apr 2006 08:31 GMT
> when you input a sentence,this program will reverse every word to
> output your sentence,for example,when you input"tom and jenny",this
[quoted text clipped - 50 lines]
> told that "exception in thread"main" java.lang.nullpointerException at
> chapter4.main<chapter4.java:25>. any ideas? thanks in advance!
My idea is that a NullPointerException occurs at line 25 in your chapter4
class. So, stack[i].print is called when stack[i] is null. You should really
just look at the error message given...
Srikanth - 11 Apr 2006 10:16 GMT
The problem is
for (i = 0; i <= index; i++)
replace it with
for (i = 0; i < index; i++)
because index would be pointing to the next index always.
Greg R. Broderick - 11 Apr 2006 10:57 GMT
mailtumengyu@gmail.com wrote in news:1144730610.706833.33730
@e56g2000cwe.googlegroups.com:
> when you input a sentence,this program will reverse every word to
> output your sentence,for example,when you input"tom and jenny",this
[quoted text clipped - 50 lines]
> told that "exception in thread"main" java.lang.nullpointerException at
> chapter4.main<chapter4.java:25>. any ideas? thanks in advance!
StackArray[] stack = new StackArray[20];
creates an array of null object references to twenty StackArray objects. Two
issues:
1. are you sure that this is what you want to do? It would seem to me that
your intention is to create a single StackArray object with size 20, not an
array of 20 StackArray objects.
2. creating the array does not create any of the objects in the array, so
when you later execute
stack[index].push(c);
stack[index] is a null object reference, so you get a NullPointerException.
Another, related issue is that you seem to be maintaining the top-of-stack
pointer both within your StackArray class (the "top" field) and outside of
your StackArray class (the "index" local variable). I'd suggest spending
some time examining the code to java.util.Stack in your JDK to see how a
stack data structure should be implemented in Java.

Signature
---------------------------------------------------------------------
Greg R. Broderick [rot13] terto@oynpxubyvb.qlaqaf.bet
A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------