0

particularly from a txt file. for example if the file reads "name ###-####-#### address" all in one single line. how do i put all three items in three separate variables and print them?

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by CrazyDieter
0

regular expressions could help you to check and then parse your string :

//a name followed by 4 sequences of 4 numbers, and an adress.
Pattern p = Pattern.compile("^(.+) ([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{4}) (.+)$");
Matcher m = p.matcher(inputString);
if(m.matches){
   String name = m.group(1);
   int a0 = Integer.parseInt(m.group(2));
   int a1 = Integer.parseInt(m.group(3));
   int a2 = Integer.parseInt(m.group(4));
   int a3 = Integer.parseInt(m.group(5));
   String adress = m.group(6);
}
else throw new IllegalArgumentException("invalid syntax");

Edited by CrazyDieter: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.