1

I want to convert a c++ (opencv) method to java (jaavcv) method

I'm worrying about this line . Is the conversion correct?

 float label=atof(entryPath.filename().c_str());

 --> float label=Float.parseFloat(child.getName());
Andrea
  • 6,032
  • 2
  • 28
  • 55
nawara
  • 1,157
  • 3
  • 24
  • 49
  • If child.getName() will have string value which can be converted to float as it it then this is correct. http://stackoverflow.com/questions/7552660/java-convert-float-to-string-and-string-to-float – HVar Oct 25 '13 at 11:35
  • Note, however, that this may throw an exception. – Ingo Oct 25 '13 at 11:36

2 Answers2

1

Yes ,You are on track

float label = Float.parseFloat(child.getName());

where child.getName() is string and in valid float format

If you want it back

String floToStr= Float.toString(label );  

Update:

If you check docs,

That method parseFloat

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable float.  
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    For example :`suresh` is not a valid float value :) – Suresh Atta Oct 25 '13 at 11:39
  • 2
    valid float format is .5, 0.5, 1.5, 23.56233, and i think 1.4582e14 so normal notation, notation without the leading zero if there is one and scientific notation – LionC Oct 25 '13 at 11:41
0

If child.getName() returns well-formatted String (for example 1.5) then yes, it's correct.

float label = Float.parseFloat(child.getName());

If child.getName() doesn't return parsable float (for example 1,5), parseFloat() will throw NumberFormatException.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110