Monday, November 25, 2013

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?

No comments:

Post a Comment