0

So i am very new to java. I've been messing around with some if statements using the scanner to test user input. When i run the program, three out of the four if statements work. The fourth runs as the third... I don't know why this is. Here is my code. Hopefully you guys have some suggestions.

    import java.io.*;
    import java.util.Scanner;

    public class TestFile5
    {
        public static void main(String[] args) throws IOException
    {
    //Have two choices? Simple and complicated?  Simple example = input (X) input.  Complicated = below
    Scanner user_input = new Scanner(System.in);

    System.out.println("Choose calculation type: Long or Short");

    String choicetype;
    System.out.print("Type: ");
    choicetype = user_input.next();

    String selection;
    if(choicetype.equals("Long"))
    {
        System.out.println("Selection: Long");
        System.out.println(" ");

        System.out.println("This is a calculator");
        System.out.println("First select type of function");
        System.out.println("1: Addition");
        System.out.println("2: Subtraction");
        System.out.println("3: Multiplication");
        System.out.println("4: Division");

        System.out.print("Selection: ");
        selection = user_input.next();



        if(selection.equals("Addition"))
        {
            System.out.println("Enter two numbers to add");
            int first_number;
            System.out.print("1: ");
            first_number = user_input.nextInt();
            int second_number;
            System.out.print("2: ");
            second_number = user_input.nextInt();

            System.out.println(first_number + second_number);

            System.exit(0);
        }
        if(selection.equals("Subtraction"))
        {
            System.out.println("Enter two numbers to subtract");
            int first_number;
            System.out.print("1: ");
            first_number = user_input.nextInt();
            int second_number;
            System.out.print("2: ");
            second_number = user_input.nextInt();

            System.out.println(first_number - second_number);

            System.exit(0);
        }
        if(selection.equals("Multiplication"));
        {
            System.out.println("Enter two numbers to multiply");
            int first_number;
            System.out.print("1: ");
            first_number = user_input.nextInt();
            int second_number;
            System.out.print("2: ");
            second_number = user_input.nextInt();

            System.out.println(first_number * second_number);

            System.exit(0);
        }
        //Does Not work.  Trys to multiply
        if(selection.equals("Division"))
        {
            System.out.println("Enter two numbers to divide");
            int first_number;
            System.out.print("1: ");
            first_number = user_input.nextInt();
            int second_number;
            System.out.print("2: ");
            second_number = user_input.nextInt();

            System.out.print("Answer: ");
            System.out.print(first_number / second_number);

            System.exit(0);
        }
    }
    if(choicetype.equals("Short"))
    {
        System.out.println("Selection: Short");
        System.out.println("");
    }
  }
}
Schmidty15
  • 181
  • 1
  • 2
  • 7

1 Answers1

2

To answer the question - there is no max amount of if statements you can implement.

I suspect your code isn't working because there is this error:

if(selection.equals("Multiplication"));

There shouldn't be a ";" there - it terminates the if statement and causes the multiply block to execute in all cases where the ifs above are not true (thanks to System.exit(0) - if the program didn't have exit in each of those "ifs", that multiply block of code would execute EVERY time any option is selected due to this error).


SIDENOTE:

Furthermore, it should be worth noting that else ifs, or even a switch statement, may have been more appropriate for this piece of code.

When you have a bunch of "ifs", if more than one case is true, they both execute. If it was an "if" followed by "else ifs" - only one in the chain would execute.

Here is a quick tutorial on switch statements, another viable option:

http://www.homeandlearn.co.uk/java/java_switch_statements.html

Also, if a line of code is shared across ALL blocks, it is best to put the line at the end of all scenarios. For example, System.exit(0) happens at the end of each of the four ifs - to cut redundancy it would be best to move that line down to the bottom of those options - right after the "}" for the "Division" case. Just a good practice thing, not required.

PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91