1

i hope you can help me :) i want to get a string of font file path from sharedpreferences, and if there isnt any string load the default font in assets, okay. the problem comes when i try to stablish the condition, see it here:

 String filePath = new String(preferences.getString(String.valueOf(v.getId()+"font"),"default"));
    Log.d("","choosed font: "+filePath);

    if (filePath == "default"){
        v.setTypeface((Typeface.createFromAsset(getAssets(),"fonts/Default.ttf")),preferencias.getInt(String.valueOf(v.getId()+"style"), 0));
    }else{
        v.setTypeface(Typeface.createFromFile(filePath),preferencias.getInt(String.valueOf(v.getId()+"style"), 0)); 
    }

as you can see, the program would load default font if filePath = default, but the program always executes the order inside "else"...

the order to make log appear shows clearly: chosen font: default. because i have never putString

so why is this condition not satisfied??

BamsBamx
  • 4,139
  • 4
  • 38
  • 63

6 Answers6

3

Compare String using equals()

if (filePath.equals("default"))

Read this for more information.

== compares references,not the values. In your case, you want to check for the value equality, not the reference equality.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
2

try

filePath.equals("default")

instead of

if (filePath == "default")
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

== checks if the reference is same.

.equals check the value

Habib
  • 219,104
  • 29
  • 407
  • 436
1

Do not just use this:
This compares references, not values!

if (filePath == "default")


always use equals(...) for Strings!

if (filePath.equals("default"))


Corrected version of your code:

 String filePath = new String(preferences.getString(String.valueOf(v.getId()+"font"),"default"));
    Log.d("","choosed font: "+filePath);

    if (filePath.equals("default")){
        v.setTypeface((Typeface.createFromAsset(getAssets(),"fonts/Default.ttf")),preferencias.getInt(String.valueOf(v.getId()+"style"), 0));
    }else{
        v.setTypeface(Typeface.createFromFile(filePath),preferencias.getInt(String.valueOf(v.getId()+"style"), 0)); 
    }

Explanation: http://leepoint.net/notes-java/data/expressions/22compareobjects.html

Compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.

Thkru
  • 4,218
  • 2
  • 18
  • 37
0

use this to avoid NullPointerException if filePath is null

if ("default".equals(filePath ))
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
  • 1
    What if that is the desired behaviour? – dacwe Jun 18 '12 at 13:02
  • It depends what you want to do. If you want always avoid NPE you should use this construction, if your reference can be null and this a part of your logic you can use usual construction and catch exception. – Georgy Gobozov Jun 18 '12 at 13:18
0

try this

if (filePath.equals("default"))
Cool Compiler
  • 857
  • 2
  • 9
  • 20