473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

homework help? doubles.

Hey, i was wondering if anyone could help me.
i have two questions that i cannot complete for a homework assignment:

[1] This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDouble() to do the hard work for you.
--------------------------------------------------------------------------------

[2] Modify your convertDouble method to ignore any extra characters at the end of the string, so that convertDouble("3.1415xxx") will produce the value 3.1415 as its result.


Here is the coding that i have also done so far:

[1]
Expand|Select|Wrap|Line Numbers
  1. public double convertDouble (String value) 
  2.     {
  3. while(true) 
  4. {
  5.   try 
  6. {
  7.     int number = Integer.parseInt(value);
  8.     break;
  9.   }
  10.  catch(NumberFormatException e) 
  11. {
  12.     value = "0";
  13.     }
  14. }
  15. return Double.parseDouble(value);
  16. }
With this question i get an error when i try to type "-1.12E8-15" , i should get "-1.12E8-15" returned. Instead i get 0.

[2]
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public double convertDouble (String value) 
  3.     {
  4.             for(int i = 0; i < value.length();i++)
  5.             {
  6.  
  7.         value = value.replaceAll("\\D*$","");
  8.         }
  9.     return Double.parseDouble(value);
  10.     }
With the above question, when an empty string is entered ("") i get an error. Also when "null" is entered i get an error. both of these should return 0.

I have been working on this for a while now and any help would be appreciated.

thanks in advance.
Feb 24 '08 #1
1 1548
sukatoa
539 512MB
Hey, i was wondering if anyone could help me.
i have two questions that i cannot complete for a homework assignment:

[1] This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDouble() to do the hard work for you.
--------------------------------------------------------------------------------

[2] Modify your convertDouble method to ignore any extra characters at the end of the string, so that convertDouble("3.1415xxx") will produce the value 3.1415 as its result.


Here is the coding that i have also done so far:

[1]
Expand|Select|Wrap|Line Numbers
  1. public double convertDouble (String value) 
  2.     {
  3. while(true) 
  4. {
  5.   try 
  6. {
  7.     int number = Integer.parseInt(value);
  8.     break;
  9.   }
  10.  catch(NumberFormatException e) 
  11. {
  12.     value = "0";
  13.     }
  14. }
  15. return Double.parseDouble(value);
  16. }
With this question i get an error when i try to type "-1.12E8-15" , i should get "-1.12E8-15" returned. Instead i get 0.

[2]
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public double convertDouble (String value) 
  3.     {
  4.             for(int i = 0; i < value.length();i++)
  5.             {
  6.  
  7.         value = value.replaceAll("\\D*$","");
  8.         }
  9.     return Double.parseDouble(value);
  10.     }
With the above question, when an empty string is entered ("") i get an error. Also when "null" is entered i get an error. both of these should return 0.

I have been working on this for a while now and any help would be appreciated.

thanks in advance.
@ convertDouble(String) why use break? what's the purpose?

"-1.12E8-15"?
maybe "-1.12E(-7)"
is equivalent to -1.12 x 10 raise to the power of -7
and it is also equivalent to -0.000000112, and if you format it with %.10f, then you can see the value... instead of zero....

use try/catch when converting doubles....
Exception will occured when convert null or "", then if it happens, try to set value = '0'

Correct me if im wrong,
Sukatoa (Shadow Shaman)
Feb 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
6
by: Rex_chaos | last post by:
I have allocated a array like double a; We know that a 1-D array just like an 1-D pointer. So I wonder that should I release the memory of the array myself like free(a); Thanks in...
7
by: Jim | last post by:
Hi people. I was hoping someone could help me as this is driving me up the wall. I'm trying to write a program that deals with matrix multiplication. The Program uses a couple of typedefined...
28
by: Bailey.W87 | last post by:
my professor give me this assignment. Sort the R's B's and W's in an array. for example, the user enter: R B W W B B R W W R R W R B W i need to swap the characters in the array and arrange it...
38
by: Jordi | last post by:
I have made a little C program that generates the following output: 227.000000 / 5 = 45.400002 Here's the code: int main (int argc, char* argv) { float var = 227; printf("%f / 5 = %f\n", var,...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
24
by: Three Headed Monkey | last post by:
write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1))))))) I tried #include <stdio.h> int pow(int n) { int i,power; power=n;
6
by: pereges | last post by:
Ok, I have some problem with arrays which i want to use for storing rays in my ray tracing project. please have a little patience to read. I need to fire rays from a a rectangular plane. The rays...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.