0

State University charges $90.00 for each semester hour of credit, $200.00 per semester for a regular room, $250.00 per semester for an air-conditional room, and $400.00 per semester for food. All students are charged a $30.00 matriculation fee. Graduating students must also pay a $35.00 diploma fee. Write a program to compute the fees that must be paid by a student. Your program should include an appropriate warning message if a student is taking more than 21 credit hours or fewer than 12 credit hours. A typical line of data for one student would include room type (R or A), student number (in four digits), credit hours, and graduation status (T or F).

this is my code:

package Assignment.Q86;

import java.util.Scanner;

public class Q086 {

    public static void main(String[] args) {
        int matriculation = 30, food = 400, hour, room = 0, graduation = 0, sum;
        String roomType, graduationStatus;
        Scanner input = new Scanner(System.in);

        System.out.println(
            "Your food cost $400.\nYour matriculation cost $30.\n"
        );

        System.out.println("Enter your semester hour:");
        hour = input.nextInt();

        System.out.println("Enter room type you want(A or R):");
        roomType = input.next();

        System.out.println("Enter your graduation status(T or F):");
        graduationStatus = input.next();

        //semester time
        if (hour < 12 && hour > 21) {
            System.out.println(
                "Alert!! Your semester hour is more or less than the usual.\n"
            );
        } else {
            hour = hour * 90;
        }

        //choosing room type
        if (roomType == "A" || roomType == "a") {
            room = 250;
        } else if (roomType == "R" && roomType == "r") {
            room = 200;
        } else {
            System.out.println("Please enter the correct room type");
        }

        //working for choosing the graduation type
        if (graduationStatus == "T" || graduationStatus == "t") {
            graduation = 35;
        } else if (graduationStatus == "F" || graduationStatus == "f") {
            graduation = 0;
        } else {
            System.out.println(
                "Please enter the correct graduation status value"
            );
        }

        sum = matriculation + food + room + hour + graduation;

        System.out.println("your total fees is " + sum);
        System.out.println(" your student number is ****");
        System.out.println("your room type is " + room);
        System.out.println("your graduation status is " + graduation);
        System.out.println("your total credit hours is " + hour);
    }
}

The problem is when I clicked any of the room type and graduation status = T. the result of both graduation and room shows the value of 0. The program says that the room and graduation has to be initialize in order to work.

this is the output example

Your food cost $400.
Your matriculation cost $30.

Enter your semester hour:
12
Enter room type you want(A or R):
r
Enter your graduation status(T or F):
t
Please enter the correct room type
Please enter the correct graduation status value
your total fees is 1510
your student number is ****
your room type is 0
your graduation status is 0
your total credit hours is 1080
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • `graduationStatus == "T"` is not how you compare Strings in Java – Scary Wombat Aug 06 '21 at 07:11
  • does it work with 'a'? if so, notice the difference between your `if (` blah blah `== a`)` statement and `else if(`blah blah `== r)` statement – mcalex Aug 06 '21 at 07:14
  • before this i had did like this: if (roomType == 'A' || roomType == 'a') but the program show error to run them so that why i change so after searching in the google. if these is invalid , so what is the solution for it? – Shangkari Sha Aug 06 '21 at 07:16
  • 1) Use for example `char roomType = input.nextLine().toLowerCase().chartAt(0)`; 2) Compare with single quotes – OneCricketeer Aug 06 '21 at 07:18
  • In the 'a' branch you use `||`, in 'r' you use `&&`. one is OR, one is AND. RoomType cannot equal 'R' AND 'r'. (I think your first try (that you mention in the comment) didn't work because you need double quotes for string, single quotes for char.) – mcalex Aug 06 '21 at 07:25
  • oh so sorry, i accidently type that one. Now, i used || in all of it. i output them again but still no changes in the code. – Shangkari Sha Aug 06 '21 at 07:30

1 Answers1

-1

== is not a good way to compare content. It merely compares references in case of Java. Try using the equals() method for content equality.

Sidenote: Try to break down your code into functions for better readability.

Replace your string comparisons with equals method where you're comparing content.

Ex: graduationStatus == "T" || graduationStatus == "t" changes to graduationStatus.equalsIgnoreCase("t")

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Paddy14
  • 14
  • 1
  • if(graduationStatus.equalsIgnoreCase("t")) { graduation = 35; } else if(graduationStatus.equalsIgnoreCase("T")) { graduation = 35; } else if(graduationStatus.equalsIgnoreCase("f")) { graduation = 0; } else if(graduationStatus.equalsIgnoreCase("F")) { graduation = 0; } – Shangkari Sha Aug 06 '21 at 07:28
  • shall i do like this above one? – Shangkari Sha Aug 06 '21 at 07:28
  • @ShangkariSha `graduationStatus.equalsIgnoreCase("t")` and `graduationStatus.equalsIgnoreCase("T")` give the same result (as the name implies, they ignore uppercase and lowercase), so you can just use one, no need for both. – Federico klez Culloca Aug 06 '21 at 07:29
  • i just edited the code and output them. it works finally the output. – Shangkari Sha Aug 06 '21 at 07:33
  • thank you very much for your help. you help save my day. – Shangkari Sha Aug 06 '21 at 07:33