Please help with this....(thank you)

Hi. I am new to the Dev. Connection and new to Java Programming. In fact what I am posting is a homework assignment that I have put several hours into and I cannot get it to perform properly. I am not looking for someone to do it for me --- rather to give me some pointers on how I can find the error and fix the problem myself. I mean what good would it do me if you (the experienced programmer) solved the whole thing for me? I want to be a kick-ass programmer and I would never be any good if everyone else did my work.
Thanks in advance for any advice.
Program Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ReverseString
     Method Name: Main()
     Return Value: none
     Input Parameters: none
     Method Description: This is the method that makes it all go!
     public static void main( String args[] )
          String sentence = ""; //String variable to hold user input
          String output = ""; //String variable to enable display of output
          String buffer = "";//String variable to hold output
          //Display an input dialog
          sentence = JOptionPane.showInputDialog(
"Enter the Sentence you want to see in REVERSE:");
          //Pass String variable 'sentence' to StringTokenizer object
          StringTokenizer st = new StringTokenizer(sentence);
               //Array to hold tokens
               String tokenHolder[] = new String[st.countTokens()];
               //Walk through the tokens
               while (st.hasMoreTokens())
                    for (int i = 0; i < st.countTokens(); i++)
                         tokenHolder[i] = st.nextToken();
               String reversedTokens[] = new String[tokenHolder.length];
               //Walk through the array in reverse
               for (int j = tokenHolder.length - 1; j >= 0; j--)
                    //Add the reversed token to the ouptut string
                    output += tokenHolder[j] + " ";
          //Display Output
          JOptionPane.showMessageDialog (null, output,
          "Reversing the Contents of an Input String.",
          JOptionPane.INFORMATION_MESSAGE );
     //Exit cleanly
     System.exit( 0 );
     }//EOMain
}//EOF
The problem is that when I run this application the output does not yield a string that has had the tokens extracted from the user input in reverse. Instead for the input "this is a test" --I get "nullnull is test". Why?
Thanks again.

...but since you are learning, and would like to know where was the bug, I tried your code and updated it.
The only two small problems I found:
1. You have a while loop that checks if there are more tokens.. it's not needed and this while loop will only run because by the second run, you would have consumed all the tokens in the for loop, check would fail and while loop will be exited. [I have removed this while loop in my version below]
2. Now, here is the bug:your for loop has a condition that evaluates (i < st.countTokens()).
Now count tokens will only return "remaining" tokens. Assuming test string "this is a test", countTokens return 4. Then as you consume the first token, it is 3, then 2 then 1. All this while for loop's i is going 1,2...
i = 0, countTokens = 4
i = 1, countTokens = 3
i = 2, countTokens = 2, check fails, and for loop is exited.
So all you have in your array is first two tokens. Rest of the array is null as it was when you created the array.
So then when you loop back and check index 3,2,1,0.. 3 and 2 elements are still nill, that's why you get the result you are getting.
Here is the fixed code:
String sentence = "this is second test"; //String variable to hold user input
String output = ""; //String variable to enable display of output
String buffer = "";//String variable to hold output
st = new StringTokenizer(sentence);
int tokenCount = st.countTokens();
//Array to hold tokens
String tokenHolder[] = new String[tokenCount];
//Walk through the tokens
for (int i = 0; i < tokenCount; i++)
     tokenHolder[i] = st.nextToken();
String reversedTokens[] = new String[tokenCount];
//Walk through the array in reverse
for (int j = tokenCount-1; j >= 0; j--)
//Add the reversed token to the ouptut string
output += tokenHolder[j] + " ";
System.out.println(output);
}

Similar Messages

Maybe you are looking for

  • Can't create a print queue on OS 10.7 via HP driver installed by Apple

    I've ust bought a reconditioned iMac and am getting applications up and running, but I'm unable to get my HP Laserjet 2200DN printer working. The Add Printer pane (Print & Scan Preferences) remains blank: Name & Location and Print Using & Add areas a

  • Re install on XP PRO Windows Media Edition. SR2023WM

    My motherboard died. I replaced it with a HP Compaq motherboard with a Dual Core CPU. I thought that I might as well upgrade since I had to get a new motherboard . The first time it was turned on, it booted up, loaded Windows and went to my Desk Top,

  • Two ipods

    hi please can anyone help, i have two ipods and only one itunes, is there any way i can have two itunes librarys, as when i use the two ipods they lose everything.

  • How to automate backup in Max DB

    Hi All, I am using Maxdb.I would like to know is there any way to automate backup in maxdb(Like scheduling the backup at a perticular time) can any one of you help in this regard.I am using windows o/s and maxdb. Thanks in advance. Thanks Venu

  • Using log4j with JavaFX and Netbeans 6.8

    Hello, I'm trying to use log4j within a JavaFX project using NetBeans 6.8. I've donwloaded the .zip file from the Apache website and added the log4j-1.2.16.jar to the project's libraries. Then I added the imports: import org.apache.log4j.Logger; impo