Splitting a string into multiple int form using indexOf() Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Splitting a string into multiple int form using indexOf() without wasting too much if your time.

The question is published on by Tutorial Guruji team.

This is my first post so go easy on me. This IS a homework question, but I have spent about 7 hours working through various means to complete this goal and have had no success. I am building various methods for an assignment, and I need to figure out how to split a String into several int variables.

Ex: given the String “100 200 300” I need to change it to three int of 100, 200, 300. I have to use indexOf(), and cannot use split() or arrays.

    String scores="100 200 300";
    int n=scores.indexOf(" ");
    String sub=scores.substring(0,n);
    Integer.parseInt(sub);

This lets me get the first string “100” and parse it. However, I do not know how to continue the code so it will get the next ones. For my method, I will need the new int variables for later arguments.

EDIT: I think I need to use a for loop: something like:

for(int i=0; i<=scores.length; i++)
{//I do not know what to put here}

Answer

Joe, indexOf() is overloaded, check out this version:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int,%20int)

We are here to answer your question about Splitting a string into multiple int form using indexOf() - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji