1

I have a String Array with multiple entries in it. I want to transcribe this String Array into another one, replacing specific entries with a digit (k in this case). What I've tried so far:

public void replace(String[] eqh, char var, int maxX){
String[] holder = new String[eqh.length];
    String  comp = "";
    for (int k = -maxX/2; k <= maxX/2; k++){
        for (int i = 0; i< eqh.length; i++){
            if (eqh[i].equals(var)){
                holder[i] = ""+k; 
            } else {
                holder[i] = eqh[i];
            } 
            comp = Arrays.toString(holder);
            System.out.println("comp: "+comp);
        } 
/// some stuff with comp here
}

Unfourtunatley this is not working, the return for comp is exactly the same as the input from eqh.

4
  • 1
    You're changing holder (a variable that exists only in replace's scope), I don't see how eqh is affected. Commented Feb 25, 2016 at 13:21
  • Some debugging should help you figure out Commented Feb 25, 2016 at 13:22
  • 2
    You are comparing strings with chars in eqh[i].equals(var) (it will always return false). If you want to check whether first char is equals to var, do eqh[i].charAt(0) == var Commented Feb 25, 2016 at 13:22
  • eqh shouldnt be affected, because ill have to transcribe it multiple times Commented Feb 25, 2016 at 13:22

2 Answers 2

3

You are comparing strings with chars in eqh[i].equals(var) (it will always return false). If you want to check whether first char is equals to var, do eqh[i].charAt(0) == var

Also, it is not a good practice to convert integers to strings doing ""+k. Use Integer.toString(k) or String.valueOf(k).

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

1 Comment

You are welcome. Also check my advice about integers conversion.
1

You are trying to check if a String is equals to a char, the result of this test will always be false.

You can use eqh[i].indexOf(var) >= 0 to test if a String contains a char.

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.