0
import java.util.Scanner;

public class ProgrammaD{
    public static void main(String[] args){
        Scanner keyboard= new Scanner(System.in);
        String sequence = args[0];
        char chars[] = new char[sequence.length()];

        for(int i = 0; i < sequence.length(); i++){
            chars[i] = sequence.charAt(i);
        }

        System.out.println("Insert a character");
        char car = keyboard.next().charAt(0);
        System.out.println("Insert an integer");
        int n = keyboard.nextInt();
        System.out.println("Original Array");

        for(int z = 0; z < sequence.length(); z++){
            System.out.println(chars[z]);
        }

        char chars2[] = MetodiD.occorrenzeContenuto(chars, n, car);
        System.out.println("Edited Array");

        for(int k = 0; k < sequence.length(); k++){
            System.out.println(chars2[k]);
        }

        boolean condition = false;
        char chars3[] = MetodiD.uniformaContenuto(chars, chars2);

        for(int x = 0; x < sequence.length(); x++){
            if(chars[x] == chars2[x]){
                condition = false;
            }
            else{
                condition = true;
            }
        }

        if(condition){
            for(int y = 0; y < sequence.length(); y++){
                System.out.println(chars3[y]);
            }
        }
    }
}

I can't execute the last if instruction, and I can't understand why. Could someone help me how to figure it out? I want the program to print the array returned in MetodiD.uniformaContenuto(), but I can't get anything from this code.

Here's the other class:

public class MetodiD{
    public static char[] occorrenzeContenuto(char par[], int a, char b){
        int contatore = 0;
            for(int i = 0; i < par.length; i++){
                if(par[i] != b){
                    contatore++;
                }else{
                }
            }
            if(contatore > a){
                for(int k = 0; k < par.length; k++){
                    if(par[k] != b){
                        par[k] = determinaCarattere(a, b);
                    }else{
                }
            }
            }else{
            }
        return par;
    }

    public static char determinaCarattere(int n, char b){
        int modificato;
            if(n%2==0){
                modificato = (int)b - n;
            }else{
                modificato = (int) b + n;
            }
        char modificato2 = (char)modificato;
        return modificato2;
    }

    public static char[] uniformaContenuto(char par1[], char par2[]){
        char rit[] = new char[par1.length];
            for(int y = 0; y < par1.length; y++){
                if(par1[y]<par2[y]){
                    rit[y] = par1[y];
                }else{
                    rit[y] = par2[y];
                }
            }
        return rit;
    }
}
aipam
  • 137
  • 3
  • 10
  • print statements are always helpful (if you don't have a debugger) – qwerty_so Jan 21 '15 at 00:41
  • What do your methods do? I'm inclined to think your "condition" boolean always returns false – Angelo Alvisi Jan 21 '15 at 00:47
  • 1
    You assign `condition` in `for` loop, only the last affects the value of `condition`, check it! – fikr4n Jan 21 '15 at 00:48
  • As @BornToCode said - you probably want to add a `break` after `condition = true`. – deyur Jan 21 '15 at 00:50
  • It suggests that your condition is always being set to false before reaching that if block. Try switching the if statement in the preceding for loop to: "if(chars[x].equals(chars2[x])".rather than using the == operator. – Mic Jan 21 '15 at 00:58
  • @Mic: these are chars. Since `char`s are primitive types, the `==` is correct. – Willem Van Onsem Jan 21 '15 at 00:58
  • What are you trying to check for with condition? – Angelo Alvisi Jan 21 '15 at 01:14
  • You're right. First pass, my chars2 was identical to my chars. @aipam do you have sample input that you expect should cause that condition to pass? – Mic Jan 21 '15 at 01:15
  • @Mic well, with sequence = "aabbaa", char car = b and int n = 3 the condition should be true. With these values should be chars = aabbaa and chars2 = eebbee – aipam Jan 21 '15 at 01:26

1 Answers1

0

chars is being set inside the occorrenzeContenuto and then returned and assigned to chars2, so they will always be equal. Try swapping it for this version where I've created a new array and copied the par parameter into it at the start, then used it for the return value.

public static char[] occorrenzeContenuto(char par[], int a, char b){
char[] retVal = new char[par.length];
System.arraycopy(par,0,retVal,0,par.length);
int contatore = 0;
for(int i = 0; i < par.length; i++){
  if(par[i] != b){
    contatore++;
  }else{
  }
}
if(contatore > a){
  for(int k = 0; k < par.length; k++){
    if(par[k] != b){
      retVal[k] = determinaCarattere(a, b);
    }else{
    }
  }
}else{
}
return retVal; 
}

The reason for the problem is that array arguments maintain a reference to the original instance, like objects do as opposed to how an individual primitive like a char behaves, as explained here: Java pass by reference

Community
  • 1
  • 1
Mic
  • 3,810
  • 1
  • 13
  • 16