0
import java.util.Scanner;

public class Methods {

/*
 * Compound interest Program Quarterly
 */


    public static void main(String[] args) {
        Scanner keyboard = new Scanner  (System.in);

        System.out.println("What is the Rate");
        int rate = keyboard.nextInt()/100;
        System.out.println("What is the Amount");
        int amount = keyboard.nextInt();
        System.out.println("How many years?");
        int years = keyboard.nextInt();

        keyboard.nextLine();
        System.out.println("How is it compounded? Press Q = Quarterly, A = Anually, S = SemiAnnualy, M = Monthly");

        String answer = keyboard.nextLine();

       int easy = amount*(1+(rate/years));
       int pow = 4 * years;


        if (answer == "/Q"){
            System.out.println("Your answer compounded Quarterly is: "  + Math.pow(easy,pow));

This is the code, but the if statement with String == "Q" doesn't work because when I press Q nothing happens? what's the issue?

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
Winter One
  • 27
  • 5

3 Answers3

0

Java right? use equalsIgnoreCase because you could be typing 'q' and that / they told you about too, keep it in mind

gia
  • 757
  • 5
  • 19
  • I'm typing in the uppercase Q to test it, I can't even test it because it ignores the upppercase Q it's not the lower case – Winter One Mar 30 '15 at 02:05
  • try to debug using answer.lenght() and System.out.println("OOO"+answer+"OOO"), maybe java is adding the line feed to your answer and that's why it fails the check. regardless, still use equalsIgnoreCase so peopel can type both Q and q – gia Mar 30 '15 at 02:10
0

First issue is that you have a typo (the forward slash) in this line:

if (answer == "/Q"){

The second issue is that you don't compare strings like this. Instead use the "equals()" method.

if(answer.equals("Q")){

See here for more information: How do I compare strings in Java?

Community
  • 1
  • 1
Aequitas
  • 2,205
  • 1
  • 25
  • 51
0

Maybe you can try

if(answer.equals("Q"))
Kim Leo
  • 1
  • 3
  • Yeah this worked, but I still would like to understand why the prior one didn't work thanks for this though ill probably just switch to this – Winter One Mar 30 '15 at 02:07
  • @WinterOne **==** just do check for if these two object are exactly same one, and **equals** do the check if two string has came content. – Kim Leo Mar 30 '15 at 05:13