FirmView
FirmView

Reputation: 3150

Parsing string using the Scanner class

I am trying to parse some lines and check their values, For example in the string:

   " 1 ON OFF";

I have to check whether:

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?

Upvotes: 1

Views: 14804

Answers (5)

ant
ant

Reputation: 22948

Another alternative :

public static void main(String[] args) throws IOException
    {

        String patternString = " 1 ON OFF";
        boolean pass = true;

            if (patternString.charAt(0) != ' ' && patternString.charAt(2) != ' ') {
                pass = false;
            }

            int digit = Character.getNumericValue(patternString.charAt(1));

            if (digit < 0 && digit > Integer.MAX_VALUE) {
                pass = false;
            }

            if (patternString.charAt(3) != 'O' && patternString.charAt(4) != 'N') {
                pass = false;
            }

            if (patternString.charAt(5) != ' ' && patternString.charAt(6) != 'O' && patternString.charAt(7) != 'F' && patternString.charAt(8) != 'F') {
                pass = false;
            }

            if (pass) {
                System.out.println("Validation pass");
            }
    }

Upvotes: 1

user180100
user180100

Reputation:

You can use parboiled and declare a small grammar.

Upvotes: 1

pb2q
pb2q

Reputation: 59607

Try using String.split(" "), to split around the spaces. You'll get an array, and if the first character is blank, the first String in the array will be an empty string:

String strs[] = " 1 ON OFF".split(" ");

With this example you'll get this array: ["", "1", "ON", "OFF"].

If the first character is a space, you'll get the empty string as the first element. If there are 2 leading spaces, then you'll see empty strings as the first two elements. The remaining strings will be the space separated tokens from the original string, but if there are additional spaces between the tokens then you'll see additional empty strings as array elements.

Looping over the resulting array, including a parseInt for the number, you'll be able to match the rules that you've described.

Note that you can use Scanner to similarly tokenize the string, by setting the delimiter to the empty string:

Scanner sc = new Scanner(str);
sc.useDelimiter("");
System.out.println(sc.next());

Upvotes: 1

Mark Elliot
Mark Elliot

Reputation: 77044

It's worth taking a look at StringReader, which will let you scan through the string character by character. Another option is simply to read each character (String#charAt) and check if it meets your rules.

Upvotes: 1

nhahtdh
nhahtdh

Reputation: 56809

The closest I can think of is splitting by word boundary:

String tokens[] = " 1 ON OFF".split("\\b");

It will give the following array:

{ " ", "1", " ", "ON", " ", "OFF" }

It fits your ordering and your definition of 1st-6th "character".

Upvotes: 2

Related Questions