Hey guys I am writing my first program ever in java starting tonight and all has actually gone smooth which has been a surprise but I have run into a problem with an if else area. Basically, what I am suppose to be doing in this section is the user is prompted to either give r, d, or n and depending on their response they receive a different price. No matter what I input I get the price for n (which is 0.00). So I was wondering if anyone knows what might be wrong?

r = 1.00
d = 1.50
n = 0.00

Thank You in Advance!

System.out.println("Type of padding (r = regular, d = delux, n = none)?: ");
    padding = sc.next();

    System.out.println(padding);

    if (sqYard > 3000)
       {
       carpetCost = price * sqYard * surChargeBig;
       System.out.println(carpetCost);
       }
    else
       System.out.println("small");

    if (padding == "r")
      System.out.println(costRegPad);
    else if (padding == "d")
       System.out.println(costDlxPad);
    else
       System.out.println(0.00);

Recommended Answers

All 15 Replies

Is this your whole code? If yes, then umm... where have you defined the variables r, d, n? The computer wouldn't know what r,d,n are...
Also the following is wrong

if (padding == "r")

same goes for the ' == "d" '

only Strings are put in double quotes. if you have declared r as a variable with a value, then remove the quotes. I'm surprised your compiler didnt give you errors! anyhow, give me the whole code and I shall fix stuff....

import java.io.*;
import java.util.Scanner;

public class RugCostEst1
  {

  //symbolic constants

  public static final double taxRate = 0.06;
  public static final int jobSize = 3000;
  public static final double surChargeBig = 1.05;
  public static final double surChargeReg = 1.07;
  public static final double costRegPad = 1.00;
  public static final double costDlxPad = 1.50;
  public static final double laborRate = 10.00;
  public static final double costMoving = 20.00;
 

  public static void main(String argv[])
    {
    String str,
           padding;

    double sqYard = 0.00,
           price = 0.00,
           numRooms = 0.00,
           moveFurniture = 0.00;

    double carpetCost = 0.00,
           laborCost = 0.00,
           preTaxTotal = 0.00,
           tax = 0.00;

    Scanner sc = new Scanner(System.in);

    System.out.println("How much carpet needed (in square feet)?: ");
    str = sc.next();

    sqYard = new Double(str).doubleValue();

    System.out.println("What is the price per square yard?: ");
    str = sc.next();

    price = new Double(str).doubleValue();

    System.out.println("Number of rooms?: ");
    str = sc.next();

    numRooms = new Double(str).doubleValue();

    System.out.println("How many rooms need furniture moved?: ");
    str = sc.next();

    moveFurniture = new Double(str).doubleValue();

    System.out.println("Type of padding (r = regular, d = delux, n = none)?: ");
    padding = sc.next();
 
    System.out.println(padding);

    if (sqYard > 3000)
       {
       carpetCost = price * sqYard * surChargeBig;  
       System.out.println(carpetCost);
       }   
    else
       System.out.println("small");

    if (padding == "r")
      System.out.println(costRegPad);
    else if (padding == "d")
       System.out.println(costDlxPad);
    else
       System.out.println(0.00);


/*


    laborCost = (numRooms * laborRate) + (costMoving * moveFurniture);  
    System.out.println(laborCost);

    //preTaxTotal = carpetCost + paddingCost + laborCost;
    //System.out.println(preTaxTotal);

    //tax = preTaxTotal * taxRate;
    //System.out.println(tax);

    //total = preTaxTotal + tax;
    //System.out.println(total);
*/
    }
  }

Not quite sure what the purpose of this program is but I can see a few mistakes which I pointed out in the previous comment.

if (padding == "r")
System.out.println(costRegPad);
else if (padding == "d")
System.out.println(costDlxPad);

THIS IS WRONG!!! On so many levels.
First, Only, ONLY, strings (something like "hello", "bye", "name") are put in double quotes. so remove those quotes.
Second, you want to compare 'char's ... so instead of putting double quotes, put single quotes. so that part changes to:

if (padding == 'r')
System.out.println(costRegPad);
else if (padding == 'd')
System.out.println(costDlxPad);

Those are the only mistakes I can see so far. See if it runs good now. If it doesn't then tell me the question you are working on.

I get this error for both now.

RugCostEst1.java:69: incomparable types: java.lang.String and char
if (padding == 'r')

Any idea?

Thanks

Since you are using 'Scanner' to get the input..

try defining what kind of input do you want. for example,

for integer:
sc.nextInt

for char:
sc.nextChar

maybe that would work. If not, I'm all out. I need to brush up my java O_O


EDIT: The error says that you are trying to compare a string value like a char value... try using something like

if (padding.equals("r"))

Ya I tried boht of those still having a few issues.

Using your old code, replace:

if (padding == "r")
System.out.println(costRegPad);
else if (padding == "d")
System.out.println(costDlxPad);

with

if (padding.equals("r"))
      System.out.println(costRegPad);
    else if (padding.equals("d"))
       System.out.println(costDlxPad);

To compare Strings you use .equals

I did something just like that right before you posted it and now I recieve this error:

Exception in thread "main" java.lang.NoClassDefFoundError: RugCostEst1/java
Caused by: java.lang.ClassNotFoundException: RugCostEst1.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: RugCostEst1.java. Program will exit.


Here is my updated code.

Thanks

import java.io.*;
import java.util.Scanner;

public class RugCostEst1
  {

  //symbolic constants

  public static final double taxRate = 0.06;
  public static final int jobSize = 3000;
  public static final double surChargeBig = 1.05;
  public static final double surChargeReg = 1.07;
  public static final double costRegPad = 1.00;
  public static final double costDlxPad = 1.50;
  public static final double laborRate = 10.00;
  public static final double costMoving = 20.00;
 
 
  public static void main(String argv[])
    {
    String str,
           regular = "r",
           deluxe = "d",
           padding;

    double sqYard = 0.00,
           price = 0.00,
           numRooms = 0.00,
           moveFurniture = 0.00;

    double carpetCost = 0.00,
           laborCost = 0.00,
           preTaxTotal = 0.00,
           tax = 0.00;

    Scanner sc = new Scanner(System.in);

    System.out.println("How much carpet needed (in square feet)?: ");
    str = sc.next();

    sqYard = new Double(str).doubleValue();

    System.out.println("What is the price per square yard?: ");
    str = sc.next();

    price = new Double(str).doubleValue();

    System.out.println("Number of rooms?: ");
    str = sc.next();

    numRooms = new Double(str).doubleValue();

    System.out.println("How many rooms need furniture moved?: ");
    str = sc.next();

    moveFurniture = new Double(str).doubleValue();

    System.out.println("Type of padding (r = regular, d = delux, n = none)?: ");
    padding = sc.next();
 
    System.out.println(padding);

    if (sqYard > 3000)
       {
       carpetCost = price * sqYard * surChargeBig;  
       System.out.println(carpetCost);
       }   
    else
       System.out.println("small");

    if (padding.equals(regular))
      System.out.println(costRegPad);
    else if (padding.equals(deluxe))
       System.out.println(costDlxPad);
    else
       System.out.println(0.00);





    laborCost = (numRooms * laborRate) + (costMoving * moveFurniture);  
    System.out.println(laborCost);

    //preTaxTotal = carpetCost + paddingCost + laborCost;
    //System.out.println(preTaxTotal);

    //tax = preTaxTotal * taxRate;
    //System.out.println(tax);

    //total = preTaxTotal + tax;
    //System.out.println(total);

    }
  }

It compiles fine for me? Have you missed/removed a { from anywhere? Are you sure you copied the exact same code you have now?

it compiles fine for me but that error comes when i execute it

Ok, can you tell me how you produce that error? The steps. The program works fine for me.

That code looks like it should compile okay - did you maybe have a command-line typo when you went to compile or run?

I literally just run the compile command and then when I execute it comes up.

z136340@turing:~/CS470$ javac RugCostEst1.java

z136340@turing:~/CS470$ java RugCostEst1.java

Exception in thread "main" java.lang.NoClassDefFoundError: RugCostEst1/java
Caused by: java.lang.ClassNotFoundException: RugCostEst1.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: RugCostEst1.java. Program will exit.

z136340@turing:~/CS470$ java RugCostEst1.java Remove ".java" from this and it'll work.

Ya that did it I literally just realized that before you guys posted it. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.