-2
    else if (answer.equals("check value")){
        System.out.println("Of what?");
        System.out.println("Tax, Donations, Income, Profit, Expenditure, Services, Military, City or Economy.");
        System.out.println("Do not use caps and spell it correctly otherwise it will not be recognised.");
        String valueToCheck = scan.nextLine();
        if (valueToCheck == "tax"){
            System.out.println(tax);
        }else if (valueToCheck == "donations"){
            System.out.println(donations + "donations per turn");
        }else if(valueToCheck == "income"){
            System.out.println(incomePerTurn + " income per turn");
        }else if(valueToCheck == "profit"){
            System.out.println(profitPerTurn + " profit per turn");
        }else if(valueToCheck == "expenditure"){
            System.out.println(expenditurePerTurn + "expenditure per turn");
        }else if(valueToCheck == "services"){
            System.out.println(servicesBudget + " to services budget");
        }else if(valueToCheck == "military"){
            System.out.println(militaryBudget + " to military budget");
        }else if(valueToCheck == "city"){
            System.out.println(cityBudget + " to cities budget");
        }else if(valueToCheck == "economy"){
            System.out.println(economy  + " to total economy");
        }
        newTurn();}

Hello, whenever I run my program and type 'check value' I get no errors, in fact I get nothing. I am just wondering why nothing is returned. Thanks for any help!

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 3
    Please add the code that comes befor the `else if`. – Jeroen Vannevel Aug 21 '13 at 19:22
  • 5
    This probably isn't enough code for us to help you. But one thing you're doing wrong is using `==` for String comparison. Use `.equals()` instead. – Daniel Kaplan Aug 21 '13 at 19:22
  • 2
    You seriously used `equals` on `answer` but not on `valueToCheck`? – rgettman Aug 21 '13 at 19:22
  • Add this statement before the long chain of `if` statements: `valueToCheck = valueToCheck.intern();`, or change it to `String valueToCheck = scan.nextLine().intern();` This way, the `==` comparisons will work. – gparyani Aug 21 '13 at 19:25
  • When you say you "get nothing" - do you mean those else if statements aren't executed or do you get literally NOTHING printed from the program? – Andrew Martin Aug 21 '13 at 19:28
  • 1
    @AndrewMartin I'm flagging this as "unclear what you're asking". Why don't you? – gparyani Aug 21 '13 at 19:29

1 Answers1

2

Nothing is showing up because none of those conditions are being met. The "==" operator compares references, but you want to compare contents. Since you are creating new Strings for each of your comparisons, they will not match the one stored in "valueToCheck".

See this question for more details.

Also, in the future, please try to guess where the error is and search the forums before posting. For example, here you have lots of string comparisons, and the output you want is a result of those comparisons. When you didn't get the result you wanted, you should have looked up "string comparisons in java" here or elsewhere to make sure you were doing it correctly before posting.

Community
  • 1
  • 1
LMNOP
  • 151
  • 6