Results 1 to 3 of 3
  1. #1
    lornapowell13 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Post if and else if being ignored can't figure out why....

    hi i'm new to this and java programming can someone please help me. I cant figure out why my if and else ifs are being ignored only the last else is being detected.

    import javax.swing.JOptionPane;
    public class ShapesFindErrors
    {
    public static void main(String [] args)
    {
    String menuMessage;
    byte option;
    double pi = 3.14,side,base,height,area,radius;
    menuMessage = "Choose an option from the following menu:\n1. Calculate area of circle\n2. Calculate area of square \n3. Calculate area of triangle";
    option = Byte.parseByte(JOptionPane.showInputDialog(null,me nuMessage));
    if(option == '1')
    {
    radius = Double.parseDouble(JOptionPane.showInputDialog(nul l,"Enter radius"));
    area = pi*radius*radius;
    JOptionPane.showMessageDialog(null,"The area is " + area);
    }
    else if(option == '2')
    {
    side= Double.parseDouble(JOptionPane.showInputDialog(nul l,"Enter length of a side"));
    area = side*side;
    JOptionPane.showMessageDialog(null,"The area is " + area);
    }
    else if(option == '3')
    {
    base = Double.parseDouble(JOptionPane.showInputDialog(nul l,"Enter base length"));
    height = Double.parseDouble(JOptionPane.showInputDialog(nul l,"Enter height length"));
    area = (2/base)*height;
    JOptionPane.showMessageDialog(null,"The area is " + area);
    }
    else
    {
    JOptionPane.showMessageDialog(null,"Hit any key to try again");
    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: if and else if being ignored can't figure out why....

    You're comparing a byte value to a character value. The character value of 1 is 0x31. So remove the single quotes in your comparison. And why are you using byte and not int?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    lornapowell13 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: if and else if being ignored can't figure out why....

    Thanks a million Jim
    Was fixing errors in a program given by a lecturer and the single quotes and byte were used in it.

    Lorna

Similar Threads

  1. Please me figure this out
    By atrodeyo in forum New To Java
    Replies: 7
    Last Post: 04-30-2012, 02:14 PM
  2. can't figure it out myself
    By Doyle Raymond in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-18-2011, 03:34 PM
  3. can someone help me to figure this out >.<
    By kissmeeq in forum New To Java
    Replies: 2
    Last Post: 08-01-2011, 03:19 PM
  4. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •