Results 1 to 12 of 12
  1. #1
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default error [Ljava.lang.String.... when compiling

    code is giving this error
    [Ljava.lang.String;@42719c [Ljava.lang.String;@30c221 [Ljava.lang.String;@119298d [Ljava.lang.String;@30c221

    how can i solve this?

    Java Code:
    String[] aa = { "question aa" };
              String[] bb = { "question bb"};
              String[] cc = { "question cc"};
              String[] dd = { "question dd"};
    
             
    String[][] questions = { aa, bb, cc, dd };
    
    int leng = questions.length;
           for (int i = 0; i < leng; i++)
           {
               int rand = (int) (Math.random() * leng);
               System.out.print(questions[rand]);
               System.out.print(" ");
           }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    That's not an error. You're trying to print an array, and that's what you get. Either loop through the inner arrays as well, or look at the Arrays API, or don't store single Strings as an array of Strings with a single index.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    so how can i do the same code in another way?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pipikrk View Post
    so how can i do the same code in another way?
    Maybe you understand better what you're doing (wrong) if you change this line:

    Java Code:
               System.out.print(questions[rand]);
    ... to this:

    Java Code:
               System.out.print(Arrays.toString(questions[rand]));
    You are trying to print an entire array (a row of a two dimensional array).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    thanks for u answer , it solve my question.

    Just 1 little question , how can i avoid that the random , display repeated questions?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pipikrk View Post
    thanks for u answer , it solve my question.

    Just 1 little question , how can i avoid that the random , display repeated questions?
    Your questions array contains just four elements so you can only generate four different (pseudo) random numbers before the sequence starts to repeat itself. Is that what you have in mind? Also read the API documentation for the Collections.shuffle( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    i just pretend that the question doesn't repeat more than 1 time,
    i was reading collection.shuffle , should i use method synchronizedMap , or you advise some other?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Why would you use that method?

    Convert your array into a Collection. Shuffle it. Iterate over it. Voila, each String, in a random order.

    Why are you using a 2D array of Strings instead of a single array of Strings?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    because i want to make kind of questionaire, with question and answer, so then i wanna to syncronize the question with the answer

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Ah, I see. Chances are you should probably create an Object that keeps track of a question and answer pair. Is that why you thought of using a Map?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    yes that why, what do you think is the best way to do this?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pipikrk View Post
    yes that why, what do you think is the best way to do this?
    You either have to remember which questions were already asked, so if a random number random generator selects an already asked question try again.

    Or, generate a list of all question numbers: 0, 1, 2, ... n-1 and use the Collections.shuffle( ... ) method to shuffle the list and use the first m questions if you want to aske m <= n questions.

    If m is very much smaller than n the first method does fine, otherwise the second method is to be preferred.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 03-17-2011, 02:26 PM
  2. Replies: 5
    Last Post: 10-10-2010, 12:17 AM
  3. [Ljava.lang.Object; cannot be cast to domain.Customer
    By niteangell21 in forum New To Java
    Replies: 7
    Last Post: 09-26-2010, 08:38 PM
  4. [SOLVED] Ljava.lang.String;@923e30 at runtime
    By Jerome in forum New To Java
    Replies: 6
    Last Post: 12-31-2008, 11:08 AM
  5. Replies: 2
    Last Post: 09-18-2008, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •