Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
mailtumengyu@gmail.com - 11 Apr 2006 05:43 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".
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?
---------------------------------------------------------------------



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.