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

I

ITrishGuru

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

Matt Humphrey

| 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/
 
M

Mark Space

Matt said:
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.
 
M

Matt Humphrey

| 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/
 
R

Roedy Green

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

Roedy Green

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

Lew

Matt said:
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();
 
A

Andrew Thompson

A

Andrew Thompson

Andrew said:
....
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
 
T

Tris Orendorff

Andrew Thompson said:
.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]
 
A

Andrew Thompson

Tris said:
[...]
.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'!
 
M

Mark Space

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

ITrishGuru

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!!
 
I

ITrishGuru

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//
 
A

Andrew Thompson

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top