what's wrong with my program? help!!!!

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!
 
R

Roedy Green

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.
 
R

Remon van Vliet

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!


My idea is that a NullPointerException occurs at line 25 in your chapter4
class. So, stack.print is called when stack is null. You should really
just look at the error message given...
 
S

Srikanth

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.
 
S

Saj

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 = new StringBuffer(temp).reverse().toString();
}
StringBuffer sb = new StringBuffer();
for(int i = 0; i < temp.length; i++)
{
sb.append(temp + " ");
}
return sb.toString().trim();
}
}
 
G

Greg R. Broderick

(e-mail address removed) wrote in @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
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!



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.


--
---------------------------------------------------------------------
Greg R. Broderick [rot13] (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
M

mailtumengyu

many thanks,all of you,Roedy Green,Remon van
Vliet,Srikanth,Saj,especially Saj and Srikanth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top