Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming problem

  1. Jan 10, 2006 #1
    Programming problem!!

    hello,
    This might be a simple answer, but I'm not sure because I am very un-familiar with programming.
    also I am not sure if this can be answered (becuase im not giving enough information)
    __________________________________________________________
    public static String[] findAndReplace( String[] in, String[] what, String[] with ) {

    // Implement this method.

    return null;

    public static void main( String[] args ) {

    displayStudentInfo();

    String[] text;
    text = new String[ 10 ];

    text[ 0 ] = new String( "I" );
    text[ 1 ] = new String( "have" );
    text[ 2 ] = new String( "never" );
    text[ 3 ] = new String( "let" );
    text[ 4 ] = new String( "my" );
    text[ 5 ] = new String( "schooling" );
    text[ 6 ] = new String( "interfere" );
    text[ 7 ] = new String( "with" );
    text[ 8 ] = new String( "my" );
    text[ 9 ] = new String( "education" );

    String[] query;
    query = new String[ 3 ];

    query[ 0 ] = text[ 0 ];
    query[ 1 ] = text[ 1 ];
    query[ 2 ] = text[ 4 ];

    String[] replacement;
    replacement = new String[ 3 ];

    replacement[ 0 ] = new String( "You" );
    replacement[ 1 ] = new String( "should" );
    replacement[ 2 ] = new String( "your" );

    String[] result;
    result = findAndReplace( text, query, replacement );

    if ( result == null ) {
    System.err.println( "findAndReplace should not return a null value" );
    System.exit( 1 );
    }
    ___________________________________

    I am having trouble with my program right here, I dont know what I have to put so the programm will continue and not exit every time.... Here is the rest of the program... its unfinished..

    // We now know that result cannot be null

    for ( int i=0; i<result.length; i++ ) {
    if ( i > 0 ) {
    System.out.print( " " );
    }
    System.out.print( result[ i ] );
    }
    System.out.println( "." );
    }
    }
    _________________________________
    my output needs to be "You should never let your schooling interfere with your education"..... I just need help getting past the "System.exit(1)..
    thank you for your time..
    adam
     
  2. jcsd
  3. Jan 10, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Is the code beginning with "for (int i = 0...)" supposed to follow the code "if ( result == null ) {..." or what?

    - Warren
     
  4. Jan 10, 2006 #3
    'if' is meaningless, everything was so clear.
     
  5. Jan 10, 2006 #4
    if your program doesn't continue beyond the if(result==null) then it is the way you calculate "result" the string is null, your function is returning null..attempt to display before the iff statement...therefore always terminating..Looking back at that FindAndReplace Function...do you actually have implementation in the part thats comment //implementation?
    if not then your returning null..therefore in the main it'll retreive a null.


    Also the if in the for is not meaningless.
     
  6. Jan 10, 2006 #5
    well, i guess.. im sorry im not sure what your saying..but thanks anyways
     
  7. Jan 10, 2006 #6
    i is always larger than 0;.
     
    Last edited: Jan 10, 2006
  8. Jan 10, 2006 #7
    That is an interesting program, you can use one string holder to store the string, it might be better in this case.

    --------------------------sinatuer----------------------------------
    1)Stop wearin so many coats to invite more audience to that restroom
    2)About whether it is cross or not, I still don't know but its possibility is high(75%). Marriage and high reproductiveness are not signs to prove oneself not being cross.
     
  9. Jan 10, 2006 #8
    NafiBear: i is equal to 0 on the initial run. It would have been better to leave it above the for loop and change the initial condition, i would agree with you.

    Pearce: is there actuallyh code inside that function Find&Replace?
     
  10. Jan 11, 2006 #9
    Of course there is,
    You can just look at the source code, no algorithms flow, only objects are being instantiated. :-D
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Programming problem
Loading...