Java Forum / General / October 2007
Convert from text string "Hello" to floating-point number issue
ITrishGuru - 28 Sep 2007 14:31 GMT Hi there, My java lecturer has given us an assignment related to casting and type conversion etc. One of the conversions he wan't is convert "Hello" to a float.
I suspect this is a trick question on his part as "Hello" is not a parsible value. This is the code I have: //Convert String object "s" to a Float object //then convert Float object to float value try { System.out.println("String is "+s+" float is "+Float.valueOf(s).floatValue()); }
catch (NumberFormatException NFE) { System.out.println("NumberFormatException: "+ NFE.getMessage()+ " The input string does not contain a parsable number"); } I emailed the guy but he says just to use your own judgement. I looked at the API for the wrapper classes etc and I think a conversion of this type is almost impossible. Any ideas welcome.
Matt Humphrey - 28 Sep 2007 15:12 GMT | Hi there, | My java lecturer has given us an assignment related to casting and [quoted text clipped - 17 lines] | conversion of this type is almost impossible. | Any ideas welcome. The purpose of this exercise might be to get you to think about the numbers that lay beneath constructions such as "strings" and "floats". You can traverse a string and pick off the characters. These will have numeric equivalences that you can combine into something that makes a floating point value (e.g. the letters are base 52) For practical purposes, however, the concept is nonsensical.
I think what bothers me most is his answer--he must be able to explain the point of the exercise.
Matt Humphrey http://www.iviz.com/
Mark Space - 28 Sep 2007 19:27 GMT > The purpose of this exercise might be to get you to think about the numbers > that lay beneath constructions such as "strings" and "floats". You can I think I like the original poster's code better. There is, really, no sensible way of answering the problem.
Unless the the OP can contact some fellow students and figure out how to make the assignment make sense in terms of recent lecture material or whatever, I'd just use the OP's code and move on to more important things.
Seriously, how often do you have to write your own base number conversions? I'd rather tackle a nasty tree update problem than tripe like this.
Matt Humphrey - 28 Sep 2007 20:04 GMT | > The purpose of this exercise might be to get you to think about the numbers | > that lay beneath constructions such as "strings" and "floats". You can | | I think I like the original poster's code better. There is, really, no | sensible way of answering the problem. I did say that in my answer and I agree that the poster's code is reasonable. Very likely the only point to the exercise is that input data needs to be validated.
| Unless the the OP can contact some fellow students and figure out how to | make the assignment make sense in terms of recent lecture material or [quoted text clipped - 3 lines] | conversions? I'd rather tackle a nasty tree update problem than tripe | like this. I think it's perfectly reasonable to assign an exercise that teaches a concept but for which you'll never actually implement in real life. It looks like a typical first-year exercise to me.
Matt Humphrey http://www.iviz.com/
Lew - 29 Sep 2007 07:39 GMT > I think it's perfectly reasonable to assign an exercise that teaches a > concept but for which you'll never actually implement in real life. It > looks like a typical first-year exercise to me. It looks like a typical customer requirements doc to me.
It's not so irrelevant to the real world in that way.
I had an interviewer ask me to implement an algorithm that assigned moentary values to letters in words. He instructed me that "'A' is worth one cent, 'B' two, and so on."
I asked, "What's 'C' worth?"
He looked suprised. "Just continue the series!"
"One, two, three, four, five, etc., or one, two, four, eight, sixteen, etc.?"
> convert "Hello" to a float. float f = "Hello".hashCode();
 Signature Lew
Andrew Thompson - 29 Sep 2007 08:21 GMT ....
>I had an interviewer ask me to implement an algorithm that assigned moentary >values to letters in words. He instructed me that "'A' is worth one cent, 'B' [quoted text clipped - 5 lines] > >"One, two, .. ..four, eight, sixteen. ;-)
 Signature Andrew Thompson http://www.athompson.info/andrew/
Andrew Thompson - 29 Sep 2007 11:54 GMT >.... >>I had an interviewer ask me to implement an algorithm that assigned moentary [quoted text clipped - 4 lines] > >..four, eight, sixteen. ;-) A pity it was not "Zero, one, .."
<sscce> class SillySeries {
static void seriesDoubledMinusOne (int number) { System.out.println("Series Doubled (minus one)"); int previous = 1; for (int ii=0; ii<number-1; ii++) { System.out.print(previous-1 + ", "); previous*=2; } System.out.println(previous-1); }
static void seriesSquared (int number) { System.out.println("Series Squared"); for (int ii=0; ii<number-1; ii++) { System.out.print((int)Math.pow(ii,2) + ", "); } System.out.println((int)Math.pow(number,2)); }
static void seriesCubed (int number) { System.out.println("Series Cubed"); for (int ii=0; ii<number-1; ii++) { System.out.print((int)Math.pow(ii,3) + ", "); } System.out.println((int)Math.pow(number,3)); }
static void seriesOddEvenNegativeFlip(int number) { System.out.println("Series Odd/Even (With Negative Flip)"); for (int ii=0; ii<number; ii++) { int result = (ii%2==0 ? -ii : ii); System.out.print( result + ", "); } int result = (number%2==0 ? -number : number); System.out.println(result); }
static void seriesUpDown(int number) { System.out.println("Series Up/Down"); for (int ii=0; ii<number; ii++) { System.out.print( ii%2 + ", "); } System.out.println(number%2); }
public static void main(String[] args) { int members = 10; seriesDoubledMinusOne(members); seriesSquared(members); seriesCubed(members); seriesOddEvenNegativeFlip(members); seriesUpDown(members); } } </sscce>
..and yes, I do *really* need to find better uses for my time.
 Signature Andrew Thompson http://www.athompson.info/andrew/
Tris Orendorff - 29 Sep 2007 17:28 GMT [...]
> .and yes, I do *really* need to find better uses > for my time. Perhaps "The On-Line Encyclopedia of Integer Sequences," http://www.research.att.com/~njas/sequences/, will help?
 Signature Tris Orendorff [ Anyone naming their child should spend a few minutes checking rhyming slang and dodgy sounding names. Brad and Angelina failed to do this when naming their kid Shiloh Pitt. At some point, someone at school is going to spoonerise her name. Craig Stark]
Andrew Thompson - 29 Sep 2007 18:46 GMT >[...] >> .and yes, I do *really* need to find better uses >> for my time. > >Perhaps "The On-Line Encyclopedia of Integer Sequences," http://www.research.att.com/~njas/sequences/, >will help? Or perhaps a drug treatment prescribed by a qualified psychiatrist?
I note hat when I visit that page, remove all the numbers after one and two, do the search and wait a while, it comes up with 3692 possiblilites or (frowns) is that 3692 *pages* of possibilities?
I think I'll take the 'drug option'!
 Signature Andrew Thompson http://www.athompson.info/andrew/
Andreas Leitgeb - 03 Oct 2007 11:33 GMT >>I had an interviewer ask me to implement an algorithm that assigned moentary >>values to letters in words. He instructed me that "'A' is worth one cent, 'B' [quoted text clipped - 3 lines] >>"One, two, .. > .four, eight, sixteen. ;-) One, two, three, five, eight, thirteen, twentyone, ...
appears selfsuggesting to me.
Roedy Green - 29 Sep 2007 00:56 GMT >My java lecturer has given us an assignment related to casting and >type conversion etc. >One of the conversions he wan't is convert "Hello" to a float. I think he is being a smart a.s, but for the answer see Double.bitsToDouble.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 29 Sep 2007 02:56 GMT >One of the conversions he wan't is convert "Hello" to a float. Throw a metaphorical pie in the guy's face. Why waste time with silly problems like this when there are so many real world problems to tackle where you could actually make use of the result?
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Mark Space - 29 Sep 2007 21:09 GMT >> One of the conversions he wan't is convert "Hello" to a float. I missed the "one of" part earlier. Yes, validating data is important, that's probably what the lesson here is.
Assuming that validating is the intent, what's the best answer?
Converting "hello" to 0 isn't right. Even NaN may have implications that aren't warranted by the input. I'd convert it to mull maybe, but also make sure to catch the exception and indicate a problem.
For an assignment, I'd continue to accept input after catching the exception. In real life, I might propagate the error up or halt the program. If instead of a file of numerical values, the input is something else, the user probably made an error and needs to be notified right away.
ITrishGuru - 01 Oct 2007 23:54 GMT > >> One of the conversions he wan't is convert "Hello" to a float. > [quoted text clipped - 12 lines] > something else, the user probably made an error and needs to be notified > right away. Thanks Guys! I'll include multiple answers to my lecturers question, I hope that impresses him!!
ITrishGuru - 02 Oct 2007 00:10 GMT > >> One of the conversions he wan't is convert "Hello" to a float. > [quoted text clipped - 12 lines] > something else, the user probably made an error and needs to be notified > right away. Thanks again, I'll go with these two options and hope it looks smart
//convert String Object "s" to a float primitive //compute the hash code of "s" then cast as a float System.out.println((float)s.hashCode());
//try catch block// //Convert String Object "s" to a Float Object //then convert Float Object to float value try { System.out.println("String is "+s+" float is "+ Float.valueOf(s).floatValue()); }
catch (NumberFormatException NFE) { System.out.println("NumberFormatException: "+ NFE.getMessage()+ " The input string does not contain a parsable number"); }
//end of try catch block//
Andrew Thompson - 03 Oct 2007 13:59 GMT >> >> One of the conversions he wan't is convert "Hello" to a float. ...
>I'll go with these two options and hope it looks smart (snip)
Looking at your end result, I suspect you have made good choices. The code covers two viable interpretations, and handles each appropriately.
You might make a comment about the possibility of 'identical hashcodes' for the String objects.
 Signature Andrew Thompson http://www.athompson.info/andrew/
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|