Solving a Programming Problem - Adam

  • Thread starter Thread starter Pearce_09
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to implementing a method in Java that replaces specific strings in an array. Participants explore the issues surrounding the implementation of the `findAndReplace` function and the handling of null values in the program's output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Adam expresses uncertainty about how to prevent the program from exiting when the `result` is null, indicating a need for help with the implementation of the `findAndReplace` method.
  • Warren questions whether the `for` loop should follow the null check for `result`, suggesting a potential misunderstanding of the code structure.
  • Another participant asserts that the `if` statement is not meaningless and emphasizes that the loop's behavior depends on the value of `result`.
  • One participant suggests that the null return from `findAndReplace` indicates that the function lacks an implementation, prompting Adam to check that section of the code.
  • There is a discussion about the initialization of the loop variable `i`, with some participants debating its placement and initial value.
  • Another participant humorously comments on the overall structure of the code, indicating that it lacks proper algorithms and flow.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the effectiveness of the current implementation or the meaning of the `if` statement. Multiple competing views exist regarding the handling of the null value and the structure of the code.

Contextual Notes

There are unresolved questions about the implementation details of the `findAndReplace` function, which may be contributing to the null return value. The discussion reflects varying levels of understanding among participants regarding the Java programming concepts involved.

Who May Find This Useful

Individuals interested in Java programming, particularly those facing challenges with string manipulation and method implementation in coding exercises.

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 (because 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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K