0

When I used a String [] array, like 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 is " + day + " and Joe has to spend " + decimal.format(totalCost) + " on a new pair of shoes. He has " + decimal.format(money) + " left.");

} //if

gave me this:

It is [Ljava.lang.string;@1ea2dfe and Joe has to spend $34.50. He have $165.50 left.

Can you tell me why? Why doesn't it tell me that it's Monday? Thanks in advance!

2
  • day is the array; you need to access an entry of the array. Commented May 21, 2013 at 10:12
  • 1
    use if(day[0].equals("Monday")) and then print day[0]. Commented May 21, 2013 at 10:12

8 Answers 8

6

Because you are printing the array itself, which calls the toString method of a Java Array. If you check the implementation of this method, you will see it doesn't print the actual values, but instead it will print a unique hash for that object.

Object.toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Now in your code change day to day[0] and use equals or equalsIgnoreCase for String comparison. == is reference equality.

if(day[0].equals("Monday")) 
    System.out.println("It is " + day[0] + " and Joe has to spend " + decimal.format(totalCost) + " on a new pair of shoes. He has " + decimal.format(money) + " left.");
Sign up to request clarification or add additional context in comments.

Comments

1

First, use equals to compare strings (i.e. day[0].equals("Monday")).

Then, print day[0] instead of day.

Comments

1

to compare Strings you have to use equals:

if(day[0].equals("Monday"))

Comments

1

You're printing day, wich is an array.
Use day[0].

Comments

0

Because you are printing array

It should be day[0] not day

Comments

0

== operator checks whether the two references point to the same object or not whereas .equals() function will check the string content.

String name1 = new String("John");
String name2 = new String("John");
if(name1 == name2 )
{
  System.out.println("Both point to same object");
}
else
{
   System.out.println("Both point to different object");      
}

output is:

Both point to different object

whereas for

if(name1.equals(name2) )
{
  System.out.println("Both have same content");
}
else
{
   System.out.println("Both have different content");      
}

output is :

Both have same content

Comments

0

There's no error, you are printing an array in stdout instead of its elements. this is the default toString() for objects (printing memory location) printday[index] instead of printing the whole array.

Comments

0

Use days[0].equals("Monday");

Secondly ,instead of System.out.println(day); use System.out.println(day[0]).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.