I have astring as a input, and required to check if a string contain a numer from 1 to 9
I implemented this by scanning a string
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == '1' || ch == '2' || ch == '3' || ch == '4' ||
ch == '5'|| ch == '5' || ch == '6' || ch =='7' || ch == '8'|| ch == '9') {
// some code
}
Is there any method instead of along if statement .
thanks
scan a string untill found a charcter
Started by eman ahmed, Jul 26 2010 11:52 PM
6 replies to this topic
#1
Posted 26 July 2010 - 11:52 PM
|
|
|
#2
Posted 27 July 2010 - 12:08 AM
You can do if( ch>=1 && ch<=9 )
If you compare a char with an integer it uses the unicode i think. And that works just fine.
An easier way would be to use regular expressions (regex):
. = Any character
* = the previous element 0 or more times
[0-9] = a range, any character between and including 0 to 9
So this reads as:
If the string equals "any character 0 or more times, then a number, then any character 0 or more times. Then it matches the regex.
If you compare a char with an integer it uses the unicode i think. And that works just fine.
An easier way would be to use regular expressions (regex):
if(string1.matches(".*[0-9].*"))
.*[0-9].*. = Any character
* = the previous element 0 or more times
[0-9] = a range, any character between and including 0 to 9
So this reads as:
If the string equals "any character 0 or more times, then a number, then any character 0 or more times. Then it matches the regex.
#3
Posted 27 July 2010 - 12:17 AM
unfortaunatly It doesn't work .
and I meant that a charcter is already 1 or 2 or 3..........or 9
not the unicode of the character is 1 or 2 or 3..........or 9
and I meant that a charcter is already 1 or 2 or 3..........or 9
not the unicode of the character is 1 or 2 or 3..........or 9
#4
Posted 27 July 2010 - 12:20 AM
Note that i edited my post :)
And ch>=1 && ch<=9 DOES work.
To compare char with int, java makes a char of the int.
It's hard to tell for chars which is 'greater than'
Then how does java decides which character is 'greater than' -->unicode:
Unicode of char = number
Unicode of '1' = number
numbers can be compared for 'greater than', 'lower than'.
And ch>=1 && ch<=9 DOES work.
To compare char with int, java makes a char of the int.
It's hard to tell for chars which is 'greater than'
Then how does java decides which character is 'greater than' -->unicode:
Unicode of char = number
Unicode of '1' = number
numbers can be compared for 'greater than', 'lower than'.
#5
Posted 27 July 2010 - 07:21 AM
once again, you can't understood me
string str="rer8kdl2";
I want to know that str contain any digit from 0 to 9 or not
and I doesn't want to get the unicode of the specific character and check if it's smaller than 9 and greater than 0
at any way thanks for your effor
but I'm still have the same problem
string str="rer8kdl2";
I want to know that str contain any digit from 0 to 9 or not
and I doesn't want to get the unicode of the specific character and check if it's smaller than 9 and greater than 0
at any way thanks for your effor
but I'm still have the same problem
#6
Posted 27 July 2010 - 08:33 AM
Oops instead of ch>=0 that must be '0' with single quotes, same with the 9. Check out this example i made: http://ideone.com/FC59Q
code:
code:
class Main{
public static void main(String[] args){
String number = "This string contains a numb3er.";
String noNumber = "This string contains no number.";
System.out.println("Using loop and ch>='0' && ch<='9'");
for (int i = 0; i < number.length(); i++) {
char ch = number.charAt(i);
if(ch>='0' && ch<='9'){
System.out.println("The String: \"" + number + "\" contains a number: " + ch );
}
}
for (int i = 0; i < noNumber.length(); i++) {
char ch = noNumber.charAt(i);
if(ch>='0' && ch<='9'){
System.out.println("The String: \"" + noNumber+ "\" contains a number: " + ch );
}
}
System.out.println("Using the regex");
if(number.matches(".*[0-9].*"))
System.out.println("The String: \"" + number+ "\" contains a number.");
if(noNumber.matches(".*[0-9].*"))
System.out.println("The String: \"" + noNumber+ "\" contains a number.");
}
}
#7
Posted 27 July 2010 - 09:10 AM
thanks alot for your help
I got it and now I can replace a long if with this statement " if(ch>='0' && ch<='9') "
your code is very useful
thanks for your efffort
I got it and now I can replace a long if with this statement " if(ch>='0' && ch<='9') "
your code is very useful
thanks for your efffort


Sign In
Create Account


Back to top









