• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I display array variables returned from method?  RSS feed

 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the source codes:
That is my lottery class.

Display results are..

The winning numbers are.....
[I@6bc7c054 Better Luck next time!
[I@6bc7c054 Better Luck next time!
[I@6bc7c054 Better Luck next time!
[I@6bc7c054 Better Luck next time!
[I@6bc7c054 Better Luck next time!

Would I need to implement the for loop to return all elements from the array? Because I thought that the field variable has already been initialized and I can return all the data stored in it.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (← this is a link) when posting.
I have added them for you this time.

Have you looked at Arrays.toString()?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← that's a link) when you post code. (I looks like someone did it for you this time.)

You will need a loop to display the lottery number. Being a field doesn't change that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also...the fact that the array was "returned from a method" is really irrelevant. you should learn to think about what needs to be done in the most basic way possible. The reason being, if you write code that displays the elements of an array, you can use it whether you get the array from a method, de-serialization of an object, you create it manually, it is a member of some class...etc.
 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Also...the fact that the array was "returned from a method" is really irrelevant. You should learn to think about what needs to be done in the most basic way possible. The reason being, if you write code that displays the elements of an array, you can use it whether you get the array from a method, de-serialization of an object, you create it manually, it is a member of some class...etc.


Well, I have read the book, and that's the only way it showed me how to return arrays from a method.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have read the book, and that's the only way it showed me how to return arrays from a method.


Well, that's true. But what about displaying an array? How about creating a method called displayLotteryNumber() that uses a loop or the Arrays.toString() method?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unless you have been told to write your own method for displaying an array, this is what everybody else is telling you about.
 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have updated the method. I have been told it is best to avoid implementing System.out.println methods in classes because it defeats the purpose of making the class program "generalized and simple". Is it a beneficial practice to implement classes that can contain System.out.println methods in some cases?

Thank you for the help guys.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand the question. What I do know is that print calls are slow, taking several milliseeconds. You won't notice if you have only a few things to print, but it is a good idea to create a utility methodThen you only need one printing call. Read about StringJoiner here. Only available in Java8. There is an example with a Stream shown in that link, which is probably better than a loop.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!