Convert from text string "Hello" to floating-point number issue

Discussion in 'Java' started by ITrishGuru, Sep 28, 2007.

  1. ITrishGuru

    ITrishGuru Guest

    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.
    ITrishGuru, Sep 28, 2007
    #1
    1. Advertising

  2. "ITrishGuru" <> wrote in message
    news:...
    | 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.

    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/
    Matt Humphrey, Sep 28, 2007
    #2
    1. Advertising

  3. ITrishGuru

    Mark Space Guest

    Matt Humphrey wrote:

    > 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.
    Mark Space, Sep 28, 2007
    #3
  4. "Mark Space" <> wrote in message
    news:eek:IbLi.1013$...
    | Matt Humphrey wrote:
    |
    | > 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
    | 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.

    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/
    Matt Humphrey, Sep 28, 2007
    #4
  5. ITrishGuru

    Roedy Green Guest

    On Fri, 28 Sep 2007 13:31:15 -0000, ITrishGuru <>
    wrote, quoted or indirectly quoted someone who said :

    >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 ass, but for the answer see
    Double.bitsToDouble.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 29, 2007
    #5
  6. ITrishGuru

    Roedy Green Guest

    On Fri, 28 Sep 2007 13:31:15 -0000, ITrishGuru <>
    wrote, quoted or indirectly quoted someone who said :

    >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?

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 29, 2007
    #6
  7. ITrishGuru

    Lew Guest

    Matt Humphrey wrote:
    > 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();

    --
    Lew
    Lew, Sep 29, 2007
    #7
  8. Lew wrote:
    ....
    >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, ..


    ..four, eight, sixteen. ;-)

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
    Andrew Thompson, Sep 29, 2007
    #8
  9. Andrew Thompson wrote:
    >....
    >>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. ;-)


    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.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
    Andrew Thompson, Sep 29, 2007
    #9
  10. "Andrew Thompson" <u32984@uwe> burped up warm pablum in news:78f01647f9361@uwe:
    [...]
    > .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?

    --
    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]
    Tris Orendorff, Sep 29, 2007
    #10
  11. Tris Orendorff wrote:
    >[...]
    >> .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'!

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via http://www.javakb.com
    Andrew Thompson, Sep 29, 2007
    #11
  12. ITrishGuru

    Mark Space Guest

    Roedy Green wrote:
    > On Fri, 28 Sep 2007 13:31:15 -0000, ITrishGuru <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> 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.
    Mark Space, Sep 29, 2007
    #12
  13. ITrishGuru

    ITrishGuru Guest

    On Sep 29, 9:09 pm, Mark Space <> wrote:
    > Roedy Green wrote:
    > > On Fri, 28 Sep 2007 13:31:15 -0000, ITrishGuru <>
    > > wrote, quoted or indirectly quoted someone who said :

    >
    > >> 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.


    Thanks Guys! I'll include multiple answers to my lecturers question, I
    hope that impresses him!!
    ITrishGuru, Oct 1, 2007
    #13
  14. ITrishGuru

    ITrishGuru Guest

    On Sep 29, 9:09 pm, Mark Space <> wrote:
    > Roedy Green wrote:
    > > On Fri, 28 Sep 2007 13:31:15 -0000, ITrishGuru <>
    > > wrote, quoted or indirectly quoted someone who said :

    >
    > >> 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.



    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//
    ITrishGuru, Oct 2, 2007
    #14
  15. Andrew Thompson <u32984@uwe> wrote:
    > Lew wrote:
    >>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, ..

    > .four, eight, sixteen. ;-)


    One, two, three, five, eight, thirteen, twentyone, ...

    appears selfsuggesting to me.
    Andreas Leitgeb, Oct 3, 2007
    #15
  16. ITrishGuru wrote:
    >> >> 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.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
    Andrew Thompson, Oct 3, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fei Liu
    Replies:
    9
    Views:
    441
  2. Roy
    Replies:
    6
    Views:
    610
    Roedy Green
    Jan 7, 2008
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,322
    Saraswati lakki
    Jan 6, 2012
  4. teeshift
    Replies:
    2
    Views:
    255
    Chris Pearl
    Dec 1, 2006
  5. SamL
    Replies:
    4
    Views:
    156
Loading...

Share This Page