Java Scanner Distributor

I use Scanner and Delimiter to tokenize my .txt file (this is the homework I need to do). The first version of the file is as follows:

5,5,5,6,5,8,9,5,6,8, good, very good, excellent, good
7,7,8,7,6,7,8,8,9,7,very good, Good, excellent, very good
8,7,6,7,8,7,5,6,8,7 ,GOOD, VERY GOOD, GOOD, AVERAGE
9,9,9,8,9,7,9,8,9,9 ,Excellent, very good, very good, excellent
7,8,8,7,8,7,8,9,6,8 ,very good, good, excellent, excellent
6,5,6,4,5,6,5,6,6,6 ,good, average, good, good
7,8,7,7,6,8,7,8,6,6 ,good, very good, good,  very good
5,7,6,7,6,7,6,7,7,7  ,excellent, very good, very good, very good

And I used useDelimiter("[ ]*(,)[ ]*"), the second version of the file is as follows:

5 5 5 6 5 8 9 5 6 8 good, very good, excellent, good
7 7 8 7 6 7 8 8 9 7 very good, Good, excellent, very good
8 7 6 7 8 7  5 6 8 7 GOOD, VERY GOOD, GOOD, AVERAGE
9 9 9 8 9 7 9  8 9 9 Excellent, very good, very good, excellent
7 8 8 7 8 7 8 9 6 8 very good, good, excellent, excellent
6 5 6 4 5 6 5 6 6 6 good, average, good, good
7  8 7 7 6 8 7 8 6 6 good, very good, good,  very good
5 7 6 7 6 7 6 7 7 7  excellent, very good, very good, very good

And I can’t come up with a regular expression that would help me separate the numbers with a space and words with a comma. Every month I need an array with 14 values ​​(it’s very good that this is the only variable)

Please note that there are several gaps (this is done in order to make it more difficult for us).

Therefore, any help would be appreciated.

PS We are allowed to use only delimiters (no splits, etc.)

+5
source
4

, - ((<?=)) (|):

String input = "9 9 9 8 9 7 9  8 9 9 Excellent, very good, very good, excellent";
Scanner s = new Scanner(input).useDelimiter("(?<=\\d)[\\s,]+|\\s*,\\s*");
while (s.hasNext()) {
    System.out.println("Token: ." + s.next() + ".");
}

Token: .9.
Token: .9.
Token: .9.
Token: .8.
Token: .9.
Token: .7.
Token: .9.
Token: .8.
Token: .9.
Token: .9.
Token: .Excellent.
Token: .very good.
Token: .very good.
Token: .excellent.
+4

(((?<=[0-9]+)\s*(?=[0-9]+))|(,\s*(?=[a-zA-Z]+))|((?<=[0-9]+)\s*(?=[a-zA-Z]+))), ,

+2

, Scanner . , 10 4 , , (\s+) 10 nextInt(), , (\s*,\s*).

- :

String input = "5 5 5 6 5 8 9 5 6 8 good, very good, excellent, good";
Scanner scanner = new Scanner(input).useDelimiter("\\s+");
int[] results = new int[14];
for (int i = 0; i < 10; ++i) {
    results[i] = scanner.nextInt();
}
scanner.useDelimiter("\\s*,\\s*");
scanner.skip("\\s*");
for (int i = 10; i < 14; ++i) {
    String wordPhrase = scanner.next();
    int wordValue;
    if ("average".equalsIgnoreCase(wordPhrase))
        wordValue = 1;
    else if ("good".equalsIgnoreCase(wordPhrase))
        wordValue = 2;
    else if ("very good".equalsIgnoreCase(wordPhrase))
        wordValue = 3;
    else if ("excellent".equalsIgnoreCase(wordPhrase))
        wordValue = 4;
    else
        wordValue = 0;
    results[i] = wordValue;
}

regex, , , , .

+2
String[] str = expression.split("(,\\s+)|(\\s+)");

:

0
source

All Articles