Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Convert from text string "Hello" to floating-point number issue

Reply
Thread Tools

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

 
 
ITrishGuru
Guest
Posts: n/a
 
      09-28-2007
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.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      09-28-2007

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


 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      09-28-2007
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.
 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      09-28-2007

"Mark Space" <> wrote in message
newsIbLi.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/


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2007
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2007
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-29-2007
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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-29-2007
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.asp...neral/200709/1

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-29-2007
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.asp...neral/200709/1

 
Reply With Quote
 
Tris Orendorff
Guest
Posts: n/a
 
      09-29-2007
"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]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert string number to real number - ValueError: invalid literal davidj411 Python 11 05-29-2008 12:02 AM
convert decimal number in a hexadecimal number ? muss C Programming 13 03-27-2006 08:14 AM
How to convert a number to hex number? Hako Python 13 11-10-2005 01:56 AM
HELP - how to convert a string representation of a number to a number ? cpptutor2000@yahoo.com C++ 5 05-21-2005 05:56 PM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57