Results 1 to 6 of 6
- 03-09-2008, 02:54 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 11
- Rep Power
- 0
Stuck on Two Questions, Please Help
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 :DLast edited by sylo18; 03-09-2008 at 04:26 PM.
- 03-09-2008, 04:08 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;
}
}
- 03-09-2008, 04:27 PM #3
Member
- Join Date
- Oct 2007
- Posts
- 11
- Rep Power
- 0
hey man
thanks for the quick reply but it didnt work.. thanks
- 03-09-2008, 08:12 PM #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); //System.out.printf("adjusted input = %s%n", input); double d = 0.0; try { d = Double.parseDouble(input); } catch(NumberFormatException e) { System.out.println("NFE: " + e.getMessage()); } return d; } }
- 03-10-2008, 12:49 AM #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.
ANSWER
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
- 03-11-2008, 01:03 AM #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);
}
Similar Threads
-
musically stuck cry for help 2
By geork in forum New To JavaReplies: 0Last Post: 02-07-2008, 02:09 PM -
musically stuck
By geork in forum New To JavaReplies: 1Last Post: 02-06-2008, 09:44 PM -
Stuck and Frustrated.
By jazzinspace in forum New To JavaReplies: 7Last Post: 01-12-2008, 02:38 PM -
hi there i am stuck with a construct can anyone help???
By sonal in forum New To JavaReplies: 3Last Post: 12-05-2007, 02:22 AM -
I am completely stuck
By jpnym15 in forum New To JavaReplies: 2Last Post: 11-14-2007, 06:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks