How to remove the last plus "+" that appears to me in the console starting from this code?

2

Can someone tell me how to get the last "+" that appears on the console from this code:

import java.util.Scanner;
public class Power2 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Valor [1..2^31] ?"); 
        int valor = input.nextInt();
        for(int i=0;valor<0;i++){
            System.out.println("Valor [1..2^31] ?"); 
            valor=input.nextInt();
        }
        int[] array=new int[31];
        int[] arrayvalor=new int[31];
        int resto;
        int ndivisoes=0;

        for(int i=0;valor>0;i++){    
            resto=(valor%2);
            System.out.println("resto= "+resto);
            valor=valor/2;
            System.out.println(valor);
            array[i]=resto;
            ndivisoes=i;
            arrayvalor[i]=valor;
            //System.out.println(arrayvalor[i]);
            //System.out.println("i= "+i);
        }
        int count=ndivisoes;

        for(int j=ndivisoes;j>=0;j--){
            String soma=" ";
            if(array[j]==1){
                if(j==0){
                    soma=" ";
                    System.out.print(2+"^"+j+soma);    
                }else{
                    soma="+";
                    System.out.print(2+"^"+j+soma);

                }                      
            }
        }
    }
}
    
asked by anonymous 26.04.2014 / 02:15

4 answers

2

Simplified solution:

   ...
}
int count=ndivisoes;
// Mantive a linha acima, que não é usada, apenas para você ver de onde eu comecei o código.

String soma="";
for(int j=ndivisoes;j>=0;j--){
    if(array[j]==1){
        System.out.print(soma+"2^"+j);
        soma="+"; // por estética pode usar soma=" + "; se preferir
    }
}

Click here to see the result on IDEONE IDEONE .

    
26.04.2014 / 05:26
0

From what I understand in your code, it works fine when the smallest value of j for which array[j] == 1 is 0 , and works poorly (displays + more) when lower j for which array[j] == 1 is > 0 .

So you do not really want to use the condition if (j == 0) , but if (j == x) where x is the smallest value of j to which array[j] == 1 .

My suggestion is that you create a method that receives array and returns the lowest position of array to which array[posição] == 1 , and use that result in your if .

I do not know if my logic is the best way to do it, but it illustrates the problem and you can work on the code to improve it from what this logic is showing.

    
26.04.2014 / 02:41
0

You can resolve this problem by not adding% with% and instead adding spaces. Then you make a + to delete the possible spaces after the value and replace the spaces with trim() .

I did the following here:

    String soma=" ";

    for(int j=ndivisoes;j>=0;j--)
    {
        if(array[j]==1)
        {
            soma += "2^" + j + " ";
        }
    }

    soma = soma.trim();
    soma = soma.replace(' ', '+');
    System.out.println(soma);

The output was:

Valor [1..2^31] ?
6
resto= 0
3
resto= 1
1
resto= 1
0
2^2+2^1
    
26.04.2014 / 02:49
0

Nothing guarantees that array[j] == 1 in the last iteration of the loop, and this is the necessary condition for you to define the variable soma as " " .

My approach to the problem considers the first impression on the output to be special, not the last one. Because we know that after something has already been sent to the screen we can put a "+" ahead of the next things we are going to print.

In this way, it is enough to use a flag to signal when something has been printed.

See the corrected algorithm:

boolean foiImpresso = false;
for (int j = ndivisoes; j >= 0; j--) {
    if (array[j] == 1) {
        if (foiImpresso) {
            System.out.print(" + 2^" + j);
        } else {
            System.out.print("2^" + j);
            foiImpresso = true;
        }
    }
}

Note that variable soma has not been used since it is completely unnecessary. Take into consideration that I have not tested this algorithm, I'm just assuming it's correct.

    
26.04.2014 / 03:23