-2

I am writing my first Java program. Just for practice and a learning experience for a complete beginner. The program is a retirement calculator and I am having an issue with the code not flowing directly into the next code that I wrote.

    System.out.println("What investment style best suits your needs? i.e.(Very Aggressive, Aggressive, Moderate, Conservative)");
    String investmentstyle = scanner.toString(); 

        if (investmentstyle == "Very Aggressive") {
            System.out.println("With this type of fund, almost 100% of your funds would go towards stocks. you could expect a 12% rate of return on your investment.");
            }
        else if (investmentstyle == "Aggressive") {
            System.out.println("With this type of fund, 75% of your funds would go towards stocks and 25% would go towards bonds. You could expect a 9% rate of return on your investment.");
            }
        else if (investmentstyle == "Moderate") {
            System.out.println("With this type of fund, 50% of your funds would go towards stocks and 50% would go towards bonds. You could expect a 6% rate of return on your investment.");
            }
        else if (investmentstyle == "Conservative") {
            System.out.println("With this type of fund, 25% of your funds would go towards stocks and 75% would go towards bonds. You could expect a 3% rate of return on your investment.");
            }

    System.out.println("Please enter the expected return rate that is in accordance with the investment style you have chosen.\n Make sure you don't enter the percentage sign (%).");
    double interestrate = scanner.nextDouble();

As you can see here, I am asking the user what type of fund he/she would be interested in. I want that input to be processed by the if/else to display the statement that correlates to the fund they chose. As of now, when I run the program it completely bypasses the if/else and moves on to then next question I ask. How would I go about fixing this?

As always, any and all help is appreciated. Thank you in advance!

  • I suggest you use enums instead of Strings, but if you are going to use Strings you really need to check that are `equals()` as `==` will check they are the same object,not just that they contain the same information. – Peter Lawrey Jun 25 '14 at 16:13
  • You should never compare `String`s with `string1 == string2`; always use `string1.equals(string2)`. Essentially, `==` checks if the two strings are the exact same object (`new String("1")` and `new String("1")` are not), but `.equals` checks if the internal character arrays contain the same values. For more information, see [How do I compare strings in Java?](http://stackoverflow.com/q/513832/2846923). – The Guy with The Hat Jun 25 '14 at 16:13
  • 3
    One more thing -- `scanner.toString();` does something different than you expect. I believe you would want `scanner.nextLine()`. – awksp Jun 25 '14 at 16:14
  • @TheLostMind Hey, *maybe* OP expects the arcane default implementation, but that's not happening... Fixed. – awksp Jun 25 '14 at 16:17

5 Answers5

1

== compares the object reference of the two strings. In order to compare two string character by character you need to use equals()

Example:

String s1 = new String("Hello");
String s2 = new String("Hello");

System.out.println(s1.equals(s2)); // True
CMPS
  • 7,733
  • 4
  • 28
  • 53
1

== checks if the two references point at the same object, where the method equals will actually check for equality in your case:

if ("Very Aggressive".equals(investmentstyle)) {
            System.out.println("With this type of fund, almost 100% of your funds would go towards stocks. you could expect a 12% rate of return on your investment.");
            }
        else if ("Aggressive".equals(investmentstyle)) {
            System.out.println("With this type of fund, 75% of your funds would go towards stocks and 25% would go towards bonds. You could expect a 9% rate of return on your investment.");
            }
        else if ("Moderate".equals(investmentstyle)) {
            System.out.println("With this type of fund, 50% of your funds would go towards stocks and 50% would go towards bonds. You could expect a 6% rate of return on your investment.");
            }
        else if ("Conservative".equals(investmentstyle)) {
            System.out.println("With this type of fund, 25% of your funds would go towards stocks and 75% would go towards bonds. You could expect a 3% rate of return on your investment.");
            }
Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48
1
  • String investmentstyle = scanner.nextLine();//To Fetch Whole Line As you are looking for comparison with String which contains space .
  • if (investmentstyle.equalsIgnoreCase("Very Aggressive")) { because it will allow user to enter data irrespective of case.

You will love to read answers of How Do I compare Strings in Java

Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
0
if (investmentstyle == "Very Aggressive") // wrong. use .equals() instead of "=="
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Do not use == to check string equality.

Use switch case instead:

switch(investmentstyle) {
  case "Very Aggressive": 
            // do something
            break;
  case "Aggressive": 
            // do something
            break;   
  // ...
  default: // do nothing
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118