Programming problem (1 Viewer)

Users Who Are Viewing This Thread (Users: 0, Guests: 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
 

chroot

Staff Emeritus
Science Advisor
Gold Member
10,097
33
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 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.
 
well, i guess.. im sorry im 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?
 
96
0
Of course there is,
You can just look at the source code, no algorithms flow, only objects are being instantiated. :-D
 

The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top