Results 1 to 2 of 2

Thread: Case help!!!

  1. #1
    cloverrea is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Angry Case help!!!

    Hello, so I have been working on this question for a very very long time and this is what I have done so far..

    public static void main(String[] args)
    {
    // declare variables
    Scanner input = new Scanner(System.in);
    double salePrice;
    double commission;
    int propertyCode;

    propertyCode = getCode();
    commission = calcCommission();

    //methods
    System.out.print("Enter the sale price: ");
    salePrice = input.nextDouble();
    }
    public static int getCode()
    {
    Scanner input = new Scanner (System.in);
    int code = propertyCode.charAt();
    System.out.print("Enter the property code according to the following: \nResidential enter r \nMulti-Dwelling enter m \nCommercial enter c \n","\nChoose your code: ");
    do
    {
    }
    while (code != "r" || code != "m" || code != "c");
    {
    if (code = "r" || code =" m"|| code = "c") break;
    else
    System.out.print("Code must either be r, m or c! ");
    }
    }

    public static double calcCommission(double price,int propertycode)
    {
    Scanner input = new Scanner(System.in);

    double commission = 0;

    Switch(propertyCode);
    {
    case ("r"):
    commission = .07 * price;
    break;
    case ("m"):
    commission = .06 * price;
    break;
    case ("c"):
    commission = .035 * price;
    }
    return commission;

    System.out.print(" The Commission is: " + commission);
    }




    I am hoping that I'm on the right track.... I need to enter a sale price, then a property code (r, m or c) and loop it if the wrong code is entered and then I need to calculate commission. We are supposed to use cases which I am having an issue with the compiler saying orphaned case... can someone help me? please????

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Case help!!!

    I would recommend reading this if you haven't already: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Scroll down to the section: "Using strings in switch statements".

    Ok, i've copied this into Eclipse and these are the problems that i have found (as specified in the comments):

    Java Code:
    public static void main(String[] args)
    {
    // declare variables
    Scanner input = new Scanner(System.in);
    double salePrice;
    double commission;
    int propertyCode;
    
    propertyCode = getCode();
    commission = calcCommission();  //You have not passed anything through to calcCommision()
    
    //methods
    System.out.print("Enter the sale price: ");
    salePrice = input.nextDouble(); //Shouldn't this go further up, before calcCommision because it expects a double value to be passed to it
    }
    public static int getCode()
    {
    Scanner input = new Scanner (System.in);
    int code = propertyCode.charAt();  //Code is an integer however further down it is being compared with letters (incompatible operand types).  Also propertyCode is not a local variable.
    System.out.print("Enter the property code according to the following: \nResidential enter r \nMulti-Dwelling enter m \nCommercial enter c \n","\nChoose your code: ");
    do
    {  //This do while loop isn't really doing anything.  You could add in: String code = input.nextLine();  (it will only continue if the values that you have specified match what is inside the variable "code")
    }
    while (code != "r" || code != "m" || code != "c");
    {
    if (code = "r" || code =" m"|| code = "c") break;
    else
    System.out.print("Code must either be r, m or c! ");
    }
    }
    
    public static double calcCommission(double price,int propertycode)  //You've passed propertycode and price as parameters but have not mapped them to variables inside this method.  To do so you can do: int propertyCode = propertycode;
    {
    Scanner input = new Scanner(System.in);
    
    double commission = 0;
    
    Switch(propertyCode); //However property code is an integer but within the case statement there are letters (error in eclipse: incompatible operand types string and int)
    {
    case ("r"):
    commission = .07 * price;
    break;
    case ("m"):
    commission = .06 * price;
    break;
    case ("c"):
    commission = .035 * price;
    }
    return commission;
    
    System.out.print(" The Commission is: " + commission);
    }
    Hope this helps,

    Regards Serb.
    Last edited by SerbianSergeant; 10-13-2011 at 12:40 AM.

Similar Threads

  1. How to use Contains - without case sensitivity?
    By Russell411 in forum New To Java
    Replies: 12
    Last Post: 11-07-2012, 02:53 PM
  2. Problem with case - might need recursion
    By Angelar in forum New To Java
    Replies: 6
    Last Post: 10-13-2010, 02:25 PM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. In Case You Want Colors!
    By angryboy in forum Forum Lobby
    Replies: 0
    Last Post: 08-29-2009, 10:06 PM
  5. case statement
    By skiing in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 12:13 PM

Tags for this Thread

Posting Permissions

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