Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default 'else' without 'if'

    I am in an introductory class for Java, and I am having a problem getting a small easy program to compile... can anyone point me in a direction to check.. because I am completely clueless.

    I have an 'if' for every 'else' and not sure if there is something I just don't know, or if there is like a small ( or { I am missing anywhere... can anyone help out a little? Thanks.

    Assignment
    Java Code:
    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.


    What I could create
    Java Code:
    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'){
          	switch(input) { 
                case 's':;
                case 'S':; }
          	String length = JOptionPane.showInputDialog("Enter length of a square");
          	String area = length * length;
          	String perimeter = length * 4;
          	String ouput = "The area is " + area + " and the perimeter is " + perimeter;
          	JOptionPane.showMessageDialog(null, output);}
          //Circle
          else if (input == 'C'){
            switch(input) {
                case 'c':;
                case 'C': }
          	String radius = JOptionPane.showInputDialog("Enter the radius of a circle");
          	String area = 3.14159 * radius * radius;
          	String 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'){
            switch(shape) {
                case 't':;
                case 'T': }
          	String base = JOptionPane.showInputDialog("Enter the base of a triangle");
          	String height = JOptionPane.showInputDialog("Enter the height of a triangle");
          	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);}
      }
    }
    Last edited by ZambonieDrivor; 02-23-2009 at 03:15 AM. Reason: Update code

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    someone else from your class posted the same thing with the same errors. if you wanna take the easy way out, follow the link at the bottom of the page to learn bad behavior.

    if you want to learn java properly, the answer to your question is to learn java syntax, understand the errors the compiler gives you, and consult friends and search engines for such issues before taking it to a forum, as it's a whole lot faster. here is what a simple google search for "java else without if" would have gotten you: 'else' without 'if'

    and here is the topic from your classmate who was fortunate to have mk12 do all of his work for him. maybe you two can be friends or something: [SOLVED] Please Help!!!

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    Looks like the program is close to the same thing.. but I am to use the JOptionPane and he used Scanner... maybe to an experienced person, they can convert it over.. but, I am new...

    I am not asking for people to build the file for me.. I am just trying to get help and understanding... from someone I can talk to, instead of reading pages upon pages of "Greek" writing.

    I solved the 'else' without 'if' problem... but brought up a series of different errors. :-\

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    well, every programmer has to learn to troubleshoot their own errors, and the first step is catching them before they happen, which you only get better at through experience and studying. the next step is by understanding the messages that the compiler gives you. it uses technical terms that you need to familiarize yourself with in order to get the most help out of search engines and out of forums as well, and most importantly are the biggest hint as to what's wrong.

    if you're really having too much trouble and don't want to read up on resources, you need help beyond what this forum can do for you. instead of having to wait around for someone to reply to a post (and back and forth), you should just work with a classmate or friend (do make some, even if they aren't the brightest, they're great for support), or even go to office hours and bug the hell out of your ta or professor if the class is too fast-paced for you.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
     if (input == 'S'){
    ... 
    else if (input == 'C'){
    If you have a '{' after an 'if', you have to close it with a '}' prior to an 'else'.

  6. #6
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    Java Code:
     if (input == 'S'){
    ... 
    else if (input == 'C'){
    If you have a '{' after an 'if', you have to close it with a '}' prior to an 'else'.
    Yeah, I got it figured out.... thanks for the help.

Posting Permissions

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