checking if string contains same chars
Hi
I am new to java, but have some trouble with it.
What is the easiest way to check if the specific string contains same characters and if so, then printing it out?
For example, if the string is "abcd", then the program prints nothing, but if the string is "abcab", then it prints "yes a, yes b". It should be done using functions string.length(),string.charAt(n).
Best regards
[411 byte] By [
wippa] at [2007-11-27 11:32:16]

What have you tried?
No one here will do your homework for you.
You already have some pretty good hints, use length() and charAt() to loop over each character.
dwga at 2007-7-29 16:44:32 >

What I have done is following:
public class charchecker {
public static void main(String[] args) {
String input = args[0];
int lenght=input.length();
for (int i=0; i<lenght; i++)
{
for (int j=i+1; j><pikkus; j++)
{
if ((input.charAt(i)).equals((input.charAt(j)))
{
System.out.println("yes "+input.charAt(i));
break;
}
}
}
}
}
However, it doesnt work, because it says that char cannot be dereferenced.
(this is wrong > if (input.charAt(i)).equals((input.charAt(j))
Futhermore, if the string for example "abcaxa", then it should print only "yes a", not "yes a, yes a". So what should I add?
wippa at 2007-7-29 16:44:32 >

> What I have done is following:
>
> > public class charchecker {
>public static void main(String[] args) {
> String input = args[0];
>int lenght=input.length();
>
> int i=0; i<lenght; i++)
>{
> (int j=i+1; j<pikkus; j++)
> {
>
> ((input.charAt(i)).equals((input.charAt(j)))
> {
> System.out.println("yes "+input.charAt(i));
> break;
> }
> }
> }
> }
> }
>
>
> However, it doesnt work, because it says that char
> cannot be dereferenced.
> (this is wrong > if
> (input.charAt(i)).equals((input.charAt(j))
>
> Futhermore, if the string for example "abcaxa", then
> it should print only "yes a", not "yes a, yes a". So
> what should I add?
j><pikkus
j><pikkus what is this mate !!!!!!!>
You need to learn about primitive types, Operators !!
Why we use .equals() for string OBJECT
Heck you need to learn from a b c d of java man.. most important - Don't use any IDEs
It should be lenght, not pikkus. Here is correct:
public class charchecker {
public static void main(String[] args) {
String input = args[0];
int lenght=input.length();
for (int i=0; i<lenght; i++)
{
for (int j=i+1; j><lenght; j++)
{
if (input.charAt(i)).equals((input.charAt(j)))
{
System.out.println("yes "+input.charAt(i));
break;
}
}
}
}
}
>
wippa at 2007-7-29 16:44:32 >

Duh!!!
What is >< mean man.....
Getting help here ll solve ur problem only today.. tomo u ll again get another similar doubt.. Learn the basics from the books
> What is >< mean man.....
A bug in the forum software.
> tomo u ll
Would you kindly learn to spell as well?
Ok, but I have another question:
I have a string str="abcde" and a char=xx taken from that string. I want to make a new string from the old one, where this character is removed. I would use the replace method to replace the char xx, but it doesn't work.
...
char xx=str.charAt(i);
newstring=str.replace(xx,"");
...
wippa at 2007-7-29 16:44:32 >

> ...
> char xx=str.charAt(i);
> newstring=str.replace(xx,"");
> ...
There are two replace(...) methods:
replace(char oldChar, char newChar)
// and
replace(CharSequence target, CharSequence replacement)
You are trying to call:
replace(char target, CharSequence replacement)
See the difference?
Note: a String is a CharSequence.