• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing the content of an array  RSS feed

 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, bare in mind that I am an absolute beginner at Java and I'm following the Derek Banas youtube tutorials. I am currently learning about arrays but a little bit stuck at the moment. This is what i want to create as a program.

1. I want to ask the user to input a number (lets say X)
2. Bases on the users input number (X) I want to create an array (with size X) and fill it with random generated numbers. So if the user fills in 5, there array has to have 5 random generated numbers.
3. After that I want to ask the user to guess the 3rd number in the array.
4. When correct print out a message, when incorrect let the user guess again.

This is my code below...I know, perhaps its not written as a pro but remember, I'm a complete newbie and don't have that much time to learn Java. Currently I'm stuck at the part to print out the array. Eclipse always prints [I@5cbc508c a number of times and not the values of the array. Can someone give me a hand ?

Thanks in advance.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris De Cooman wrote:Eclipse always prints [I@5cbc508c a number of times and not the values of the array. Can someone give me a hand ?

Yes. That's because Java's "array" class (which is hidden) doesn't override the toString() method - and it really ought to.

But until it does, you can use:
  System.out.println( Arrays.toString(myArray) );

HIH. And welcome to JavaRanch.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kris,

Arrays don't have their toString() method overloaded, so printing one just prints the class name followed by the hash code. To print the contents of an array, you should first use Arrays.toString().

There are some other issues with your code:

  • Arrays.fill() overwrites the entire contents of an array. I'm guessing that's not what you want.
  • As a client of the createArray() method, I would expect it returns an array. It doesn't return anything.
  • You're performing an assignment in a conditional expression (z = ???).
  • You say you want the user to guess the third number, but what if the array only contains two numbers?
  •  
    Chris Green
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks to the both of you... I already figured it out on my own, guess it just came to me :-)

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You might want to change the flow of your application. What you're doing now is very typical for old procedural programs.

    It doesn't make sense to have createArray() call guessNumber(). createArray() should create an array, and not guess a number.

    Do you know how to create and use objects?
     
    Chris Green
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:You might want to change the flow of your application. What you're doing now is very typical for old procedural programs.

    It doesn't make sense to have createArray() call guessNumber(). createArray() should create an array, and not guess a number.

    Do you know how to create and use objects?


    But createArray() does create the array no ?! In this method i do call guessnumber and pass a variable to it...
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, it does create an array, but it also guesses a number. Then why is it not called createAnArrayAndGuessNumber()? It's better to give methods a clear responsibility and nothing more. main() should probably first call createArray(), and then guessNumber().

    It also doesn't really make sense to create an array full of random ints, and then discard the entire array except for one number. Why not just generate a single number?
     
    Chris Green
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:Yes, it does create an array, but it also guesses a number. Then why is it not called createAnArrayAndGuessNumber()? It's better to give methods a clear responsibility and nothing more. main() should probably first call createArray(), and then guessNumber().

    It also doesn't really make sense to create an array full of random ints, and then discard the entire array except for one number. Why not just generate a single number?


    To force myself to create something a little more challenging :-D
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then I challenge you to write a program that asks the user to guess *all* numbers in the array, one by one, until they've guessed them all correctly.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't use Math#random and start doing arithmetic with it. If you want such a range of random numbers, use a Random object. Better still, you can use the Random object to create the array for you:-That only works in Java8.
     
    Chris Green
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:Then I challenge you to write a program that asks the user to guess *all* numbers in the array, one by one, until they've guessed them all correctly.


    Hi Stephan...Sorry it took me so long to reply to your post. Like I already mentioned in this thread I don't have a lot of time to practice you and I am a real beginner at it. This morning I tried to program what you asked me to..A program that asks the users to guess all the numbers in an array. I think I had to use an arraylist for this...when browsing the web I read that an arraylist is resizeable and a normal array isn't. Thats why i used an arraylist. The program seems to be working for me. Btw I know I am always printing back the arraylist but that because I want to know if I'mactually guessing right or not :-) Ok so here is my superclass with my main:



    And here is my subclass where i actually do the guessing...



    Please give feedback on what I'm doing ok/nok..

    With regards.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Make the parameter in your method List<...> rather than ArrayList<...>.
    You have got ><Something> in your code tags. I believe that is an error introduced by the forum code.
    Why are you using a List to remove things from your List? You appear to have only one number to remove, so you can simply remove that number.
    If you use List#removeAll, that suggests to me that you only have one instance of each number, rather like lottery balls. In which case, why are you using a List? Why not use a Set<Integer> instead?
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nearly three weeks ago, I wrote:. . .That only works in Java8.
    You cannot directly get a List from that IntStream, but you can turn it into a Stream<Integer> with the mapToObj() method. That needs some sort of function to get an Integer from an int; I am going to try telling it to use the valueOf() method of the Integer class. In the case of a List, you can use the size given earlier (5L) and you need a Collector reference for the collect() method. The simplest way to get such a Collector is to use a method of the Collectors class which returns a ready‑made Collector.For a Set, you remove duplicates; in order to get five elements, you remove the 5L argument from the ints() method. You then call the IntStream#distinct() method and follow it with IntStream#limit(5L) which means you don't get more than 5 elements. I challenge you to work out what will happen if I write limit(11L) Also, what would happen if you change the order of the limit and distinct calls. Also what would happen if you miss out the distinct and limit calls and use 5L as you did for List and array.
     
    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
    You call the guessArray method recursively in two places (here's one of them):

    This is a bad use of recursion.  Create a loop instead.
     
    Jaypee Hernandez
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a question. Why didn't you used the Random class?

     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But he is using a Random object.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!