Home > Mobile >  I need to prase integers after a specific character from list of strings
I need to prase integers after a specific character from list of strings

Time:04-28

i got a problem here guys. I need to get all the numbers from a string here from a list of strings.

Lets say one of the strings in the list is "Jhon [B] - 14, 15, 16" and the format of the strings is constant, every string has maximum of 7 numbers in it and the numbers are separated with "," . I want to get every number after the "-". i am really confused here, i tried everything i know of but i am not getting even close.

public static List<String> readInput() {
    final Scanner scan = new Scanner(System.in);
    final List<String> items = new ArrayList<>();
    while (scan.hasNextLine()) {
        items.add(scan.nextLine());
    }
    return items;
}

public static void main(String[] args) {
    final List<String> stats= readInput();
    
}

}

CodePudding user response:

You could...

Just manually parse the String using things like String#indexOf and String#split (and String#trim)

String text = "Jhon [B] - 14, 15, 16";
int indexOfDash = text.indexOf("-");
if (indexOfDash < 0 && indexOfDash   1 < text.length()) {
    return;
}
String trailingText = text.substring(indexOfDash   1).trim();
String[] parts = trailingText.split(",");
// There's probably a really sweet and awesome
// way to use Streams, but the point is to try
// and keep it simple            
  • Related