Let us see how to find whether the
given Character of a String is a number or first Character
of any String is a number or not ?
CharNumber.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package java4s;
public class CharNumber {
public
static void main(String... args)
{
String
str = "java4s";
//Check
whether the given character is a number of not ?
for(int
i=0; i<str.length();i++)
{
Boolean
flag = Character.isDigit(str.charAt(i));
if(flag){
System.out.println("'"+str.charAt(i)+"' is a number");
}
else{
System.out.println("'"+str.charAt(i)+"' is not a number");
}
}
//----------------------------------------------------------------
//To
check first character is a number or not ?
Boolean
flag2 = str.substring(0, 1).matches("[0-9]");
if(flag2){
System.out.println("First Character is a number..!");
}
else{
System.out.println("First character is not a number..!");
}
}
}
|
Output
‘j’ is not a number
‘a’ is not a number
‘v’ is not a number
‘a’ is not a number
’4′ is a number
‘s’ is not a number
First character is not a number..!
‘a’ is not a number
‘v’ is not a number
‘a’ is not a number
’4′ is a number
‘s’ is not a number
First character is not a number..!
No comments:
Post a Comment