简体   繁体   中英

Splitting a string into multiple int form using indexOf()

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}

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

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

You need two things:

  1. a loop;
  2. being able to run indexOf() from where it left off (hint: read the Javadoc ).
public static void main(String[] args) {
        String scores = "100 200 300";
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        int n = 0;
        while (n != -1) {
            String sub = "";
            n = scores.indexOf(" ");
            if (n != -1) {
                sub = scores.substring(0, n);
                scores = scores.substring((n + 1));
            } else {
                sub = scores;
            }
            numbers.add(Integer.parseInt(sub));
        }
        for (int i : numbers) {
            System.out.println("" + i);
        }
    }

Try something like this to loop through and add numbers to arraylist. The arraylist numbers will contain all your numbers.

try this:

    String scores="100 200 300";
    int offset = 0;
    int space;
    int score;

    scores = scores.trim(); //clean the string

    do
    {
        space= scores.indexOf(" ", offset);
        if(space > -1)
        {
            score = Integer.parseInt(scores.substring(offset , space)); 
        }
        else
        {
            score = Integer.parseInt(scores.substring(offset));     
        }

        System.out.println(score);  
        offset = space + 1;

    }while(space > -1);

Your 'n' variable is the important part. You get your first String by slicing from 0 to 'n', so your next string starts not at 0, but at n + " ".size()

Ok, so here is what I have come up with: Since I needed to compare the newly parsed ints with a different variable, as well as ensure that the amount of ints was equal to a different variable, I created this while loop:

public boolean isValid()
{
int index=0;
int initialindex=0;
int ntotal=0;
int ncount=0;
boolean flag=false;
while (index!=-1)
{
    index=scores.indexOf(" ");
    String temp=scores.substring(initialindex,index);
    int num=Integer.parseInt(temp);
    ntotal+=num;
    ncount++;
    initialindex=index;
}
 if (ntotal==total && ncount==count)
{
    flag=true;
}
return flag;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM