English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to make a method that will scan for any symbol in a string, but I tried to scan using the string "what" and it returns n
why is that?

private char scanSymbol (String s)
{
String symbols = "`~!@#$%^&*()-_+<>,./'?";
char symbol = ' ';
for (int i = 0; i<= s.length()-1; i++)
{
{
if (s.indexOf(s.charAt(i)) != -1)
symbol = s.charAt(i);
}
}
return symbol;
}

2006-10-29 12:52:28 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

It is returning a garbage value, your function is completely wrong for what you are trying to do.

Also what it will do is, even if it finds a symbol if the function was correct, it will keep on loop until the length of the string and keep replacing symbol. you need to put return statement inside the for loop or a break statement if the symbol was found.

2006-10-29 12:58:06 · answer #1 · answered by Manish 5 · 0 0

fedest.com, questions and answers