aspose file tools*
The moose likes Java in General and the fly likes  Help me Debug the Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Help me Debug the Code " Watch " Help me Debug the Code " New topic
Author

Help me Debug the Code

bapi dhar
Ranch Hand

Joined: Mar 02, 2001
Posts: 32
Dear Friend
Please Help me Debug the code.I have a Clock.java in current directory which contains the methods getHours,getminutes etc.
i m trying to sort clock objects in ascending order while implementing the Comparator interface.
thanx
bapi


[This message has been edited by bapi dhar (edited March 13, 2001).]
pl use the [ code ] tags without the spaces when posting source code, please!
- satya

[This message has been edited by Madhav Lakkapragada (edited March 14, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I found the following issues with your code:

  • In order to use BufferedReader, you will have to also import java.io.*;
  • In your compare method you have cast o1 to a Clock object only for its first use, and you have never cast o2 to a Clock object.
  • In the compare method, you are comparing c2 to its self every other line. I think you intended to compare c2 to c1.
  • In your getTime method, you are making use of a BufferedReader that is undefined.
  • In your getTime method, you are catching Throwable, which is acceptable, but I personally would catch IOException, since that is what will be thrown from your try block.
  • Finally, you have not included a definition for the Clock class. Obviously this will be key, and I am sure you have that coded and just didn't include it in your post.

  • I would try it like this:
    <PRE>
    import java.util.*;
    import java.io.*;
    class ClockComparator implements Comparator
    {
    public int compare( Object o1, Object o2 )
    {
    Clock c1, c2;
    c1 = (Clock)o1; // cast back to Clock
    c2 = (Clock)o2; // cast back to Clock
    if( c1.getHours() > c2.getHours() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getHours() > c1.getHours() )
    {
    return -1; // Clock two is greater
    }
    // Hours are equal, compare minutes
    else if ( c1.getMinutes() > c2.getMinutes() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getMinutes() > c1.getMinutes() )
    {
    return -1; // Clock two is greater
    }
    // Minutes are equal, compare seconds
    else if ( c1.getSeconds() > c2.getSeconds() )
    {
    return 1; // Clock one is greater
    }
    else if ( c2.getSeconds() > c1.getSeconds() )
    {
    return -1; // Clock two is greater
    }
    // Clocks are equal
    else
    {
    return 0;
    }
    }
    }

    public class Sort
    {
    static Clock clock[];
    static Object temp;
    public static void sort( Object[] c, ClockComparator cc )
    {
    for ( int i = 0; i < c.length; i++ )
    {
    for( int j = i + 1; j < c.length; j++ )
    {
    if ( cc.compare( c[j], c[i] ) < 1 )
    {
    temp = c[j];
    c[j] = c[i];
    c[i] = temp;
    }
    }
    }
    }

    public static void main( String args[] )
    {
    if ( args.length != 1 )
    {
    System.out.println( "Please include number of Clock times in Command Line ... Thank You" );
    System.exit( 0 );
    }
    int nc = Integer.parseInt( args[0] );
    clock = new Clock[nc];
    for ( int i = 0; i < nc; i++ )
    {
    System.out.println( "Enter Clock " + ( i + 1 ) );
    clock[i] = getTime( String.valueOf( i + 1 ) );
    }
    sort( clock, new ClockComparator() );
    } // main
    private static Clock getTime( String timeType )
    {
    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    Clock t;
    while( true )
    {
    System.out.print( "Enter the " + timeType + " time in HH:MM:SS, 24-hr format: " );
    try
    {
    t = Clock.valueOf( br.readLine() );
    break;
    }
    catch ( IOException e )
    {
    System.out.println( e );
    System.out.println( "Problem, try again!" );
    }
    } // while
    System.out.println( "The " + timeType + " time is " + t );
    return t;
    }
    } // class
    </PRE>
    HTH
    [This message has been edited by Bodie Minster (edited March 14, 2001).]
bapi dhar
Ranch Hand

Joined: Mar 02, 2001
Posts: 32
thanx
bapi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me Debug the Code