Search code examples
javaregexalgorithmstring-parsing

Parsing a string that contains multiple symbols results in crash


Im trying to split some characters in Java that contains "," , ":" and "-"

For instance ,

if the input is 58,1:2-4, it should produce the following output

  1. Booknumber: 58
  2. Chapter Number: 1
  3. Verses = [2,3,4] (since 2-4 is the values from 2 to 4)

Following is the code that I have tried,

private int getBookNumber() {
        bookNumber = chapterNumber.split("[,]")[0];
        return Integer.valueOf(bookNumber);
    }

    private int getChapterNumber() {
        chapterNumber = sample.split("[:]")[0];
        verseNumbers = sample.split("[:]")[1];
        return Integer.valueOf(chapterNumber);
    }

    private List<Integer> getVerseNumbers(String bookValue) {
        List<Integer> verseNumList = new ArrayList<>();
        if (bookValue.contains("-")) {
            //TODO parse - separated string
        } else {
            verseNumList.add(Integer.valueOf(bookValue));
        }

        return verseNumList;
    }

I would invoke them in the following manner sequentially

int chapterNumber = getChapterNumber(); 
int  bookNumber    = getBookNumber();
List<Integer> verseNumbers  = getVerseNumbers(this.verseNumbers);

But Im getting Caused by: java.lang.NumberFormatException: Invalid int: "58 , 1 " in the line int chapterNumber = getChapterNumber();

is there an efficient way to parse this string ?


Solution

  • You should change getChapterNumber like this:

    private int getChapterNumber() {
        chapterNumber = sample.split("[:]")[0];
        verseNumbers = sample.split("[:]")[1];
        return Integer.valueOf(chapterNumber.split("[,]")[1]);
    }
    

    But the best would be to use matcher:

    String line = "58,1:2-4";
    Pattern pattern = Pattern.compile("(\\d+),(\\d+):(.*)");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        System.out.println("group 1: " + matcher.group(1));
        System.out.println("group 2: " + matcher.group(2));
        System.out.println("group 3: " + matcher.group(3));
    }
    

    Output:

    group 1: 58
    group 2: 1
    group 3: 2-4