Difference between revisions of "Debugging Practice"

From CompSciWiki
Jump to: navigation, search
Line 8: Line 8:
 
public class WordReversal  
 
public class WordReversal  
 
{
 
{
     public static void main( String[] args )
+
     public static void main( String[] args )  
 
     {
 
     {
 
         int lastSpace = 0;
 
         int lastSpace = 0;
Line 15: Line 15:
 
         String reversedWord = "";
 
         String reversedWord = "";
 
         String currWord = "";
 
         String currWord = "";
       
+
 
 
         String input = JOptionPane.showInputDialog( "Enter a sentence:" );
 
         String input = JOptionPane.showInputDialog( "Enter a sentence:" );
         while( nextSpace >= 0 )
+
         while( nextSpace >= 0 )  
 
         {
 
         {
 
             nextSpace = input.indexOf( ' ', lastSpace );
 
             nextSpace = input.indexOf( ' ', lastSpace );
           
+
 
             if( nextSpace != -1 )
+
             if( nextSpace != -1 )  
 
             {
 
             {
                 currWord = input.substring( lastSpace, nextSpace );
+
                 currWord = input.substring( lastSpace, nextSpace + 1 );
               
+
            }
                 for( int j = 0; j < currWord.length(); j++ )  
+
            else
                {
+
            {
                    reversedWord = reversedWord + currWord.charAt( j );
+
                 currWord = input.substring( lastSpace, input.length() );
                }
+
            }
 +
 
 +
            for( int j = currWord.length() - 1; j >= 0; j-- )  
 +
            {
 +
                reversedWord = reversedWord + currWord.charAt( j );
 
             }
 
             }
           
+
 
 
             result = result + reversedWord;
 
             result = result + reversedWord;
 
         }
 
         }
       
+
 
         JOptionPane.showMessageDialog( null, result, "Reversed Words",  
+
         JOptionPane.showMessageDialog( null, result, "Reversed Words",
            JOptionPane.INFORMATION_MESSAGE );
+
                JOptionPane.INFORMATION_MESSAGE );
 
     }
 
     }
 
}
 
}

Revision as of 12:26, 6 April 2010

Back to the Program-A-Day homepage

Problem

Find the bugs in the code provided below. Hint: there are lots

import javax.swing.JOptionPane;

public class WordReversal 
{
    public static void main( String[] args ) 
    {
        int lastSpace = 0;
        int nextSpace = 0;
        String result = "";
        String reversedWord = "";
        String currWord = "";

        String input = JOptionPane.showInputDialog( "Enter a sentence:" );
        while( nextSpace >= 0 ) 
        {
            nextSpace = input.indexOf( ' ', lastSpace );

            if( nextSpace != -1 ) 
            {
                currWord = input.substring( lastSpace, nextSpace + 1 );
            } 
            else 
            {
                currWord = input.substring( lastSpace, input.length() );
            }

            for( int j = currWord.length() - 1; j >= 0; j-- ) 
            {
                reversedWord = reversedWord + currWord.charAt( j );
            }

            result = result + reversedWord;
        }

        JOptionPane.showMessageDialog( null, result, "Reversed Words",
                JOptionPane.INFORMATION_MESSAGE );
    }
}
 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution...

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage