Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Argh, need help.

Name: Anonymous 2007-02-06 15:09

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: Anonymous 2007-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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List