Difference between revisions of "Debugging Practice"

From CompSciWiki
Jump to: navigation, search
Line 10: Line 10:
 
     public static void main( String[] args )  
 
     public static void main( String[] args )  
 
     {
 
     {
         int lastSpace = 0;
+
         int lastSpace = 0; //position of the last space we found
         int nextSpace = 0;
+
         int nextSpace = 0; //position of the next space
         String result = "";
+
         String result = ""; //final result
         String reversedWord = "";
+
         String reversedWord = ""; //temporarily holds a reversed word
         String currWord = "";
+
         String currWord = ""; //the current word we are reversing
  
 +
        //get input from the user
 
         String input = JOptionPane.showInputDialog( "Enter a sentence:" );
 
         String input = JOptionPane.showInputDialog( "Enter a sentence:" );
         while( nextSpace >= 0 )  
+
 
 +
        //keep looping until we no don't find a new space
 +
         while( nextSpace >= 0 )
 
         {
 
         {
 +
            //finds the next space in the sentence
 
             nextSpace = input.indexOf( ' ', lastSpace );
 
             nextSpace = input.indexOf( ' ', lastSpace );
  
 
             if( nextSpace != -1 )  
 
             if( nextSpace != -1 )  
 
             {
 
             {
 +
                //space was found, grab the word between the spaces
 
                 currWord = input.substring( lastSpace, nextSpace + 1 );
 
                 currWord = input.substring( lastSpace, nextSpace + 1 );
 
             }  
 
             }  
 
             else  
 
             else  
 
             {
 
             {
 +
                //no more space was found, just grab the rest of the sentence
 
                 currWord = input.substring( lastSpace, input.length() );
 
                 currWord = input.substring( lastSpace, input.length() );
 
             }
 
             }
  
 +
            //reverse the word
 
             for( int j = currWord.length() - 1; j >= 0; j-- )  
 
             for( int j = currWord.length() - 1; j >= 0; j-- )  
 
             {
 
             {
Line 35: Line 42:
 
             }
 
             }
  
 +
            //add the reversed word to the result so far
 
             result = result + reversedWord;
 
             result = result + reversedWord;
 
         }
 
         }
  
 +
        //output the result
 
         JOptionPane.showMessageDialog( null, result, "Reversed Words",
 
         JOptionPane.showMessageDialog( null, result, "Reversed Words",
 
                 JOptionPane.INFORMATION_MESSAGE );
 
                 JOptionPane.INFORMATION_MESSAGE );

Revision as of 00:15, 8 April 2010

Back to the Program-A-Day homepage

Problem

The code provided below was written by a student who was challenged to write a program that reversed every word in a provided sentence. After testing it successfully with a single word, the student was confident that the code would work for a full sentence. Unfortunately, that is not the case. Find all the bugs in the student's code, and fix them. Hint: There are three of them.

import javax.swing.JOptionPane;

public class WordReversal 
{
    public static void main( String[] args ) 
    {
        int lastSpace = 0; //position of the last space we found
        int nextSpace = 0; //position of the next space
        String result = ""; //final result
        String reversedWord = ""; //temporarily holds a reversed word
        String currWord = ""; //the current word we are reversing

        //get input from the user
        String input = JOptionPane.showInputDialog( "Enter a sentence:" );

        //keep looping until we no don't find a new space
        while( nextSpace >= 0 )
        {
            //finds the next space in the sentence
            nextSpace = input.indexOf( ' ', lastSpace );

            if( nextSpace != -1 ) 
            {
                //space was found, grab the word between the spaces
                currWord = input.substring( lastSpace, nextSpace + 1 );
            } 
            else 
            {
                //no more space was found, just grab the rest of the sentence
                currWord = input.substring( lastSpace, input.length() );
            }

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

            //add the reversed word to the result so far
            result = result + reversedWord;
        }

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

String Methods and Debugging

Wiki array02.jpg

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage