0

Let's say that i have a method goTo(int finalx, finaly, int currx, int curry) and a string that maybe looks like this:

String sequence = "0001112122"

The above means that 00->01->11->21->22

And this is how it should look like:

goTo(0,1,0,0);
goTo(1,1,0,1);
goTo(2,1,1,1);
goTo(2,2,2,1);

How can i write out the above using a foor loop maybe to iterate through the string an input the specific numbers at the right place?

Notice that sequence may not always be that specific length it could be a different sequence string.

Thanks in advance!

0

2 Answers 2

2

I don't know the purpose of your goTo method but you would definitely need to use the String charAt(int index) method

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#charAt(int)

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

Comments

0

Are you looking for something like this?

String sequence = "0001112122";
for(int i=0; i+3<sequence.length(); i+=2){
    int currx = Character.getNumericValue(sequence.charAt(i));
    int curry = Character.getNumericValue(sequence.charAt(i+1));
    int finalX = Character.getNumericValue(sequence.charAt(i+2));
    int finalY = Character.getNumericValue(sequence.charAt(i+3));
    goTo(finalX, finalY, currx, curry);
}

It parses the 1st, 2nd, 3rd, and 4th digit into your goTo() method then jumps forward two characters and repeats.

Comments

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.