0

I have to make this program which asks for a furniture type, the material type and calculates the cost of the furniture while displaying price and the type of material.

// Adam Bashir public class Exercise1 {

public static void main(String[] args) throws Exception {

    // TODO Auto-generated method stub
    char furntype; 
    char fabtype; 
    int c =1000;
    int C =1000;
    int l =750;
    int L =750;
    int r =1200;
    int R =1200;
    String couch="";
    System.out.println("Please enter the type of furniture you want! C for couch L for loveseats, and R for Recliner");
    furntype = (char)System.in.read();

    if (furntype == 'c' || furntype == 'C') {
        System.out.println("You chose a couch! It sells for $ " + c);
    } else if (furntype == 'l' || furntype == 'L') {
        System.out.println("You chose a loveseat! It sells for $750");
    } else if (furntype == 'r' || furntype == 'R') {
        System.out.println("you chose recliner! It sells for $1200");
    } else {
        System.out.println("Invalid choice");
    }

    System.out.println("Would you like fabric or leather for the couch, leather costs 35% more.");
    fabtype = (char)System.in.read();

    if (fabtype == 'f' || fabtype =='F') {
        System.out.println("you selected fabric! your total is $" + furntype);
    } else if (fabtype == 'l' || fabtype =='L') {
        System.out.println("you chose leather! your new total is $" + fabtype);
    } else {
        System.out.println("Invalid choice");
    }
}

I cannot figure out why my second set of if statements doesn't let me input the fabric choice

alexrnov
  • 1,384
  • 2
  • 7
  • 22
Adam
  • 3
  • 2
  • the code is not done, i just need to figure out why i cannot input my fabric choice! – Adam Feb 21 '19 at 00:56
  • If you input L followed by the Enter key, then you have entered two characters. The first, `L` is used for the first choice. The second, `Enter` (aka `\n`, 0x0A) , will be used for the second choice, and it's invalid. Read a full line instead each time. – that other guy Feb 21 '19 at 00:58
  • how would I go about reading a full line instead each time? – Adam Feb 21 '19 at 01:05
  • [This question](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) asks the same thing. You can use Scanner and its `readLine()` function. Note that this gives you a String and not a char, so you'd have to compare with `furntype.equals("c")` – that other guy Feb 21 '19 at 01:16

1 Answers1

0

Why don't you use a scanner?

public static void main(String[] args) throws Exception  {

    // TODO Auto-generated method stub
    char furntype;
    char fabtype;
    int c =1000;
    int C =1000;
    int l =750;
    int L =750;
    int r =1200;
    int R =1200;
    String couch="";
    Scanner scanner = new Scanner(System.in);

    System.out.println("Please enter the type of furniture you want! "+
         "C for couch L for loveseats, and R for Recliner");

    //Take user input as a String, and get the first char.
    furntype = scanner.nextLine().charAt(0);


    if (furntype == 'c' || furntype == 'C')
        System.out.println("You chose a couch! It sells for $ " + c);

    else if (furntype == 'l' || furntype == 'L')
        System.out.println("You chose a loveseat! It sells for $750");

    else if (furntype == 'r' || furntype == 'R')
        System.out.println("you chose recliner! It sells for $1200");

    else
        System.out.println("Invalid choice");


    System.out.println("Would you like fabric or "+
        "leather for the couch, leather costs 35% more.");

    //Get the first char of input string
    fabtype = scanner.nextLine().charAt(0);


    if (fabtype == 'f' || fabtype =='F')
        System.out.println("you selected fabric! your total is $" + furntype);

    else if (fabtype == 'l' || fabtype =='L')
        System.out.println("you chose leather! your new total is $" + fabtype);

    else{
        System.out.println("Invalid choice");
    }
}

Ideally, I would recommend to use String instead of char. Treat your input as a String and use new Scanner().readLine();. Then to check for equality, use if(input.equalsIgnoreCase("c")). It will make your program easier to write.

Soutzikevich
  • 904
  • 3
  • 11
  • 28