简体   繁体   中英

Character validation for each newLine

I'm trying to find a way to validate if each line being read from "srcFile" starts with an alpha character. I tried using the Character.isLetter() method but it complains about line being declared as a String and not a char array...from what I thought, a String is an array of characters, but appearently isLetter() does not work in that way. I'm wondering what method I could use. I want to filter the data from srcFile and place it a HashMap and Vector but I first need to find an effective and clean way to figure out if the line starts with alpha characters. I also thought about using String.contains() because I know what specfic char sequence I'm looking for but it would proce to be more verbose and possibly uneccesary way of coding and it also makes my program less generic, which I want to a certain extent.

    try {
        Scanner myScanner = new Scanner(srcFile);
        // Why not declare line outside while loop 
        // and write cond. as (line = myScan... != null) ?
        while (myScanner.hasNextLine()) {
            String line = myScanner.nextLine();
            //System.out.println(line);

            if (Character.IsLetter(line[0])) {

            }
        }
        myScanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

The rest of the condition would like this:

                //if first char start with a aplha char, move to the ticket list
                if (Char.IsLetter(line[0]))
                {
                    ticket t = new ticket();
                    t.parseLine(line);
                    //logger.Debug(line);

                    tickets.Add(t);
                }
                else
                {
                    sbPeople sbuPeople = new sbPeople();
                    sbuPeople.parseLine(line);

To get the first character in a String, use line.charAt(0) . Only arrays are indexed with [] , and Strings are not arrays.

You can also use a regular expression. For example:

String line = myScanner.nextLine();
if (line.matches("^\\p{Alpha}.*$")) {
    System.out.printf("Starts with an alphabetic character: %s%n", line);
    ...
} else {
    ...
}

Explanation of each item in the regular expression:

NODE                     EXPLANATION
---------------------------------------------------------------------
  ^                        the beginning of the string
---------------------------------------------------------------------
  \p{Alpha}                any character of: letters
---------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
---------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM