-2

Please help, this will be an easy and obvious problem but im new to coding and im teaching myself online. My program should ask the user to enter an amount, it should then ask the user to enter an account type, and it should then output the value of the amount with a years interest.

import java.io.IOException;
import java.util.Scanner;


public class BankInvestment {

    private static final String Account = null;

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int S = 3;
        double D = 0.5;
        double C = 1.5;
        int L = 4;
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        @SuppressWarnings("resource")
        Scanner user_input = new Scanner( System.in );
        int n = input.nextInt();
        int Output = 0;
        String Accounttype = null;

        System.out.println("Please Enter Account Type(S,D,C,L): ");
        Accounttype = user_input.next();

        if (Accounttype == "S"){
            Output = n / 100 * S + n;
            System.out.println(Output);
        }
        else if (Account == "D"){
            Output = (int) (n / 100 * D + n);
            System.out.println(Output);
        }
        else if (Account == "C") {
            Output = (int) (n / 100 * C + n);
            System.out.println(Output);
        }
        else if (Account == "L"){
            Output = n / 100 * L + n;
            System.out.println(Output);
        }
    }
}
Cilan
  • 13,101
  • 3
  • 34
  • 51
squish
  • 846
  • 12
  • 24

2 Answers2

0

Don't compare String or any other object in Java with ==. That only compares references. Use the .equals() method to compare meaningfully.

Change this

if (Accounttype == "S"){
    Output = n / 100 * S + n;
    System.out.println(Output);
}

to this

if (Accounttype.equals("S")){
    Output = n / 100 * S + n;
    System.out.println(Output);
}
Kon
  • 10,702
  • 6
  • 41
  • 58
0

You need to use the right compare in java, String is an Object and this is the reason why should you use Accounttype.equals("L"). == Check if this is the same refrence which is not.. those 2 will be 2 diffrent objects

Aviad
  • 1,539
  • 1
  • 9
  • 24
  • @Avaid: Since questioner is directly initializing the String (String Accounttype = user_input.next()), therefore in this case, == will work fine as the String Pool is common. Whereas in case of String Accounttype = new String(user_input.next()), then == could not have been used as String Pool will be different and .equals would be a correct approach – Ankur Piyush Feb 08 '15 at 22:30