user2900130
user2900130

Reputation: 13

Array and Java string error: [Ljava.lang.String;@19c42c4b

I've created a program that allows a user to enter in Journal entries (up to 7 days) and then allows a person to call up one of those days after they enter in an entry. Unfortunately, this has left me with some weird string error that I'm not familiar with.

Code as follows:

public class eDiary{
public static void main (String args[]){
    int[] days = new int[7];//get our days
    days[0] = 1;//start with 1 and not 0
    days[1] = 2;
    days[2] = 3;
    days[3] = 4;
    days[4] = 5;
    days[5] = 6;
    days[6] = 7;
    String [] events = new String[7];//events for the days
    int i = 0;

    //asks for input and counts
   for(i=0; i<7; i++){
    String event = Console.readString("Tell me the major event of day " + days[i] + "\n");
    events[i] = event; 

}

 int journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
 while (journal_entry != 0) { 
     System.out.println(events);
     journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");   
    //get r dun!

The input and output:

Tell me the major event of day 1
one
Tell me the major event of day 2
two
Tell me the major event of day 3
thre
Tell me the major event of day 4
four
Tell me the major event of day 5
five
Tell me the major event of day 6
six
Tell me the major event of day 7
seven
Enter what day you want to hear or Enter 0 to stop 
1
[Ljava.lang.String;@10181f5b
Enter what day you want to hear or Enter 0 to stop 
0

Howdy y'all!

Thanks a lot for the quick responses. One thing it seems to be doing now is when replacing

System.out.println(events);

with

System.out.println(events[journal_entry]);

Now gives me input such as this:

Tell me the major event of day 1 
first day
Tell me the major event of day 2
second day
Tell me the major event of day 3
third day
Tell me the major event of day 4
fourth day
Tell me the major event of day 5
fifth day
Tell me the major event of day 6
sixth day
Tell me the major event of day 7
seventh day
Enter what day you want to hear or Enter 0 to stop 
1//the day im asking for
second day//spitting out the next day's entry instead of the first day's entry
Enter what day you want to hear or Enter 0 to stop 
0//this is me stopping it

Upvotes: 1

Views: 57680

Answers (8)

Stephen C
Stephen C

Reputation: 719436

The [Ljava.lang.String;@10181f5b stuff is what you get when you explicitly or implicitly call Object.toString() and the target object's class doesn't override toString(). In this case, the issue is that Java array types do not override toString().

If you want to output an array, use java.util.Arrays.toString(...) to convert it to a String, then output that.

But in this case, you actually need to output a specific entry, not the entire array. The fix is to change

    System.out.println(events);

to

    System.out.println(events[journal_entry]);

For the record, the stuff above consists of the classes internal name ("[Ljava.lang.String;") and the object's identity hashcode (in hexadecimal).

This is not a "weird error string".

Upvotes: 4

user2900130
user2900130

Reputation: 13

Thanks for all the responses! I was able to resolve the issue. Here's the code if anyone is curious:

public static void main (String args[]){
int[] days = new int[7];//get our days
days[0] = 1;//start with 1 and not 0
days[1] = 2;
days[2] = 3;
days[3] = 4;
days[4] = 5;
days[5] = 6;
days[6] = 7;
String [] events = new String[7];//events for the days
int i = 0;

//asks for input and counts


for(i=0; i<7; i++){
String event = Console.readString("Tell me the major event of day " + days[i] + "\n");
events[i] = event; 

int journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n"); while (journal_entry != 0) { System.out.println("On day " + days[i = 0] + " " + events[journal_entry - 1]); journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");

Upvotes: 0

adekorir
adekorir

Reputation: 21

It won't print out the answer correctly because you just pointed System.out.println() to events which is supposed to be an array pointer and not the actual variable. You should just replace this line with

System.out.println(events[journal_entry]);

For it to make sense. Run it with the conmmand and see if it will run properly.

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

In java array's are consider as object. you are printing the event array object that's not what you want.

You need to print name of the day in a week. You need to replace

 System.out.println(events);

to

 System.out.println(events[journal_entry]);

Upvotes: 0

Maroun
Maroun

Reputation: 96018

The output you are getting is because:

In Java, each object has toString() method, the default is displaying the class name representation, then adding @ and then the hashcode.

You should use Arrays#toString(), which is implemented this way:

3860     public static String toString(int[] a) { {
3861        if (a == null)
3862            return "null";
3863        int iMax = a.length - 1;
3864        if (iMax == -1)
3865            return "[]";
3866
3867        StringBuilder b = new StringBuilder();
3868        b.append('[');
3869        for (int i = 0; ; i++) {
3870            b.append(a[i]);
3871            if (i == iMax)
3872                return b.append(']').toString();
3873            b.append(", ");
3874        }
3875    }

This will help you to better understand arrays.

Of course you can manually loop on the array and print it:

for(String event: events) {
    System.out.println(event);
}

Upvotes: 2

Iwo Kucharski
Iwo Kucharski

Reputation: 3825

This is not an error. You want to print the value of variable events. [Ljava.lang.String;@10181f5b means that events is an array of type java.lang.String and 10181f5b is hashcode of this variable. What you want to println is event[i] where i is the number of a day.

Upvotes: 0

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33096

There is nothing wrong and that's not an error message.

Instead, it's the string representation of an array. Consider this line:

System.out.println(events);

You are printing the whole array, so you get that representation -- which happens to be a bit ugly, indeed. You want to print only one element, the one corresponding to the selected day. Use:

System.out.println(events[journal_entry]);

And perform bound checks.

Upvotes: 0

BackSlash
BackSlash

Reputation: 22243

It's not an error.

System.out.println(events);

In this line you are trying to print the array, but that statement doesn't print the array contents, it only prints the object class name followed by its hashcode.

To print the array content you have to use

System.out.println(Arrays.toString(events));

Or, if you want, loop through the array and print its values

Upvotes: 10

Related Questions