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? :/
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? :/