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

Pages: 1-

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

Name: Anonymous 2007-02-06 17:10

import hsa.Stdin;

This is not standard, it's crap, don't use it.

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

Name: Anonymous 2007-02-07 7:29

Things you might find useful:

http://java.sun.com/j2se/1.5.0/docs/api/

Look for:
java.lang.String
java.lang.StringBuilder

Read, Understand, Think, and Learn.

Name: Anonymous 2007-02-07 12:16

Read, Understand, Think, and Learn.
By not using java.

Name: Anonymous 2007-02-08 2:51

>>8
ZING

Name: Anonymous 2007-02-08 3:29

>>1
java.util.regex

Name: Anonymous 2007-02-08 21:10

>>10

I admire your optimism wrt >>1, though I fear it may be misplaced.

Name: Anonymous 2007-02-08 21:26

Holtsoftware Standard In is useless, go use java.util.Scanner or something.  Honestly, the book is shit too.

Name: Anonymous 2007-02-08 22:11

Why don't you just use the replace method of the string class?

System.out.println( input.replace( search, replace ) );

Name: Anonymous 2007-02-09 3:48

One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-02-09 3:54

FUCK OFF FORCED INTENDATION MORONS

Name: Anonymous 2007-02-09 4:27

>>15
One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-02-09 4:53

>>15
YHBT

Name: Anonymous 2009-01-14 14:51

FAGGOT

Name: Anonymous 2010-06-25 14:50

WARNING: NECRO POST

Name: Anonymous 2011-01-31 21:34

<-- check em dubz

Name: tray 2012-03-15 16:15

hey ya all, guten tag!

Name: Sgt.Kabu뭎눌kiman銝ᝀ 2012-05-28 22:22

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

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