Solving a Programming Problem - Adam

  • Thread starter Thread starter Pearce_09
  • Start date Start date
  • Tags Tags
    Programming
AI Thread Summary
The discussion revolves around a programming issue related to a method called `findAndReplace`, which is intended to replace certain words in a string array with new words. The user is struggling with the implementation of this method, leading to a null output that causes the program to terminate prematurely. The expected output is a complete sentence, but due to the lack of implementation in the `findAndReplace` function, the program fails to produce the desired result. Participants in the discussion point out that the null return value from the function is the root cause of the issue, suggesting that the user needs to implement the logic for replacing the words within the method. Additionally, there is clarification on the use of the loop that prints the result, emphasizing that the initial condition of the loop is important for proper execution. Overall, the conversation highlights the need for a functional implementation of the `findAndReplace` method to avoid null returns and achieve the desired output.
Pearce_09
Messages
71
Reaction score
0
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 I am 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 don't 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
 
Technology news on Phys.org
Is the code beginning with "for (int i = 0...)" supposed to follow the code "if ( result == null ) {..." or what?

- Warren
 
'if' is meaningless, everything was so clear.
adam said:
for ( int i=0; i<result.length; i++ ) {
if ( i > 0 ) {
System.out.print( " " );
}
System.out.print( result[ i ] );
}
System.out.println( "." );
}
}
 
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 that's 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.
 
well, i guess.. I am sorry I am not sure what your saying..but thanks anyways
 
Also the if in the for is not meaningless.
i is always larger than 0;.
 
Last edited:
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.
 
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?
 
Of course there is,
You can just look at the source code, no algorithms flow, only objects are being instantiated. :-D
 
Back
Top