Results 1 to 10 of 10
  1. #1
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Convert String to double?

    I have three java source files Dog.java, Animal.java, and DogApp.java
    Java Code:
    public class Dog extends Animal
    {
    private String breed;
    private String sound;
    
    	public Dog()
    	{
    	super();
    	breed = "";
    	sound = "";
    	}
    	public Dog(String n, String w, String l, String c, String b, String s)
    	{
            super(n,w,l,c);
    	name = n;
    	weight = w;
    	length = l;
    	color = c;
    	breed = b;
    	sound = s;
    	}
    	public String getName()
    	{
    	return name;
    	}
    	public String getWeight()
    	{
    	return weight;
    	}
    	public String getLength()
    	{
    	return length;
    	}
    	public String getColor()
    	{
    	return color;
    	}
    	public String getBreed()
    	{
    	return breed;
    	}
    	public String getSound()
    	{
            return sound;
    	}
    }
    Java Code:
    import java.util.Scanner;
    public class DogApp
    {
    	public static void main(String [] args)
    	{
    	String name, weight, breed, length, sound, color;
    	Scanner input = new Scanner(System.in);
    	System.out.print("Please name your dog: ");
    	name = input.next();
    	System.out.print("What color is your dog? (One color only): ");
    	color = input.next();
    	System.out.print("What breed is your dog? (One breed only): ");
    	breed = input.next();
    	System.out.print("What sound does your dog make?: ");
    	sound = input.next();
    	System.out.print("What is the length of your dog?: ");
    	length = input.next();
    	System.out.print("How much does your dog weigh?: ");
    	weight = input.next();
    	Dog a_dog = new Dog(name, weight, length, color, breed, sound);
    	System.out.println("\nThe name of the dog is " + a_dog.getName() + ".");
    	System.out.println("The color of the dog is " + a_dog.getColor() + ".");
    	System.out.println("The breed of the dog is " + a_dog.getBreed() + ".");
    	System.out.println("The sound of the dog is " + a_dog.getSound() + ".");
    	System.out.println("The length of the dog is " + a_dog.getLength() + ".");
    	System.out.println("The weight of the dog is " + a_dog.getWeight() + ".");
    	}
    Java Code:
    public class Animal extends Object
    {
    protected String name;
    protected String weight;
    protected String length;
    protected String color;
    
    	public Animal()
    	{
    	name = "";
    	weight = "";
    	length = "";
    	color = "";
    	}
    public Animal(String n, String w, String l, String c)
    {
    name = n;
    weight = w;
    length = l;
    color = c;
    }
    }
    Everything compiles fine, my answers however must be in double format. What is the simplest way to convert these strings to doubles?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Convert String to double?

    Did you read the API documentationn for the Double class? That class has a parseDouble( ... ) method that can do the job. But why keep the weight and length as Strings in the first place?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Re: Convert String to double?

    Thanks Jos. My professor for some reason wants them as Strings, not the slightest clue why my friend. I have read over the parseDouble method a couple of times. I know I have to change it in all three files. For instance, would I use it like so?
    Java Code:
    Double.parseDouble(length);
    after line 32 in the dog file?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Convert String to double?

    I'd convert those Strings to doubles in the constructor and store those instead; everytime you ask the length for a Dog object, you can return the same value over and over again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Convert String to double?

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Convert String to double?

    Also, all classes implicitly extend Object so you don't really need it in the Animal class. And a lot of the methods that you include for Dog, also apply to Animals. So why not put the get and set methods in the Animal class? You can always override them if necessary. Lastly, reassigning name, weight, length, and color in your Dog class is not necessary. That is done for you by the constructor which you invoke via super.

    Regards,
    Jim
    Last edited by jim829; 11-25-2013 at 09:38 PM. Reason: s/wieght/weight
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Re: Convert String to double?

    Quote Originally Posted by Norm View Post
    Agreed Norm, I did post the question in several places, and I received vastly different answers. I see nothing wrong with asking a question in more than one location. I am curious, and I do get a little impatient at times. I find it better to ask how to do something more than once, rather than demand it be done for me by someone else.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Convert String to double?

    There is no problem posting a question on several sites.
    There is a problem with not telling each site about the others. It can make for wasted efforts for some one on this forum to work on a problem and enter a post that has already been answered on another site. Many of us don't like wasting time if a question has already been answered.

    In the future I'll wait to post on your questions knowing that you are probably getting answers from another site.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Re: Convert String to double?

    With all things considered that is fine, Norm. I didn't mean to offend anyone, or waste anyone's time - I just had a question.
    Cheers mate.

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Convert String to double?

    The solution is simple. Just show where this has been posted and provide the link(s). So if you post at five different sites, each post should have four links to the others posts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 8
    Last Post: 09-22-2013, 01:20 AM
  2. Replies: 3
    Last Post: 02-10-2012, 07:26 PM
  3. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 06:27 PM
  4. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 03:46 AM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 AM

Posting Permissions

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