How to check and take the value 0 or 1 of a string and save that number to that column?

Asked

Viewed 74 times

0

I have to keep the number 0 in a variable desc and 1 in the variable asc, if one of these values is found in the string.

How do I do it?

package testes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Ordenar {

public static void main(String[] args) {

      String desc = null;
      String asc = null;
      String coluna = null;
      List list = new ArrayList(); 
      String sampleString = "jrp_jasper.jrp_jas_nome|0;jrp_jasper.jrp_jas_sobrenome|1";

      String[] items = sampleString.split(";");
     // List<String> itemList = Arrays.asList(items);
      for(int i = 0; i < items.length; i++){

         if(sampleString.charAt(i) == '0'){
             desc = "0";
             System.out.println(desc);
         }else{
             asc = "1";
             System.out.println(asc);
         }

         coluna = items[i].replaceAll("[|01]", "");
         Map map = new HashMap();
         map.put(coluna, asc);
         list.add(coluna);

         // ....
  • Use equals and not equal sign when comparing strings. Another thing, the 0 and 1 are not separated by point and comma, the split will not locate them. Not to mention that your loop is on top of the list separated by period and comma, the if won’t run through all the chars.

  • Give me a better idea? Please...

  • The best way to compare Java Strings is to use . equals() "Stringquevcquerto compare.equals(Stringqueseracomparative)".

1 answer

5


Exchange the validation of your if, using contains over a string array item items:

if(items[i].contains("0")){
    desc = "0";
    System.out.println(desc);
}else if((items[i].contains("1"))){
    asc = "1";
    System.out.println(asc);
}
  • I’ll try to wait....

  • WORKED VALEEEEEEEEEEEEEEEEEEEEEU.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.