0

Splitting input

HI CAN SOMEONE TEACH ME HOW TO SPLIT AN INPUT .EXAMPLE IF I INPUT "A1B2C3D4" THE OUTPUT WOULD BE "LETTERS:ABCD" AND "NUMBERS:1234"

1st Aug 2019, 11:52 PM
Cheska Sarahah
3 Antworten
+ 15
you can learn regex for it & then try it, here is code for it: String text=new Scanner(System.in).nextLine(); //splitting by 1 or more digits, '+' is one or more quantifier String letters[]= text.split("\\d+"); for(String a:letters) System.out.print(a); System.out.println(); //splitting by 1 or more non-digits String digits[]=text.split("\\D+"); for(String b:digits) System.out.print(b);
2nd Aug 2019, 1:06 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
//this ways pretty simple, although I’m sure someone can do much better :) Scanner scan = new Scanner(System.in); String input = scan.nextLine(); String nums = ""; String lets = ""; for(String s : input.split("")) { if(s.matches("[0-9]*")) nums += s; else if(s.matches("[a-zA-Z]*")) lets += s; } System.out.printf("NUMBERS: %s\nLETTERS: %s",nums,lets);
2nd Aug 2019, 1:18 AM
Jake
Jake - avatar
+ 3
By using split() method and regex, here's an example: https://code.sololearn.com/ckq6CG77SsoX/?ref=app
2nd Aug 2019, 1:19 AM
voja
voja - avatar