java - String [] Array Error -


when used string [] array, this:

 import java.lang.string.*;    import java.text.decimalformat;    import java.text.numberformat;  public class javajoe  {  public static void main(string args[])   {  string [] day = {"monday", "tuesday", "wednesday", "thursday", "saturday", "sunday"}; 

the output of this:

if(day[0] == ("monday"))  {  double cost = 30;  double totalcost = 30 * 1.15; //cost including tax  money = money - totalcost;  system.out.println("it " + day + " , joe has spend " + decimal.format(totalcost) + " on new pair of shoes. has " + decimal.format(money) + " left.");  } //if 

gave me this:

it [ljava.lang.string;@1ea2dfe , joe has spend $34.50. have $165.50 left.

can tell me why? why doesn't tell me it's monday? in advance!

because printing array itself, calls tostring method of java array. if check implementation of method, see doesn't print actual values, instead print unique hash object.

object.tostring()

returns string representation of object. in general, tostring method returns string "textually represents" object. result should concise informative representation easy person read. recommended subclasses override method.

the tostring method class object returns string consisting of name of class of object instance, at-sign character `@', , unsigned hexadecimal representation of hash code of object. in other words, method returns string equal value of:

getclass().getname() + '@' + integer.tohexstring(hashcode()) 

now in code change day day[0] , use equals or equalsignorecase string comparison. == reference equality.

if(day[0].equals("monday"))      system.out.println("it " + day[0] + " , joe has spend " + decimal.format(totalcost) + " on new pair of shoes. has " + decimal.format(money) + " left."); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -