Home > Net >  Modifying String in Java
Modifying String in Java

Time:02-14

I have a String , String a = newNumber "*" nn " " difference; the newNumber = 106 , nn = 3 and difference = 3. so the output should be as follow ;

Output : 106*3 3

I would like to modify the String so that the output becomes (35*3 1)*3 3 and then with this new String I would like to modify it again so that it becomes ((11*3 2)*3 1)*3 3 Basically I just need to replace the newNumber which was 106 and kept changing to 11, as you can see I'm trying to modify only the newNumber and replacing it with another while keeping the entire String untouched , I'm just replacing and adding to it , how can this be achieved ? The output should be like this, Output :

106*3 3
(35*3 1)*3 3
((11*3 2)*3 1)*3 3

I'm solving an equation with steps , the formulas don't matter I'm just trying to figure out how can I modify the String by replacing the newNumber with a another number and adding new brackets to the equation. I hope I wrote my problem in a way you would understand , I'd really appreciate the help.

CodePudding user response:

You can’t actually modify Strings, but you can use replaceFirst() like this:

s = s.replaceFirst("106", "(35*3 1)");
s = s.replaceFirst("35", "(11*3 2)");

etc

CodePudding user response:

I could not get to the same output which you have but here the code which try to solve this problem I think it might give you little help though which you could solve the problem.

Breaking the number until its prime number and adding the prime numbers to the result. Since we are replacing and appending with strings its better to use StringBuilder.

import java.io.PrintStream;
import java.util.Arrays;

public class StringSimplification {
    public static PrintStream out = System.out;
    public static final boolean prime[];
    public static final int SIZE = 1000000;

    static {
        prime = new boolean[SIZE];
        Arrays.fill(prime, true);
        prime[0] = prime[1] = false;
        //Sieve of Eratosthenes algorithm to find weather number is prime
        for (int i = 2; i < SIZE; i  )
            if (prime[i])
                for (int j = i * 2; j < SIZE; j  = i)
                    prime[j] = false;
    }
    //simplifies your String expression
    public static String simplify(final String expression) {
        StringBuilder result = new StringBuilder("");
        String exp = "";
        for (char ch : expression.toCharArray()) {
            if (Character.isDigit(ch))
                exp  = ch;
            else {
                if (isNumber(exp)) {
                    String simplified = getExpression(Integer.parseInt(exp));
                    result.append(simplified ch);
                    exp = "";//clearing exp
                };
            }
        }
        result.append(exp);
        return result.toString();
    }
    //returns weather number is prime or not
    static boolean isPrime(final int val) {
        return prime[val];
    }

    static String getExpression(final int val) {
        if (val == 0 || val == 1 || prime[val])
            return "("   val   ")";
        int prev = 1;
        int div = 1;
        for (int i = 1; i < val; i  ) {
            if (val % i == 0) {
                prev = i;
                div = val / i;
            }
        }
        return getExpression(prev)   "*"   getExpression(div);
    }
    //Check's weather the expression is number
    public static boolean isNumber(final String s) {
        for (var c : s.toCharArray())
            if (!Character.isDigit(c))
                return false;

        return s.length() > 0;
    }

    public static void main(final String... $) {
        out.println(simplify("106*3 3"));
        out.println(simplify("1024*3 3"));
    }
}

Output:

(53)*(2)*(3) 3
(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(2)*(3) 3

CodePudding user response:

Strings in java are immutable. You will have to use StringBuilder or String Buffer

However if you insist then you may try(from what I understood of the pattern)

    int num = 106;
    String rep = "";
    String S = "106*3 3";
    String target;
    int b = 1;
    int largestfactor = 1;
    System.out.println(S);
    for (int i = num; i > 0; i--) {

        for (int j = 1; j < (num - b); j  ) {
            if ((num - b) % j == 0)
                largestfactor = j;
        }

        target = ""   num;
        rep = "("   largestfactor   "*"   (num - b) / largestfactor   ")"   " "   b;
        S = S.replace(target,rep);
        System.out.println(S);
        num = largestfactor;
        b  ;
        if(b>num)
            break;
    }
  •  Tags:  
  • java
  • Related