Esben86
Esben86

Reputation: 493

If-statement and logical operators

I am working on an exercise from an introduction course book. The exercise is to write a program that prompts the user to enter two characters and display the major and status represented in the characters. The first character indicates the major and the second is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior or senior. Suppose the following characters are used to denote majors:

I: Information Management

C: Computer Science

A: Accounting

The letter character is typed first, and the number character second. So far the program works as long as both characters are typed right. If the characters are typed wrong, i.e T3, the program should print "Invalid input". The program prints "Invalid input" if the letter character is typed right, and number character wrong. But if the letter character is typed wrong, and the number character right, the program prints a blank space instead of the major, plus title. In the last if-statement I have tried to make a condition which says that if any other character than either 'I', 'C', or 'A' is typed in along with a number character that is not in the interval between 1 and 4, the program should print "Invalid input". Can anyone see what is wrong? Code example below:

import java.util.Scanner;

public class MajorAndStatus {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String major = "";
        String title = "";

        System.out.println("Enter 2 characters: ");

        String inputString = input.nextLine();

        if (inputString.charAt(0) == 'I') {
            major = "Information Management";
        }
            else if(inputString.charAt(0) == 'C') {
                major = "Computer Science";
            }
            else if(inputString.charAt(0) == 'A') {
                major  = "Accounting";
            }


        if (inputString.charAt(1) == '1') {
            title = "Freshman";
        }
            else if(inputString.charAt(1) == '2') {
                title = "Sophomore";
            }
            else if(inputString.charAt(1) == '3') {
                title  = "Junior";
            }
            else if(inputString.charAt(1) == '4') {
                title  = "Senior";
            }

        if (((inputString.charAt(0) != 'I' || inputString.charAt(0) != 'C' || inputString.charAt(0) != 'A')) & ((inputString.charAt(1) < '1' || inputString.charAt(1) > '4'))) {
            System.out.println("Invalid input");
        }
        else {
            System.out.println(major + " " + title);
        }

    }
}

Upvotes: 0

Views: 737

Answers (3)

Eran
Eran

Reputation: 394136

Your condition for printing Invalid Input is wrong.

It should be

if (((inputString.charAt(0) != 'I' && inputString.charAt(0) != 'C' && inputString.charAt(0) != 'A')) || ((inputString.charAt(1) < '1' || inputString.charAt(1) > '4')))

However, I'd eliminate that if statement.

Instead, add an else cluase to your to if-else-if statements :

    if (inputString.charAt(0) == 'I') {
        major = "Information Management";
    } else if(inputString.charAt(0) == 'C') {
        major = "Computer Science";
    } else if(inputString.charAt(0) == 'A') {
        major  = "Accounting";
    } else {
        System.out.println("Invalid input");
    }

    if (inputString.charAt(1) == '1') {
        title = "Freshman";
    } else if(inputString.charAt(1) == '2') {
        title = "Sophomore";
    } else if(inputString.charAt(1) == '3') {
        title  = "Junior";
    } else if(inputString.charAt(1) == '4') {
        title  = "Senior";
    } else {
        System.out.println("Invalid input");
    }

    if (!major.equals("") && !title.equals("")) {
        System.out.println(major + " " + title);
    }

EDIT :

I you wish "Invalid Input" to be printed only once, you can check the value of the major and title variables to determine if the correct input was entered:

    if (inputString.charAt(0) == 'I') {
        major = "Information Management";
    } else if(inputString.charAt(0) == 'C') {
        major = "Computer Science";
    } else if(inputString.charAt(0) == 'A') {
        major  = "Accounting";
    }

    if (inputString.charAt(1) == '1') {
        title = "Freshman";
    } else if(inputString.charAt(1) == '2') {
        title = "Sophomore";
    } else if(inputString.charAt(1) == '3') {
        title  = "Junior";
    } else if(inputString.charAt(1) == '4') {
        title  = "Senior";
    }

    if (!major.equals("") && !title.equals("")) {
        System.out.println(major + " " + title);
    } else {
        System.out.println("Invalid input");
    }

Upvotes: 2

Jacek Cz
Jacek Cz

Reputation: 1906

Both aswers don't detect problems with too short string (IndexOutOfBoundsException)

replace

   String inputString = input.nextLine();

    if (inputString.charAt(0) == 'I') {
    ... }

with

    String inputString = input.nextLine();
    if(inputString.length<2) {
         System.out.println("Type a least 2 charactyes");
    }
    if (inputString.charAt(0) == 'I') {
    ... }

Upvotes: 0

helencrump
helencrump

Reputation: 1389

Instead of checking for errors at the bottom of your program, why don't you add in an else to your if statements, which will print invalid input.

Something like this, perhaps:

if (inputString.charAt(0) == 'I') {
    major = "Information Management";
}
else if(inputString.charAt(0) == 'C') {
    major = "Computer Science";
}
else if(inputString.charAt(0) == 'A') {
    major  = "Accounting";
}
else {
    System.out.println("Invalid input");
}

You can add in the same else to your if statement for the second character too.

Upvotes: 1

Related Questions