-3

I have this method:

public void separator(){
    int count=0, i=0;
    while (count == 0) {
        if (track1result.charAt(i) != '^') {
            char c = track1result.charAt(i);
            number += c;
        } else {
            count++;
        }
        i++;
    }
}

It's supposed to iterate a String until he reaches the ^ symbol, and that's great, and working so far, the problem is that i'm not sure how can i like keep going from there so i can get the string that's after the symbol and store it in another variable. If you can give me ideas i would really appreciate it!

Lothar
  • 5,323
  • 1
  • 11
  • 27

2 Answers2

2

you can just split the String into array

String s1 = "hello^world";
    String[] arr = s1.split("\\^");
    String firstpart = arr[0];
    String secondpart = arr[1];
    System.out.println(firstpart+" "+secondpart);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

Inside the else part add this:

if (i < track1result.length() - 1)
    rest = track1result.substring(i + 1);

where rest is previously declared:

String rest = "";
forpas
  • 160,666
  • 10
  • 38
  • 76
  • This is a really poor solution--not only for the reason that you are supporting the OP's poor choice of using a loop. – Barns Nov 08 '18 at 20:19
  • @Barns Do you know why the OP chose this loop to solve this kind of problem? Don't you know that many questions in SO are just school exercises with restrictions on use of certain methods that would otherwise never been used by a developer? – forpas Nov 08 '18 at 20:22
  • Exactly, offer the OP the opportunity to use this poor example and get an Failing grade on the assignment. They should will be thankful. – Barns Nov 08 '18 at 20:23
  • Thanks man, it really helped me out – Sarcastic Hawke Nov 08 '18 at 20:24
  • @Barns I guess you did not even read my comment. What I'm saying is if the assignment strictly demanded the use of a loop then the OP would not and should not use any other more effective method. Am I clear now? – forpas Nov 08 '18 at 20:26