2

I'm trying to learn and build my first program. It is a program to work out a cars mpg.

I've created a variable that sets itself to the users iso3 code so when the program is used in different country, I can use it to change other variables in the code - ie. a gallon in the USA and the UK are slightly different so set the correct gallon value for what ever country the program is being used in.

The problem I've got is creating an if statement to get it to work.

Locale defaultLocale = Locale.getDefault();
String usercountry = (defaultLocale.getISO3Country());

I've tried this:

if (usercountry == USA{
    mpg = mpg*0.83;
}

And this:

if (usercountry.equals(USA)){
    mpg = mpg*0.83;
}

But both come back saying USA cannot be resolved to a variable. Am I missing something small or am I going about it the wrong way? Thanks in advance.

Calcolat
  • 878
  • 2
  • 11
  • 22
jammypotato
  • 65
  • 1
  • 7

2 Answers2

3

In Java, String literals need to be surrounded by double quotes: "USA", not USA. Changing your code to

if (usercountry.equals("USA"))

or

if (defaultLocale.equals(Locale.US))

should work.

The former will check whether the ISO 3166-1 alpha-3 code of the JVM's default locale matches the one denoting the United States ("USA"). The latter will check the locale itself and match it against constant Locale.US.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
  • "Am I missing something small" - this doesn't really answer the question, it just says "do it like this". – user1886323 Aug 03 '15 at 23:06
  • @user1886323, care to elaborate? – Mick Mnemonic Aug 03 '15 at 23:10
  • In my opinion, your answer fails to address the main question the asker is having - "what is wrong with my solution". Although your code does indeed perform what the asker wants, it doesn't explain to him where he went wrong. – user1886323 Aug 03 '15 at 23:13
  • I was editing the answer when you downvoted. Please let me know what is still missing. – Mick Mnemonic Aug 03 '15 at 23:14
  • Put simply, you do not answer this (even in edited form) - "Am I missing something small or am I going about it the wrong way?". You just suggest a different implementation that works. I think the asker needed to have explained that he was missing the quotes around USA. That is the reason for the compiler error he was seeing. – user1886323 Aug 03 '15 at 23:17
  • 1
    My answer addresses the omission of quotes as well. – Mick Mnemonic Aug 03 '15 at 23:18
  • perfect thanks very much – jammypotato Aug 04 '15 at 21:54
0

USA is not being implemented as a string. You will need to either place quotations around it ("USA") or create a String variable holding the string USA. Then you can compare it against usercountry.

Geno Diaz
  • 400
  • 1
  • 7
  • 21
  • 2
    it is worth pointing out that the second of the posters tries (equals() method instead of == operator) is the one to use, combined with your solution. – user1886323 Aug 03 '15 at 22:59
  • @user1886323 correct. Please take this into account when deciding which logical implementation you choose. – Geno Diaz Aug 03 '15 at 23:01