Results 1 to 4 of 4
  1. #1
    jawa is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default output shows null !!!!

    hi all
    can anyone tell me whats wrong with the code... the output gives only null rather than the supposed output

    Java Code:
    public class FDebugEight4
    {
       public static void main(String[] args)
       {
          int x = 0, y;
          String[] array = new String[100];
          String entry;
          final String STOP = "XXX";
          StringBuffer message = new
              StringBuffer("The words in reverse order are\n");
         
          entry = JOptionPane.showInputDialog(null,		
            "Enter any word\n" +
            "Enter " + STOP + " when you want to stop"); 
          while(!(entry.equals(STOP))) 	//keep asking for word till XXX is inputed
    		      {
             array[x] = entry;	
    			x++;						
    			entry = JOptionPane.showInputDialog(null,
                "Enter another word\n" +
                "Enter " + STOP + " when you want to stop"); 
    				
          }
          for( y = x; y > 0 ; --y)
          {
             message.append(array[y]);
             message.append("\n");
          }
          JOptionPane.showMessageDialog(null, message);
       }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Aash96 is offline Member
    Join Date
    Dec 2012
    Posts
    26
    Rep Power
    0

    Default Re: output shows null !!!!

    Where does it say null?

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: output shows null !!!!

    When the user clicks the cancel button, you get a null. So all you have to do is to check to see if entry == null.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class FDebugEight4
    {
       public static void main(String[] args)
       {
          int x = 0, y;
          String[] array = new String[100];
          String entry;
          final String STOP = "XXX";
          StringBuffer message = new
              StringBuffer("The words in reverse order are\n");
          
          entry = JOptionPane.showInputDialog(null,     
            "Enter any word\n" +
            "Enter " + STOP + " when you want to stop");
          while(entry != null && !(entry.equals(STOP)))  //keep asking for word till XXX is inputed
          {
             array[x] = entry;  
                x++;                        
                entry = JOptionPane.showInputDialog(null,
                "Enter another word\n" +
                "Enter " + STOP + " when you want to stop"); 
                     
          }
          for( y = x; y > 0 ; --y)
          {
             message.append(array[y]);
             message.append("\n");
          }
          JOptionPane.showMessageDialog(null, message);
       }
    }

Similar Threads

  1. Unwanted Null output.
    By steinarsen in forum New To Java
    Replies: 8
    Last Post: 11-08-2012, 10:01 AM
  2. I am created this program.but it shows null pointer exception...can any one help me..
    By vetrivelmanian in forum Threads and Synchronization
    Replies: 3
    Last Post: 07-26-2011, 11:30 AM
  3. Replies: 3
    Last Post: 07-09-2011, 01:21 PM
  4. output shows undesired results(advice ?)
    By jasperFernandes in forum New To Java
    Replies: 0
    Last Post: 03-10-2011, 02:32 AM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •