473,790 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

homework help? doubles.

9 New Member
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.parseDou ble() 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 1564
sukatoa
539 Contributor
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.parseDou ble() 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(S tring) 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
2281
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 class for awhile and don't know what I'm doing. I have included my (probably wrong) answers for the first few questions. It would be great if someone could tell me what is missing or what I need to work on at least just for the first few. I...
6
1911
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 advance.
7
1740
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 structure as follows: typedef double (*Rowptr); // Holds rows of 4 doubles
28
2203
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 into R R R R R W W W W W W B B B B --------------------------------------------------------------------
38
2537
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, var / 5); return 0; }
8
29171
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 classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
24
1744
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
1550
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 are parallel to each other so their direction is same but they differ in their origin points or source location. I tried to determine the source location by creating a grid of the rectangular plane. The rays are going to be spaced from each...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.