Results 1 to 6 of 6
  1. #1
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default 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 :D
    Last edited by sylo18; 03-09-2008 at 04:26 PM.

  2. #2
    rugbyGeek is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    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. #3
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    hey man
    thanks for the quick reply but it didnt work.. thanks

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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;
        }
    }

  5. #5
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    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

  6. #6
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    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

  1. musically stuck cry for help 2
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 02:09 PM
  2. musically stuck
    By geork in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 09:44 PM
  3. Stuck and Frustrated.
    By jazzinspace in forum New To Java
    Replies: 7
    Last Post: 01-12-2008, 02:38 PM
  4. Replies: 3
    Last Post: 12-05-2007, 02:22 AM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Posting Permissions

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