1. Member
Join Date
Oct 2007
Posts
11
Rep Power
0

hey all
Im in Uni doing a Digital Media Course and one of my Mods is Programming, via BlueJ.

We get set Java problems/questions to solve every couple of weeks and im really stuck on 2 questions, just have NO idea what to do here, they dont really teach us anything as the mod is just the basics and dont really need it for next yr. anyway, here are the questions :) thanks

[1]

Write a method called convertDouble as follows:

public double convertDouble (String value) {
...
}

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.

Note also that the string may contain a standard Form number such as 2.3E-20, 6.6E-32, or -4.127E-22

Insert your convertDouble method in the space below:

AND

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

Note again that the string may contain a standard Form number such as 2.3E-20, 6.6E-32, or -4.127E-22. It may also be null
Insert your convertDouble method in the space below:

Thanks for any help :D
Last edited by sylo18; 03-09-2008 at 05:26 PM.

2. Member
Join Date
Mar 2008
Posts
4
Rep Power
0
public class Test{
public static void main (String args[]){

String value = "31246";
System.out.println(convertString(value));

}

public static double convertString(String value){
double converted = Double.parseDouble(value);
return converted;
}

}

3. Member
Join Date
Oct 2007
Posts
11
Rep Power
0
hey man
thanks for the quick reply but it didnt work.. thanks

4. Java Code:
```public class Test {
public static void main(String[] args) {
String[] s = {
"3.1415", "3.1g15", "3.1415xxx", "3.1415  ",
"2.3E-20", "6.6E-32", "-4.127E-22"
};
for(int j = 0; j < s.length; j++)
System.out.printf("convertDouble2(%s) = %f%n",
s[j], convertDouble2(s[j]));
}

private static double convertDouble(String input) {
double d = 0.0;
try {
d = Double.parseDouble(input);
} catch(NumberFormatException e) {
System.out.println("NFE: " + e.getMessage());
}
return d;
}

private static double convertDouble2(String input) {
// Remove letters at end
int pos = input.length()-1;
while(!Character.isDigit(input.charAt(pos))) {
pos--;
}
//System.out.printf("input = %s  pos = %d  ", input, pos);
input = input.substring(0, pos+1);
double d = 0.0;
try {
d = Double.parseDouble(input);
} catch(NumberFormatException e) {
System.out.println("NFE: " + e.getMessage());
}
return d;
}
}```

5. Member
Join Date
Oct 2007
Posts
11
Rep Power
0
hey
thanks again for such fast replies..
but i think ur going to advanced.. these are ment to be quite basic stuff..
and i only need part of the code.. not the full thing, if that makes sense

errm Example of a previous question and answer
QUESTION

Given a list of integers and an integer variable declared like this:

List<Integer> list;
int max;

and assuming that some values have been added to the list, write a loop which finds the largest value in list and stores it in max.

max = -12222222;
for (; i < list.size(); i++)
if (max < list.get(i))
{
max = list.get(i);
}
System.out.println(max);

so
yeah hope that helps to the sorta level/answer i need..

again thanks alot for helping :D

6. Member
Join Date
Oct 2007
Posts
11
Rep Power
0
for anyone who care the Answers Were

Q1.

public Double convertDouble(String value) {
try {
return (new Double(value));
} catch (NumberFormatException e) {
return (new Double(0.0));
}
catch (NullPointerException e) {
return (new Double(0.0));
}
}

Q2.

public double convertDouble (String value) {
try
{
Double.parseDouble(value);
}
catch (NumberFormatException e)
{
return 0.0;
}
catch (NullPointerException e) {
return (new Double(0.0));
}
return Double.parseDouble(value);
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•