Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Incomparable Data types char and String

Tags:

java

When I run the Javac, it tells me that i have an incomparable data types char and String in the

while(responseChar == "y")

not sure what to change to fix this error

import java.util.Scanner;
public class UseOrder
{
public static void main(String[] args)
{

    int answer, num, q;
    String name, response;
    double p;
    char responseChar;
    Scanner keyboard = new Scanner(System.in);

    Order order1 = new Order();
    order1.setCustomerName();
    order1.setCustomerNum();
    order1.setQuantity();
    order1.setUnitPrice();
    order1.computePrice();
    order1.displayInfo();

    keyboard.nextLine(); 
    System.out.println("Do you need the shipping and handling service?");
    System.out.println("Enter y or n :");
    response = keyboard.nextLine();
    responseChar = response.charAt(0);
    while(responseChar == "y")
    {
        ShippedOrder order2 = new ShippedOrder();
        order2.getCustomerName();
        order2.getCustomerNum();
        order2.getQuantity();
        order2.getUnitPrice();
        order2.computePrice();
        order2.displayInfo();
    }
}
}
like image 697
Nate Avatar asked Aug 18 '12 14:08

Nate


3 Answers

To define a character literal, use a single quote: '. Double quotes define string literals.

while(responseChar == 'y')
like image 78
Jeffrey Avatar answered Nov 14 '22 19:11

Jeffrey


Optionally you could use the big C Character class to convert your char to a String:

String converted = Character.toString(responseChar); 
while(converted.equalsIgnoreCase("y"))
{
  // ...
}

But this version is much more verbose than the literal comparison suggested by Jeffrey

like image 43
Hunter McMillen Avatar answered Nov 14 '22 19:11

Hunter McMillen


Instead of "y" do this 'y'. "" Represents string.

like image 1
Amit Deshpande Avatar answered Nov 14 '22 18:11

Amit Deshpande