Error passing String to float and float to String

-1

I have a code that adds certain values to a list of Strings but it seems that when I convert them to float then to String again they are not added! What is the problem?

Code where they are added:

if(media.size()!=0){       
    medias.add(media.get(media.size()-1).text());
}

Code where they are not added:

if(media.size()!=0){
    Float media_float = Float.valueOf(media.get(media.size()-1).text());
    media_float = media_float /10;
    String media_string = String.valueOf(media_float);
    medias.add(media_string);
}

Here is the complete code:

    Thread t1 = new Thread() {
        @Override
        public void run() {
            try  {

                String site1 = "http://www.dges.gov.pt/guias/indcurso.asp?letra=";
                Document document;

                Elements lista;

                document = Jsoup.connect(site1 +letra_value).get();

                Elements boxes = document.select("div.box10");


                for (Element box : boxes) {

                    String linAreaC1 = box.select(".lin-area-c1").text();
                    String linAreaC2 = box.select(".lin-area-c2").text();
                    String linAreaC3 = box.select(".lin-area-c3").text();

                    codigoCurso.add(linAreaC1);
                    curso.add(linAreaC2);


                    Element linCurso = box.nextElementSibling();

                    while (linCurso.hasClass("lin-curso")) {
                        String linCursoC2 = linCurso.select(".lin-curso-c2").text();
                        String linCursoC3 = linCurso.select(".lin-curso-c3").text();
                        String linCursoC4 = linCurso.select(".lin-curso-c4").text();


                        codigoFaculdade.add(linCursoC2);
                        faculades.add(linCursoC3);

                        linCurso = linCurso.nextElementSibling();
                    }
                    faculades_main.add(faculades);
                    faculades = new ArrayList<String>();
                    codigosFaculdade.add(codigoFaculdade);
                    codigoFaculdade= new ArrayList<String>();
                }


                for(int contador=0;contador<faculades_main.size();contador++){
                    String codigoFaculdadi;
                    String codigoCursi;


                    codigoCursi = codigoCurso.get(contador);


                    for(int i=0;i<codigosFaculdade.get(contador).size();i++){
                        codigoFaculdadi=codigosFaculdade.get(contador).get(i);
                        String site = "http://www.dges.gov.pt/guias/detcursopi.asp?codc="+codigoCursi+"&code="+codigoFaculdadi;
                        Document document1 = Jsoup.connect(site).get();

                        Elements media = document1.select(".tvag");



                        if(media.size()!=0){
                            medias.add(media.get(media.size()-1).text());
                        }else {
                            medias.add("N/A");
                        }




                    }
                }



            } catch (Exception e) {
                e.printStackTrace();
            }
            prepareListData();



        }

    };
    
asked by anonymous 23.08.2017 / 14:54

3 answers

0

I tested this program, but I did print the contents of medias (for letter "A"). Here some of the numbers excluding the N / A:

 140,2
 141,0
 150,4
 124,8

As we can see, these texts contain a comma and therefore is not a valid float (in Java) - Float.valueOf and Float.parseFloat only accept the point > as a separator!

Strange that your code did not play a

java.lang.NumberFormatException: For input string: "140,2"

Use NumberFormat to make the conversion (possibly passing Locale ):

NumberFormat numberFormat = NumberFormat.getInstance(new Locale("pt", "BR"));

...

    String texto = media.get(media.size()-1).text();
    try {
        float media_float = numberFormat.parse(texto).floatValue();
        ...
    } catch (ParseException ex) {
        ex.printStackTrace();
    }

Note also that I am using float with small print to do the calculations ...

    
23.08.2017 / 20:55
3

Make sure that the value you receive as string can initially be converted to float (that your string initial matches only numbers). Change valorStr to your variable String correpondente

Functional code example:

String valorStr = "55";
Float media_float = Float.parseFloat(valorStr);
media_float = media_float / 10;
String media_string = Float.toString(media_float);
//medias.add(media_string); #comentei apenas para nao criar a coleção

System.out.println(media_string); //mostra 5.5
    
23.08.2017 / 15:34
0

You should use parseFloat and toString , for example:

String to float

float f = Float.parseFloat("25");

From float to String

String s = Float.toString(25.0f);
    
23.08.2017 / 14:57