Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

string to double JAVA Dont know how to do this. how to get these formulas to wor

ID: 665330 • Letter: S

Question

string to double JAVA

Dont know how to do this.

how to get these formulas to work

        //formulas
        area= width * length;
        perimeter= 2 * width + 2 * length;
        double area = Double.parseDouble(area);

The double was my attempt at it.

package assignment4;
import java.util.Scanner;
import java.util.*;
import java.text.*;


/**
*
*/
public class Assignment4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Welcome to the Area and Perimeter Calculator ");

        Scanner sc = new Scanner(System.in);
        String length, width, area, perimeter;
           
        //formulas
        area= width * length;
        perimeter= 2 * width + 2 * length;
        double area = Double.parseDouble(area);

        String choice = "y";

        while (choice.equalsIgnoreCase("y"))

        {

            // get the input from the user
        System.out.print("Enter length: ");
        length = sc.next();
      
        System.out.print("Enter width: ");
        width = sc.next();

      
        //calucated and displayed to user
        System.out.println("Area: " + area);
      
        System.out.println("Perimeter: " + perimeter);
      
        //ask user if they want to contine
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        sc.nextLine(); // discard any other data entered on the line
        System.out.println();
      
        }      
    }

    public static double getDoubleWithinRange(Scanner sc, String prompt,

    double min, double max)

    {

        double d = 0.0;

        boolean isValid = false;

        while (isValid == false)

        {

            d = getDouble(sc, prompt);

            if (d <= min)

                System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (d >= max)

                System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

                isValid = true;

        }

        return d;

    }

    public static double getDouble(Scanner sc, String prompt)

    {

        double d = 0.0;

        boolean isValid = false;

        while (isValid == false)

        {

            System.out.print(prompt);

            if (sc.hasNextDouble())

            {

                d = sc.nextDouble();

                isValid = true;

            }

            else

            {

                System.out.println("Error! Invalid decimal value. Try again.");

            }

            sc.nextLine(); // discard any other data entered on the line

        }

        return d;

    }

    public static int getIntWithinRange(Scanner sc, String prompt,

    int min, int max)

    {

        int i = 0;

        boolean isValid = false;

        while (isValid == false)

        {

            i = getInt(sc, prompt);

            if (i <= min)

                System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (i >= max)

                System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

                isValid = true;

        }

        return i;

    }

    public static int getInt(Scanner sc, String prompt)

    {

        int i = 0;

        boolean isValid = false;

        while (isValid == false)

        {

            System.out.print(prompt);

            if (sc.hasNextInt())

            {

                i = sc.nextInt();

                isValid = true;

            }

            else

            {

                System.out.println("Error! Invalid integer value. Try again.");

            }

            sc.nextLine(); // discard any other data entered on the line

        }

        return i;

    }

}

Explanation / Answer

// The initial compilation of the above code in java compiler gave 4 errors:

Error 1: Line 22: Bad operand type for binary operator '*'    area = width * length

// This is obvious, because the code is trying to multiply two string variables width and area

// only integer, float and double data types can be used in arithmetic operations like multiply, divide, add a,d subtraction

Error 2: Line 23: Bad operand type for binary operator '*'    2 * width    - width is a string can not *

Error 3: Line 23: Bad operand type for binary operator '*'    2 * length   - length is a string - can not *

Error 4: Line 24: Variable arae is already defined in method main(String[]) - duplicate declaratio

// Here is the corrected code - it worked ok and produced the expected output:

//package assignment4;
import java.util.Scanner;
import java.util.*;
import java.text.*;
public class Assignment4 {
    // @param args the command line arguments
    public static void main(String[] args) {
        System.out.println("Welcome to the Area and Perimeter Calculator ");
        Scanner sc = new Scanner(System.in);
        double length, width, area, perimeter;
        //area = Double.parseDouble(area);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            // get the input from the user
        System.out.print("Enter length: ");
        length = sc.nextDouble();
        System.out.print("Enter width: ");
        width = sc.nextDouble();
        //calucate and display to user
        area= width * length;
        perimeter= 2 * width + 2 * length;

        System.out.println("Area: " + area);
        System.out.println("Perimeter: " + perimeter);
        //ask user if they want to contine
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        sc.nextLine(); // discard any other data entered on the line
        System.out.println();
        }      // end of while
    }

    public static double getDoubleWithinRange(Scanner sc, String prompt,

    double min, double max)

    {

        double d = 0.0;

        boolean isValid = false;

        while (isValid == false)

        {

            d = getDouble(sc, prompt);

            if (d <= min)

                System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (d >= max)

                System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

                isValid = true;

        }

        return d;

    }

    public static double getDouble(Scanner sc, String prompt)

    {

        double d = 0.0;

        boolean isValid = false;

        while (isValid == false)

        {

            System.out.print(prompt);

            if (sc.hasNextDouble())

            {

                d = sc.nextDouble();

                isValid = true;

            }

            else

            {

                System.out.println("Error! Invalid decimal value. Try again.");

            }

            sc.nextLine(); // discard any other data entered on the line

        }

        return d;

    }

    public static int getIntWithinRange(Scanner sc, String prompt,

    int min, int max)

    {

        int i = 0;

        boolean isValid = false;

        while (isValid == false)

        {

            i = getInt(sc, prompt);

            if (i <= min)

                System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (i >= max)

                System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

                isValid = true;

        }

        return i;

    }

    public static int getInt(Scanner sc, String prompt)

    {

        int i = 0;

        boolean isValid = false;

        while (isValid == false)

        {

            System.out.print(prompt);

            if (sc.hasNextInt())

            {

                i = sc.nextInt();

                isValid = true;

            }

            else

            {

                System.out.println("Error! Invalid integer value. Try again.");

            }

            sc.nextLine(); // discard any other data entered on the line

        }

        return i;

    }

}