I am trying to parse some lines and check their values, For example in the string:
" 1 ON OFF";
I have to check whether:
- the first character is blank.
- the second character is int.
- the third character is blank.
- the fourth character is 2 characters and it is ON.
- the fifth character is blank.
- the sixth character is 3 characters and it is OFF.
I can do this at one go using regex, but what I want is that after each check I have to display whether is correct or not like:
System.out.println("1st character is not a blank : incorrect");
System.out.println("1st character is blank : correct");
I thought of using Scanner class for this, but when I try to detect the first character, it is showing 1 instead of blank for the string,
" 1 ON OFF";
public class NewClass {
public void StringExample(){
String str = " 1 ON OFF";
Scanner sc = new Scanner(str);
System.out.println(sc.next());
}
public static void main(String args[]){
NewClass nc = new NewClass();
nc.StringExample();
}
}
Is there any other class in java with which this can be done easily?
The closest I can think of is splitting by word boundary:
It will give the following array:
It fits your ordering and your definition of 1st-6th “character”.