1

First off, thank you for taking time to read my question. I have three files that I am using to practice inheritance, however I have a question about converting strings to doubles. I have read the API on doubles and have an understanding that parseDouble would be the simplest way to go in terms of converting, but I am not sure where I can place parseDouble in the code provided below.

//Code Omitted
public Animal()
{
  name = "";
  weight = "";
  length = "";
  color = "";
}

public Animal(String n, String w, String l, String c)
{
  name = n;
  weight = w;
  length = l;
  color = c;
}

//Code Omitted The below class is an extension of my Animal class

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

//Code Omitted
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?: ");
1
  • There is nowhere in your code that you would need parseDouble(). If you were to add a method which does some calculation with the length or weight, you could use it there. Commented Nov 25, 2013 at 17:48

2 Answers 2

5

You do not need to convert strings to doubles if you are using a Scanner: it has a method that is perfect for your purposes - nextDouble() reads the next double, and returns it back to you:

System.out.print("How much does your dog weigh?: ");
if (input.hasNextDouble()) { // Add a safety check here...
    weight = input.nextDouble();
} else {
    // User did not enter a double - report an error
}
Sign up to request clarification or add additional context in comments.

Comments

2

I think the simplest way would be to use the nextDouble() method of the Scanner class :) So, instead of doing

System.out.print("What is the length of your dog?: ");
length = input.next();

you could use

System.out.print("What is the length of your dog?: ");
double length = input.nextDouble();

and pass that to your Animal class (remember to change the type of the relevant parameter though)

2 Comments

Thank you! I tried that earlier and I get the incompatible types error when I compile it. Edit: Ah! I did not try it with the double header :|. Java errors are always the simplest things.
well, your length variable is String, so you need to change that to double, also you need to change the relevant datatypes in your animal and dog classes :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.