Hope
Hope

Reputation: 79

Split mathematical string in Java

I have this string: "23+43*435/675-23". How can I split it? The last result which I want is:

String 1st=23
String 2nd=435
String 3rd=675
String 4th=23

I already used this method:

String s = "hello+pLus-minuss*multi/divide";
String[] split = s.split("\\+");
String[] split1 = s.split("\\-");
String[] split2 = s.split("\\*");
String[] split3 = s.split("\\/");
String plus = split[1];
String minus = split1[1];
String multi = split2[1];
String div = split3[1];
System.out.println(plus+"\n"+minus+"\n"+multi+"\n"+div+"\n");

But it gives me this result:

pLus-minuss*multi/divide
minuss*multi/divide
multi/divide
divide

But I require result in this form

pLus
minuss
multi
divide

Upvotes: 0

Views: 1058

Answers (4)

Achintya Jha
Achintya Jha

Reputation: 12843

Try this:

public static void main(String[] args) {
    String s ="23+43*435/675-23";
    String[] ss = s.split("[-+*/]");
    for(String str: ss)
        System.out.println(str);

}

Output:

23
43
435
675
23

I dont know why you want to store in variables and then print . Anyway try below code:

public static void main(String[] args) {
    String s = "hello+pLus-minuss*multi/divide";
    String[] ss = s.split("[-+*/]");
    String first =ss[1];
    String second =ss[2];
    String third =ss[3];
    String forth =ss[4];

    System.out.println(first+"\n"+second+"\n"+third+"\n"+forth+"\n");

    }

Output:

pLus
minuss
multi
divide

Upvotes: 2

user000001
user000001

Reputation: 33307

If the order of operators matters, change your code to this:

    String s = "hello+pLus-minuss*multi/divide";
    String[] split = s.split("\\+");
    String[] split1 = split[1].split("\\-");
    String[] split2 = split1[1].split("\\*");
    String[] split3 = split2[1].split("\\/");
    String plus = split1[0];
    String minus = split2[0];
    String multi = split3[0];
    String div = split3[1];
    System.out.println(plus + "\n" + minus + "\n" + multi + "\n" + div + "\n");

Otherwise, to spit on any operator, and store to variable do this:

public static void main(String[] args) {
    String s = "hello+pLus-minuss*multi/divide";

    String[] ss = s.split("[-+*/]");
    String plus = ss[1];
    String minus = ss[2];
    String multi = ss[3];
    String div = ss[4];
    System.out.println(plus + "\n" + minus + "\n" + multi + "\n" + div + "\n");
}

Upvotes: 0

Sharun
Sharun

Reputation: 3082

I think it is only the issue of index. You should have used index 0 to get the split result.

String[] split = s.split("\\+");
String[] split1 = split .split("\\-");
String[] split2 = split1 .split("\\*");
String[] split3 = split2 .split("\\/");

String hello= split[0];//split[0]=hello,split[1]=pLus-minuss*multi/divide

String plus= split1[0];//split1[0]=plus,split1[1]=minuss*multi/divide
String minus= split2[0];//split2[0]=minuss,split2[1]=multi/divide
String multi= split3[0];//split3[0]=multi,split3[1]=divide

String div= split3[1];

Upvotes: 0

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Try this out :

        String data = "23+43*435/675-23";
    Pattern pattern = Pattern.compile("[^\\+\\*\\/\\-]+");

    Matcher matcher = pattern.matcher(data);

    List<String> list = new ArrayList<String>();
    while (matcher.find()) {
        list.add(matcher.group());
    }

    for (int index = 0; index < list.size(); index++) {
        System.out.println(index + " : " + list.get(index));
    }

Output :

0 : 23

1 : 43

2 : 435

3 : 675

4 : 23

Upvotes: 1

Related Questions