I have used scanner instead of string tokenizer ,, below is the piece of code...
Scanner scanner = new Scanner("Home,1;Cell,2;Work,3");
scanner.useDelimiter(";");
while (scanner.hasNext()) {
// System.out.println(scanner.next());
String phoneDtls = scanner.next();
// System.out.println(phoneDtls);
ArrayList<String> phoneTypeList = new ArrayList<String>();
if(phoneDtls.indexOf(',')!=-1) {
String value = phoneDtls.substring(0, phoneDtls.indexOf(','));
phoneTypeList.add(value);
}
Iterator itr=phoneTypeList.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
The ouput I get upon executing this... Home Cell Work
As it is seen from the above code is that in the array list phoneTypeList we are finally storing the values..but the logic of finding out the value on the basisi of ',' is not that much great..that is ..
if(phoneDtls.indexOf(',')!=-1) {
String value = phoneDtls.substring(0, phoneDtls.indexOf(','));
phoneTypeList.add(value);
}
could you please advise me with some other alternative ..!! to achieve the same thing...!!thanks a lot in advance..!!
-
1As long as you're only here to have other people fix your code, maybe you could start upvoting/accepting answers.Paul Bellora– Paul Bellora2012年04月08日 06:43:29 +00:00Commented Apr 8, 2012 at 6:43
-
@PaulBellora The guy only has 4 rep, he can't upvote, and when i first joined the site, I didnt know that accepting was a thing. Lets not be mean here.Lucas– Lucas2012年04月08日 07:43:33 +00:00Commented Apr 8, 2012 at 7:43
-
@Lucas - Take a look at the OP's questions in the last 12 hours. This is the kind of user that SO manages to keep being great in spite of, just sayin.Paul Bellora– Paul Bellora2012年04月08日 15:21:01 +00:00Commented Apr 8, 2012 at 15:21
4 Answers 4
Well, since you asked if there is another way to do it then here is an alternative: You can split the string directly and do it with less code with the foreach statement:
String input = "Home,1;Cell,2;Work,3";
String[] splitInput = input.split(";");
for (String s : splitInput ) {
System.out.println(s.split(",")[0]);
}
No need to use the ArrayList<T> since you can iterate over an array as well.
2 Comments
Scanner is useful if you have an InputStream and you need to do some regular expression group matching. But if all you're doing string manipulation (such as splitting a string) then you're easier off with using string manipulation functions such as String.split(...) in this case.could you try to split based on ',' STIRNG_VALUE.split(','); will return u an array with strings separated with , may be this helps
Comments
If i understand correctly. The problem statement is you want to maintain a list of Phone-Type-List. Like this: ["Home", "Cell", "Work"].
I suggest you keep this in a property file / config file / database which ever makes sense and load it to memory on start of you app.
If the input cannot be changed then as for the algorithm i couldn't think of a better one. Looks good.
You could use split function of string if that makes sense. First use split on ";" Then a split on ","
Comments
declare the arraylist outside the while loop.
try this, i have made some change for better performance too. hope you can compare and understand the change.
ArrayList<String> phoneTypeList = new ArrayList<String>();
Scanner scanner = new Scanner("Home,1;Cell,2;Work,3");
scanner.useDelimiter(";");
String phoneDtls = null;
String value = null;
while (scanner.hasNext()) {
phoneDtls = scanner.next();
if (phoneDtls.indexOf(',') != -1) {
value = phoneDtls.split(",")[0];
phoneTypeList.add(value);
}
}
Iterator itr = phoneTypeList.iterator();
while (itr.hasNext())
System.out.println(itr.next());
I have executed n got the result, check screenshot.
enter image description here