Error passing from String to float and from float to String

Asked

Viewed 421 times

0

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

Code in which they are added :

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

Code in which 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();



        }

    };
  • 1

    I did a test and it worked perfectly your example, are you sure the variable media_float is being filled correctly?

  • Yes the way I said in the first example of , but in the second no , I do not understand

  • If you want I can post the whole code

  • Do this, edit your post please show us where you create this structure media and where it is filled

  • Elements is a structure you implemented?

  • Are you sure the String variable is not getting null?

Show 1 more comment

3 answers

2

Be sure that the value you receive as string initially can be converted into float(that your string initial corresponds to only numbers). Exchange the valorStr for your variable String corresponding

Example of functional code:

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
  • I’m sure it’s ! I don’t understand why you’re not adding anything to the list..

  • Certainly, I didn’t put it together because I believe the right place to test this is at the time of popular the collection

0

You must use the parseFloat and toString, example:

String to float

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

From float to String

String s = Float.toString(25.0f);

0


I tested this program, but I had the contents of medias (to letter "A"). Here are some of the numbers excluding 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) - o Float.valueOf and the Float.parseFloat only accept the dot as a separator!

Strange that your code didn’t throw a

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

Use the NumberFormat to make the conversion (eventually passing a 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();
    }

also note that I am using the float with small letter to make the calculations...

Browser other questions tagged

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