Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Comparing Strings with "If" statements : help please. :-)

843785Feb 22 2009 — edited Feb 22 2009
I have an assignment for class... and having lots of trouble trying to figure out what I did wrong. I cannot find a solution.. if anyone can help me with this, it'd be much appreciated.

Assignment
Write a program to allow the user to calculate the area and perimeter of a square, or the area and circumference of a circle, or the area of a triangle.

To do this, the user will enter one of the following characters: S, C, or T. The program should then ask the user for the appropriate information in order to make the calculation, and should display the results of the calculation. See the example program execution shown in class.

The program should use dialog boxes.

When expecting an S, C, or T, the program should reject other characters with an appropriate message.

Get extra points for allowing both the uppercase and lowercase versions of a valid character to work. Name the program ShapesCalc.java.
My error codes are
incomparable types: java.lang.String and Char
cannot find symbol variable output
incomparable types: java.lang.String and Char
incomparable types: java.lang.String and Char
I've asked a friend and they said something about Strings cannot be compared in "If" statements... if that is the case.. how is this supposed to be arranged? If you can point me in the right direction, I will be very grateful! :-)

What I have created so far
import javax.swing.JOptionPane;

public class ShapesCalc {
    public static void main(String[] args) {

      //Enter S,C, or T
      String input = JOptionPane.showInputDialog("Enter S,C, or T");
           
      //If Statements
      //Square
      if (input == 'S'){
      	String lengthu = JOptionPane.showInputDialog("Enter length of a square");
      	double length = Double.parseDouble(lengthu); 
      	double area = length * length;
      	double perimeter = length * 4;
      	String ouput = "The area is " + area + " and the perimeter is " + perimeter;
      	JOptionPane.showMessageDialog(null, output);}
      //Circle
      else if (input == 'C'){
      	String radiusu = JOptionPane.showInputDialog("Enter the radius of a circle");
      	double radius = Double.parseDouble(radiusu);
      	double area = 3.14159 * radius * radius;
      	double circumference = 2 * 3.14159 * radius;
      	String output = "The area is " + area + " and the circumference is " + circumference;
      	JOptionPane.showMessageDialog(null, output);}
      //Triangle
      else if (input == 'T'){
      	String baseu = JOptionPane.showInputDialog("Enter the base of a triangle");
      	double base = Double.parseDouble(baseu);
      	String heightu = JOptionPane.showInputDialog("Enter the height of a triangle");
      	double height = Double.parseDouble(heightu);
      	String output = "The area is " + (base * height) / 2;
      	JOptionPane.showMessageDialog(null,output);}
      //Error Message
      else {
      	String error = "Incorrect variable please enter S,C, or T only.";
      	JOptionPane.showMessageDialog(null,error);}
      
      //Signature
      String signature = "Rodriguez, Markos has compiled a Java program.";
      JOptionPane.showMessageDialog(null,signature);
  }
}
Edited by: ZambonieDrivor on Feb 22, 2009 6:52 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2009
Added on Feb 22 2009
7 comments
922 views