0

I have sentences starting with

  1. ccccccccddkskdjskjdksjdfksjfkjs sjfsjfksjjs

I want to see if the starting of the line I read is with a number and . if yes I want to read the rest of the line.

How can I do that ?

I taught of reading the line to a

 string str.
str[0] and str [1].
str[1]= .

 x= Character.isDigit(string.charAt(0)); 
 if(x) { if (string.charAt(1)=='.') 
 { print ...I get a Exception 

is there a easy way that I can check if str[0] is a number ..right now I am checking manually if it is 1 (or) 2...like that?


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(Unknown Source)

Azodious
  • 13,752
  • 1
  • 36
  • 71
The Learner
  • 3,867
  • 14
  • 40
  • 50

5 Answers5

3

Character class has isDigit method, which you can use as follows:

String str1 = "I start with letter";
boolean startWithDigit = Character.isDigit(str1.charAt(0));
// startWithDigit = false

String str2 = "1 start with digit";
boolean startWithDigit = Character.isDigit(str2.charAt(0));
// startWithDigit = true
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • Thank you this resolved by question..but I am having one more associated question before I close this: ----Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(Unknown Source)...I tried like you said x= Character.isDigit(string.charAt(0)); if(x) { if (line.charAt(1)=='.') { I get that Exception – The Learner Sep 26 '12 at 06:48
  • add the code in your question, it will be more clear to undestand. – Azodious Sep 26 '12 at 06:50
  • 1
    before accessing index, always check `if (index < line.length())` – Azodious Sep 26 '12 at 06:51
1
String str="1. ccccccccddkskdjskjdksjdfksjfkjs sjfsjfksjjs";
char charAt = str.charAt(0);
Pattern p = Pattern.compile("\\d+");
Matcher m1 = p.matcher(""+charAt);
System.out.println(m1.matches());
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

Check for the first character's ascii value.

 char x = str.charAt(0);
 if(x >= 48 && x <= 57)
 {
     // do whatever
 }
akaHuman
  • 1,332
  • 1
  • 14
  • 33
0

You can do:

boolean isANumber = false;
try{
  Integer.parseInt(str[0]);
  isANumber = true;
}catch(Exception e) {
}

//now isANumber is true if str[0] is a number
Arcymag
  • 1,037
  • 1
  • 8
  • 18
  • Char.isDigit(). Wont work if OP wants to check if it's a decimal (e.g. Double.parseDouble), or if it's not in base 10 – Arcymag Sep 26 '12 at 06:39
  • I don't know what are you talking about. In your code you check only first character and try to parse it. So it is too much code, isDigit is enough. – apocalypse Sep 26 '12 at 07:04
0

Do like this

String string="ccccccccddkskdjskjdksjdfksjfkjs sjfsjfksjjs"

Character.isDigit(string.charAt(0));

u can check ur value like this

for more detail see this one

How do I find out if first character of a string is a number?

Community
  • 1
  • 1
Aamirkhan
  • 5,746
  • 10
  • 47
  • 74