Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: If/Else Statment Help

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default If/Else Statment Help

    I have a simple If/Else program that wont seem to work correctly! Can anyone spot the error?


    package lab1;
    import javax.swing.JOptionPane;

    public class Exercise_5 {
    public static void main(String[] args) {
    String number1 = JOptionPane.showInputDialog("Enter One Number: ");
    String number2 = JOptionPane.showInputDialog("Enter Another Number: ");
    String operation = JOptionPane.showInputDialog("Enter an Operation, Type 1 for Addition, Type 2 for Subtraciton, Type 3 for Multiplication, Type 4 for Divison: ");

    int number1i = Integer.parseInt(number1);
    int number2i = Integer.parseInt(number2);
    int operationi = Integer.parseInt(operation);

    int add = number1i + number2i;
    int sub = number1i - number2i;
    int multi = number1i * number2i;
    int divi = number1i / number2i;

    if (number2i == 0){
    String num = "Congrat's! you broke the universe and divided by 0";
    }
    else {
    if (operationi == 1) {
    int number = sub;
    String num = Integer.toString(number);
    } else if (operationi == 2) {
    int number = add;
    String num = Integer.toString(number);
    } else if (operationi == 3) {
    int number = multi;
    String num = Integer.toString(number);
    } else if (operationi == 4) {
    int number = divi;
    String num = Integer.toString(number);
    }
    else {
    String num = "Sorry you did not enter a correct operating value";
    }
    }



    String output = "After calculating the two numbers, the answer is: "+ num;
    JOptionPane.showMessageDialog(null, output);


    }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: If/Else Statment Help

    For future reference, please surround your code in tags as found in my signature.
    the answer is: "+ num;, the compiler can't find the variable num, because it doesn't exist in the scope it is used.
    By declaring variables inside if statements, it's scope is local to that conditional block.

    You should declare and initialise the variable num outside of the if blocks, and alter them inside.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. final calculation after if statment
    By spongyballs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 23rd, 2011, 01:47 PM
  2. Else-if statment failure
    By tyb97 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 17th, 2011, 08:52 PM
  3. [SOLVED] My string is not activating my if statment.
    By ZeroLRS in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2011, 06:11 PM
  4. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 05:11 PM
  5. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM