0
long l = ((File)localObject).length();
  if (l >= 1024L)
  {
    if (l >= 1048576L)
    {
      if (l >= 1073741824L)
        str = Float.toString(Math.round(100.0F * ((float)l / 1.073742E+09F)) / 100.0F) + " GB";
      else
        str = Float.toString(Math.round(100.0F * ((float)str / 1048576.0F)) / 100.0F) + " MB";
    }
    else
      str = Float.toString(Math.round(100.0F * ((float)str / 1024.0F)) / 100.0F) + " KB";
  }
  else
    str = Long.toString(str) + " B";

else str = Long.toOctalString(str) + " B"; } return (String)str; }

Another error in 16 th Here i got the error in 9 th line and 12 th line Cannot cast from String to float in java

3
  • What causes you to think a float cast of a string should work? What if the string has an illegal format ("ABCNOTA NUMBER")? How can a cast work with bad data? Have you seen documentation or a tutorial that makes it look like this should work? Commented Feb 17, 2012 at 12:12
  • 1
    There's an error on the last line: maybe you should write str = Long.toString(l) + " B" Commented Feb 17, 2012 at 12:17
  • 2
    Also, please read the book "Clean code" by R.C. Martin. Your code is horrible to read with nested if-else-statements, magic numbers, nested arguments. What if a team member is to understand this code? That is impossible. I even think that in a week or two you won't be able to understand it yourself. Commented Feb 17, 2012 at 12:21

5 Answers 5

7

I suppose str is a String, in which case you need to use Float.valueOf(str) instead of (float)str.

Or more likely you meant (float)l instead of (float)str.

And as mentioned by enzom83, your last line should probably be:

str = Long.toString(l) + " B";
Sign up to request clarification or add additional context in comments.

1 Comment

here i got the error in line str = Long.toString(str) + " B"; The method toString(long) in the type Long is not applicable for the arguments (String)
3

use parseFloat method Float.parseFloat(str)

Comments

2

Looks like you are trying to cast the String str to a float. Probably you have a mistake and you are supposed to use the l variable instead.

Comments

2

You can't use (float)str when str is a String.

Replace it with this (and catch possible exceptions!): Float.valueOf(str)

Comments

1

Hm, sorry for my English

If str is a String that you can't convert into a float like this (float)str, you must use a Float method called parseFloat(String s), which returns a new float initialized to the value represented by the specified String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.