In my Java class, we're holding a bit of a competition.
In this particular program, I need to have the user input a string, a search string, and a replacement string such that the program searches the input string for the search string, and when it finds the search string, it replaces it with the replacement string.
Here's what I have so far.
import hsa.Stdin;
public class B1
{
public static void main (String[] args)
{
System.out.print ("Input String: ");
String input = Stdin.readLine ();
System.out.print ("Search string: ");
String search = Stdin.readLine ();
System.out.print ("Replacement string: ");
String replace = Stdin.readLine ();
String[] inp2 = {input};
String[] replace2 = {replace};
for (int i = 0 ; i < input.length () ; i++)
{
if (!(input.charAt (i) == search.charAt (i)))
inp2 [i] = replace2 [i];
System.out.print (inp2 [i]);
}
}
}
Now, here's the problem: I have no clue how to get this to work correctly. I think I'm on the right track, just not there yet!
Here's my output:
Input String: Josh Baker
Search string: osh
Replacement string: ohn
ohnjava.lang.ArrayIndexOutOfBoundsException: 1
at B1.main(B1.java:18)
Can anyone help? :/
Name:
Anonymous2007-02-06 15:33
>Now, here's the problem: I have no clue how to get this to work correctly.
That much is obvious.
>I think I'm on the right track, just not there yet!
You're not.
Now, instead of writing code at random go back and first write a solution in pseudocode. Skipping the pseudocode step is only allowed for EXPERT PROGRAMMERS. Here's an example of how you might replace the *first* occurrence of a string.
To replace the first occurrence of X with Y in Z, take all characters from Z before the first occurrence of X in Z, append Y, and then append all characters from Z after the first occurrence of X in Z.
Name:
Anonymous2007-02-06 16:03
Listen, I thank you for the advice, but that's not why I asked. I have attempted to do everything in my knowledge to fix this, and nothing has worked. Therefore, you telling me EXACTLY what I've been trying to do did nothing. I'm sure that you know what the problem is, and I'm only asking for the solution. I'd like to at least have a decent hint or two at it.
Name:
Anonymous2007-02-06 17:10
import hsa.Stdin;
This is not standard, it's crap, don't use it.
Name:
Anonymous2007-02-06 17:15
>>3
Okay, your solution is completely retarded. As of right now it isn't even close, as it assumes the search and replace strings are as long or longer than the input string (which is why you get the out of bounds error). Any time the input string character at i doesn't match the search string at i, it uses the character of replace at i (even stupider).
WHAT YOU SHOULD BE DOING IS
Loop through input until input.charAt(i) matches search.charAt(0). Then you do another loop for the length of search (lets say you use the loop variable j), to see if input.charAt(i+j) == search.charAt(j). If this is true, THEN you can load replace2 into inp2, where inp2 from [i] to [i + replace length - 1] will equal replace from [0] to [replace length - 1].
Name:
Anonymous2007-02-06 17:19
P.S. Because you were nowhere close to right, chances are someone else better than you is going to win the "contest", whatever the terms are. If you actually win, that means your whole class is a bunch of failures who will never be good programmers.
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy