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"
4 Respuestas
+ 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);
+ 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);
+ 3
By using split() method and regex, here's an example:
https://code.sololearn.com/ckq6CG77SsoX/?ref=app