• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking for help with if statement  RSS feed

 
Jd Reynolds
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! I am new to the site and joined because I just started my first programming class in java and could really use some help on a project I have. I am NOT asking anyone to do the project for me, that is unethical but I am stuck on a simple part. I pasted the prompt below.

** My Problem **

so i am setting up the main method and I am asked for users to input there flooring selection at LAM, OAK, or CHE. Once they make the selection I need to write an if statement and I am not sure how to do that because when the user enters their choice it doesnt save that as the variable so my if statement will not work. ANY GUIDANCE IS GREATLY APPRECIATED!!!




A hardwood flooring company calculates the cost of installation for 1000 square feet according to the
following table:
Basic laminate $5000
Solid oak $7000 + $100 per room
Solid antique cherry $9000 + $75 per room
$750 for additional coating for up to 4 layers
$400 for any extra layers for additional coating beyond 4 layers
Baseboard 10% of the cost
write a Java program that contains one class named HarwoodCostCalculator. This class will
contain a Java program that computes the cost of hardwood floor installation according to the values
given above.
You need to get the following input data from user:
 The type of hardwood (laminate, oak or cherry)
 If the type of hardwood is not laminate, the number of rooms
 If the type of hardwood is cherry, the number of coatings purchased
 Whether the customer wants baseboard or not (all types)
You may assume that all input numerical data is of the proper form and will not contain negative values.
Depending on the input data value(s), the program will calculate and display the amount of the cost for
the user.
The hardwood type will be entered at the keyboard as three letters ("LAM" for laminate, "OAK" for oak,
"CHE" for cherry). If the user enters a type that doesn't match the three accepted entries, do not
compute a cost; just output an error message. All numerical values (number of rooms and coating
layers) are entered as integers. The baseboard question will be answered with the string "YES" or "NO".
If the user enters an answer that does not match these strings, then output a warning and assume the
answer is "NO". Your program should ask only those questions that are necessary to determine the total
cost.
Program Requirements: When you ask the user for the hardwood selection, you should present the user
with a menu with the three options:
--Available Harwood Types--
LAM [Laminate]
OAK [Solid oak]
CHE [Solid antique cherry]
Enter your type choice:
Use the nextLine() method of the Scanner class to read the type choice. The input string MUST match
one of the three choices above; otherwise, the choice is unknown. (So, "LAM" is ok but "lam" is not,
"LAMINATE" is not, and "Lam" is not.)
Once you read in the required data value(s) from the user, call a static method that will compute and
return the required cost. You will need to write three static methods, one for each hardwood type. Use
the following signatures for your static methods:
public static int computeLaminate(boolean hasBaseboard)
public static int computeSolidOak(int rooms, boolean
hasBaseboard)
public static int computeSolidAntiqueCherry(int rooms, int
numCoatingLayers, boolean hasBaseboard)
Once your main method calls the appropriate method and receives the returned value, display this in
dollars and cents. Sample Output:
--Available Harwood Type--
LAM [Laminate]
OAK [Solid oak]
CHE [Solid antique cherry]
Enter your type choice:LAM
Do you have baseboard? (YES/NO) YES
Your cost is $5500.00
--Available Harwood Type--
LAM [Laminate]
OAK [Solid oak]
CHE [Solid antique cherry]
Enter your menu choice: OAK
Do you have baseboard? (YES/NO) NO
Enter the number of rooms: 2
Your cost is $7200.00
 
Jd Reynolds
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I now know to use .equals() instead of == but that wasnt my problem.

so basically if a use inputs LAM how can I write an if statement that will read their input and know to distinguish their input from others and run a specific if statement?

I am having trouble getting my if statement to read the input
 
Jd Reynolds
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my new code now

import java.util.*;

public class HardwoodCostCalaulator {

public static int computeLaminate (boolean hasBaseboard){

}

public static int computeSolidOak (int rooms, boolean hasBasebaord){

}

public static int computeSolidAntiqueCherry (int rooms, int numCoatingLayers, boolean hasBaseboard){

}

public static void main(String[] args) {
// TODO Auto-generated method stub



String LAM = "LAM";
String OAK = "OAK";
String CHE = "CHE";
Scanner scan = new Scanner(System.in);


System.out.println("--Avaiable Hardwood Types--");
System.out.println("LAM [Laminate]");
System.out.println("OAK [Solid oak]");
System.out.println("CHE [Solid antique cherry]");
System.out.print("Enter your type choice: ");

String choice = scan.next();

if (choice.equals("LAM")){
System.out.println("Do you have baseboard? (YES/NO)");

String baseboard = scan.nextLine();

} else if (scan.equals("OAK")){
System.out.println("Do you have baseboard? (YES/NO)");
String baseboard = scan.nextLine();

System.out.println("Enter the number of rooms: ");
} else if (scan.equals("CHE")){
System.out.println("Do you have baseboard? (YES/NO)");
String baseboard = scan.nextLine();







}



}

}
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LAM without quotes is not the same as "LAM" with quotes.  The former is interpreted as a symbolic name, the latter as a String literal.  Since you have not declared any variables or constants named LAM or OAK or CHE, you'll get a compiler error on line 34 and 35.

You can learn more about Strings in Java here: https://docs.oracle.com/javase/tutorial/java/data/strings.html
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jd Reynolds wrote:
} else if (scan.equals("OAK")){


The scan variable is of type Scanner -- ie. it is referring to an instance of Scanner class.  I think that it is safe to say that it is *never* equal to a string.

Henry
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an if statement that will read their input

You rarely want to read a value in an if statement.  It is better to read the user's input into a variable whose value can then be tested and used as needed.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when you are working with String literals like that, it's safer to do this:

This is safer because it will work even if choice is null. If you did it the other way, then you'd get a NullPointerException when choice is null.

Since you have defined symbolic names for those String values, you might as well use them:

This latter code is valid because you have defined LAM, OAK, and CHE as variables. In the first version of the code that you posted, you didn't.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. .   Since you have not declared any variables or constants named LAM or OAK or CHE, you'll get a compiler error on line 34 and 35.  . . .
You will also get a ompiler error for the comma, because you don't use a comma to join two predicates like that.

And welcome again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!