Output deviates from START and END of java loop

I am writing code to translate signals from one form to another. My code works well but is not suitable for the purpose.

INPUT: String [] test = {"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };

      

REQUIRED OUTPUT: B / 101 B / 1 B / 106 B / 201 B / 21 B / 11

GOT OUTPUT: B / 1 B / 101 B / 1 B / 106 B / 201 B / 21

Comparison of the required output and the received output
       The first term B / 1 is not required in the received output. B / 11 at the end is missing from the required outlet.

ALGORITHM: "B" is replaced with "B /" and then numbers appearing on strings like "2", "3", "100" are added, which gives 105 and "1" must be added for "B" hence , 106 and the final result becomes "B / 106".

I am new to java and programming. I need help to get the required result.

This is my code:

public class SignalConversion { 

public static void main(String args[]) {

String [] test  ={"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };

int i=0; int x=test.length;

String netSignal="";
int total=0;

while(!(x==0)){

        StringBuilder sb_matra= new StringBuilder();
        StringBuilder sb_sur= new StringBuilder(); 

        if(!test[i].equals("B")) {   

            total=total+(Integer.valueOf(test[i])); 
        }
        else {
            total=total+1;
            sb_sur.append(test[i]+"/"+Integer.toString(total)+"  " );
            total=0; 
        } 

        netSignal=sb_sur.toString()+sb_matra.toString();

        System.out.printf(netSignal);   
        i++;
        x--;
    }
}
}

      

+3


source to share


3 answers


When you come across a "B", you should start adding the numbers following it, but only output the result when you come across the next "B". This is why you have trouble making ends. You print the first "B" when you encounter it, before calculating the number that should appear with it.

Likewise, at the end of the loop, you must add an extra B with the last amount.



Here's a potential way to do it (I think this loop is simpler than yours):

StringBuilder sb_sur= new StringBuilder(); 
boolean first = true;
for (int i = 0; i < test.length; i++) {
    if(!test[i].equals("B")) {   
        total=total+(Integer.valueOf(test[i])); 
    } else {
        if (!first) {
          total=total+1;
          sb_sur.append("B/"+Integer.toString(total)+"  " );
          total=0; 
        }
        first = false;
    }  
}
total=total+1;
// account for the last B
sb_sur.append("B/"+Integer.toString(total)+"  " );

      

+1


source


I would do it,



public static void main(String[] args) {
    String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200",
            "B", "3", "17", "B", "10" };

    boolean foundB = false;
    int total = 0;
    for(int i=0;i<test.length;i++){

        if(foundB){
            if(test[i].equals("B")){
                System.out.print("B/"+(total+1)+" ");
                total=0;
            }else{
                total += Integer.parseInt(test[i]);
            }

            if(i==(test.length-1)){
                System.out.print("B/"+(total+1)+" "); // The last B
            }
        }
        if(test[i].equals("B")){
            foundB = true; // start counting only after you find a B
        }
    }
}

      

+1


source


Oh, I see that Eran has done much the same.

    String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200", "B", "3", "17", "B", "10" };

    List<String> resultTest = new ArrayList<>();
    int value = 0;
    for (int i = 0; i < test.length; i++) {
        if (i != 0 && test[i].equalsIgnoreCase("B")) {
            resultTest.add("B\\" + (value + 1));
            value = 0;
        } else {
            if (!test[i].equalsIgnoreCase("B")) {
                value += Integer.parseInt(test[i]);
            }
        }
    }
    resultTest.add("B\\" + (value + 1));
    resultTest.forEach(System.out::println);

      

0


source







All Articles