Solving a Programming Problem - Adam

  • Thread starter Pearce_09
  • Start date
  • Tags
    Programming
In summary, the programmer is having trouble with their program and is asking for help. They explain that they are not familiar with programming, and asks for help finding and replacing strings. They also ask for help understanding whether it is cross or not. The code that is being discussed is unfinished, and the function is being discussed.
  • #1
Pearce_09
74
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
  • #2
Is the code beginning with "for (int i = 0...)" supposed to follow the code "if ( result == null ) {..." or what?

- Warren
 
  • #3
'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( "." );
}
}
 
  • #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 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.
 
  • #5
well, i guess.. I am sorry I am not sure what your saying..but thanks anyways
 
  • #6
Also the if in the for is not meaningless.
i is always larger than 0;.
 
Last edited:
  • #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.
 
  • #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?
 
  • #9
Of course there is,
You can just look at the source code, no algorithms flow, only objects are being instantiated. :-D
 

1. What is the first step in solving a programming problem?

The first step in solving a programming problem is to clearly define the problem and understand what is being asked. This involves breaking down the problem into smaller, more manageable tasks and identifying any potential constraints or limitations.

2. How do you approach a complex programming problem?

When faced with a complex programming problem, it is important to start by breaking it down into smaller, more manageable tasks. This allows for a step-by-step approach and makes the problem easier to understand. Additionally, it may be helpful to brainstorm potential solutions and to seek out resources or advice from other programmers.

3. What is the importance of testing and debugging in solving a programming problem?

Testing and debugging are essential steps in solving a programming problem. Testing involves running the code and checking for any errors or unexpected outcomes. Debugging is the process of identifying and fixing any errors or bugs in the code. Both of these steps help to ensure that the program runs smoothly and produces the desired results.

4. How do you know when a programming problem has been successfully solved?

A programming problem can be considered successfully solved when the desired results have been achieved and the program runs without any errors or bugs. It is important to thoroughly test the program and ensure that it meets all of the requirements before considering it solved.

5. What are some strategies for troubleshooting when facing a difficult programming problem?

When facing a difficult programming problem, it can be helpful to step away and take a break to clear your mind. Sometimes, looking at the problem with fresh eyes can lead to a new perspective. Additionally, seeking out help from other programmers or consulting online resources can provide valuable insights and solutions.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
619
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top