0

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..!!

3
  • 1
    As long as you're only here to have other people fix your code, maybe you could start upvoting/accepting answers. Commented 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. Commented 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. Commented Apr 8, 2012 at 15:21

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

oh i thought the input is dynamic n you are reading the value from somewhere using Scanner.
The 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.
1

could you try to split based on ',' STIRNG_VALUE.split(','); will return u an array with strings separated with , may be this helps

Comments

1

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

1
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

2 Comments

Hi Rajkumar, thnanks a lot..!! very good expnation, could you please expain the one statement that is... value = phoneDtls.split(",")[0]; ,as far as I have grasp is that it is spliting futher on the basis of (",") but what the [0] is doing here..!! thanks a lot..!!
split method returns an array, here it will contain 2 elements. "[0]" will take the first element.. pls let me know if u need more explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.