Splitting up input using regular expressions in Java

Posted by Joe24 on Stack Overflow See other posts from Stack Overflow or by Joe24
Published on 2012-11-11T22:39:42Z Indexed on 2012/11/11 23:00 UTC
Read the original article Hit count: 137

Filed under:

I am making a program that lets a user input a chemical for example C9H11N02. When they enter that I want to split it up into pieces so I can have it like C9, H11, N, 02. When I have it like this I want to make changes to it so I can make it C10H12N203 and then put it back together. This is what I have done so far. using the regular expression I have used I can extract the integer value, but how would I go about get C10, H11 etc..?

System.out.println("Enter Data");

Scanner k = new Scanner( System.in );
String input = k.nextLine();

String reg = "\\s\\s\\s";
String [] data;

data = input.split( reg );

int m = Integer.parseInt( data[0] );
int n = Integer.parseInt( data[1] );

© Stack Overflow or respective owner

Related posts about java