Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2008, 03:54 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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

Last edited by sylo18 : 03-09-2008 at 05:26 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-09-2008, 05:08 PM
Member
 
Join Date: Mar 2008
Posts: 4
rugbyGeek is on a distinguished road
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;
}


}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-09-2008, 05:27 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
hey man
thanks for the quick reply but it didnt work.. thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-09-2008, 09:12 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-10-2008, 01:49 AM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-11-2008, 02:03 AM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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);
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
musically stuck cry for help 2 geork New To Java 0 02-07-2008 03:09 PM
musically stuck geork New To Java 1 02-06-2008 10:44 PM
Stuck and Frustrated. jazzinspace New To Java 7 01-12-2008 03:38 PM
hi there i am stuck with a construct can anyone help??? sonal New To Java 3 12-05-2007 03:22 AM
I am completely stuck jpnym15 New To Java 2 11-14-2007 07:40 PM


All times are GMT +3. The time now is 06:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org