Problem with strings

Hi All,I want to write a program which accepts two strings and then removes all the occurrences of the second string in the main string. I don't know how to go about... Please help...
[205 byte] By [New_to_Javaa] at [2007-11-27 8:45:09]
# 1
Which bit don't you get?
ita6cgra at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 2
> ... I don't know how to go about...> > Please help...What do you mean? You don't know any Java? How can people help you then?
prometheuzza at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 3

I did not get the logic of how to compare the characters of two strings.

What i was thinking was I will convert both the strings into array and then compare each element of one array with the other but how to get the remaining characters in the main string.

will this be fine or there is some simpler method to do it without converting the strings to array..

New_to_Javaa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 4

> I did not get the logic of how to compare the

> characters of two strings.

>

> What i was thinking was I will convert both the

> strings into array and then compare each element of

> one array with the other but how to get the remaining

> characters in the main string.

>

> will this be fine or there is some simpler method to

> do it without converting the strings to array..

you could use the charAt(...) method from the String class, but toCharArray() will work ok too. Why not just give it a try yourself and when you get stuck you can ask a question here.

prometheuzza at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 5

This is what i had written but i could not figure out how the loops should be like..

public class Test_Compare

{

public static void main(String[] args)

{

String string1 = new String();

String string2 = new String();

String output = new String();

string1 = "simple";

string2 = "surge";

char[] array1 = string1.toCharArray();

char[] array2 = string2.toCharArray();

int len1 = string1.length();

int len2 = string2.length();

for (int i = 0; i < len1; i++)

{

for (int j = 0; j < len2; j++)

{

if (array2[j] == array1[i])

{

output = string1.replace(array2[j], ' ');

}

}

System.out.println(output);

}

}

}

I am getting an output like this:

imple

imple

imple

imple

imple

simpl

where am I going wrong ? and how should i correct it..

New_to_Javaa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 6

There are no occurrences of the string "surge" in the string "simple", right?

So the result should be the original string, unaltered, unless I am misunderstanding

your problem description. But your code simply compares one char from each

string and if they are equal, calls replace. Shouldn't you hold off on calling

replace until you know the entire second string is there?

And are you allowed to use other methods in the API? If so, this

algorithm could be one line long!

BigDaddyLoveHandlesa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 7
Your question is still too vague. anyway comparing strings should be done with equals sign not ==.
schumachera at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 8
> Your question is still too vague. anyway comparing> strings should be done with equals sign not ==.Comparing Strings should be done with the equals(String) method.
Djaunla at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 9
> Your question is still too vague. anyway comparing> strings should be done with equals sign not ==.He isn't doing that anywhere.
CaptainMorgan08a at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 10

> > Your question is still too vague. anyway comparing

> > strings should be done with equals sign not ==.

>

> Comparing Strings should be done with the

> equals(String) method.

In the given code, array2[j] == array1[i] is comparing chars...

BigDaddyLoveHandlesa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 11

> > > Your question is still too vague. anyway

> comparing

> > > strings should be done with equals sign not ==.

> >

> > Comparing Strings should be done with the

> > equals(String) method.

>

> In the given code, array2[j] == array1 is

> comparing chars...

I see this; I was just correcting the poster above me, as way too many people don't know how to compare Strings.

Message was edited by:

Djaunl

Djaunla at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 12

Hi All,

What I want to do is I want to remove all the charcaters from the string1 which are present in both the strings.

So I thought I will first compare both the strings and and find out which charcaters of string2 are present in the string1 and then remove those from the string1.

I am still confused how to do it.. Please help.. If my approach is wrong..

New_to_Javaa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 13

You have the basic logic already, just the "How-to" is missing. Maybe you can declare another variable to store the modified string1. Use methods such as indexOf, charAt to achieve that is easier. To simplify it,

Let say string1 = "abc" and string2 = "cdr"

Then, when you access the "c" in string1 and compare it to string2...

string1.charAt(i).indexOf(string2) > -1 inside a for loop, you just skip the character in string1, otherwise just append it to another variable, say string3.

Hope this can help you.

money_0422a at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 14

> What I want to do is I want to remove all the charcaters from the string1 which are present in both the strings.

Are you sure? In the original post you wrote:

> I want to write a program which accepts two strings and then removes all the occurrences of the second string in the main string.

These really are completely different!

BigDaddyLoveHandlesa at 2007-7-12 20:46:12 > top of Java-index,Java Essentials,New To Java...
# 15

> > What I want to do is I want to remove all the

> charcaters from the string1 which are present in both

> the strings.

>

> Are you sure? In the original post you wrote:

>

> > I want to write a program which accepts two strings

> and then removes all the occurrences of the second

> string in the main string.

>

> These really are completely different!

I think both mean the same, the only difference is earlier i had written that i want to accept two strings. otherthan that its means the same ..

I do not know how to proceed further..

New_to_Javaa at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 16

No, one means that if one string is "hello world" then you want to remove all occurances of the string "hello world" from another string.

The other statement is that you want to remove the occurances of all characters in "hello world from the other string. Meaning removing all 'h's, all 'e's, all 'l's, all 'w's, all 'o's,....... etc.

BIG difference!

petes1234a at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 17
Man, do I love regular expressions.But, since this is probably a homework assignment, never mind.By the way, that's not a very wise choice of a display name.
myncknma at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 18

Sorry my mistake,

But what I want to do is:

I want write a program to accept two strings and remove all the occurrences of the second string in the main string. I do not want to use any standard library functions.

Hopefully this should be clear and this is what i want to achieve out of my program, so what the best way to do it..

New_to_Javaa at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 19

> But what I want to do is:

>

> I want write a program to accept two strings and

> remove all the occurrences of the second string in

> the main string. I do not want to use any standard

> library functions.

I'm confused cuz your example code tries to do the opposite: remove each character in a string from another string.

You might do well to repost your code updated to try to match this new specification...

petes1234a at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 20
OK lets get this clear!Do you want to do:remove "cat" from "catch a bat" gives "tch a bat"or:remove "cat from "catch a bat" gives "h bt"
floundera at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 21
Let's say input string are string1 and string2String string1 = "problems";String string2 = "solution";then the output should be :string1 = "prbem";I donot want to use any standard library functions...
New_to_Javaa at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 22

> Let's say input string are string1 and string2

>

> String string1 = "problems";

> String string2 = "solution";

>

> then the output should be :

> string1 = "prbem";

That still doesn't say much.

So

problems -> solution = prbem

but what about:

Java -> a = Jva or Jv?

Racecar -> ca = Recar or Rer?

> I donot want to use any standard library functions...

But you were already using String's replace(...) method! What can, and what can't you use?

prometheuzza at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 23
If my string1 = "Java"And string2 = "a"then my output result should be "Jv"and if string1 = "Racecar "and string2 = "ca"then the output should be : Rerand initially i had used replace() , then i reailsed that i should not be using that...
New_to_Javaa at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...
# 24

> If my string1 = "Java"

> And string2 = "a"

> then my output result should be "Jv"

>

> and

>

> if string1 = "Racecar "

> and string2 = "ca"

> then the output should be : Rer

>

> and initially i had used replace() , then i reailsed

> that i should not be using that...

Ok, here's a pretty simple algorithm:

public class TestCompare

{

public static void main(String[] args)

{

String 'originalString' <- the original String

String 'removeString' <- the String that contains the chars to remove

String 'newString' <- the newly formed String

LOOP 'i' from 0 to 'originalString'.length

char 'candidate' <- the char from 'originalString' at index 'i'

boolean 'isInRemoveString' <- initialize as false

LOOP 'j' from 0 to 'removeString'.length

IF 'candidate' equals the char at index 'j' from 'removeString'

set 'isInRemoveString' to true and break out of this loop

END IF

END LOOP

IF 'isInRemoveString' equals false

append 'candidate' to 'newString'

END IF

END LOOP

print 'newString'

}

}

Try to finish it.

prometheuzza at 2007-7-21 22:47:01 > top of Java-index,Java Essentials,New To Java...