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?

Recommended Answers

All 2 Replies

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");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.