This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

ArrayList problem

 
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, please help me with this code:

Output:
[1, 11, 12]
[1, 11, 12]
[1, 11, 12]


Problem: why am i not getting three random numbers...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are generating only one random number, in line 1.
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, but while adding the objects i have added it with 1 and 2.... is it not the right way to do?? if so can you please suggest me with some code...


thanks
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:ok, but while adding the objects i have added it with 1 and 2.... is it not the right way to do??


That entirely depends on what you're trying to achieve, which isn't obvious from that code.

What you're currently doing is picking a random number, converting it to a String, appending "1" and "2" to it (note that it's using String addition - so concatenation - because you've converted it to a String). You're adding all these to a list. Then you're iterating over the list, but each time printing out the entire list (instead of the current element).

What are you trying to do?
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

What are you trying to do?



I want to add three random numbers from 1 to 6 to the ArrayList.... but the numbers must be added as a string in the list...

this code is a part of the SimpleDotComGame ( page 110) from the Head First Java book by Kathy Sierra & Bert Bates.

i have used the ArrayList instead of the String array but not able to get the desired output.


Shall i post all the three classes here???
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MY FULL PROGRAM WITH ALL THE CLASSES:







 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:I want to add three random numbers from 1 to 6 to the ArrayList....


Can you see how your solution is not doing what you say you want to do?

abrar alvi wrote:ok, but while adding the objects i have added it with 1 and 2.... is it not the right way to do?? if so can you please suggest me with some code...


If you want three random numbers, then you have to generate three random numbers, by calling Math.random() three times. What you are doing now obviously is not generating three random numbers.

Do you understand what your current code is doing? Matthew explained it in detail.

Matthew Brown wrote:What you're currently doing is picking a random number, converting it to a String, appending "1" and "2" to it (note that it's using String addition - so concatenation - because you've converted it to a String). You're adding all these to a list. Then you're iterating over the list, but each time printing out the entire list (instead of the current element).



Your line

generates one random number between 0 and 4 (inclusive). Not a random number between 1 and 6.
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote: What you are doing now obviously is not generating three random numbers.



i need three consecutive numbers from a single random number

so i modified the code as you said...

code:
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:i need three consecutive numbers from a single random number


But that is not what you said before.

So, is it solved now, or does it still not do what you expect? Please explain as clearly and exactly as possible what you want, what your program really does and how that differs from what you expected.
 
Marshal
Posts: 76831
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better to use a java.util.Random object, surely?
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper de Jong,

i have posted all the three classes for my dotcom game....

Program must do the following:

* it is a guessing game...

* the program stores three consecutive numbers as string from 1 to 6 in an ArrayList

* it takes input guess from the user and matches it with the numbers in the arraylist

* if the input number matches with the number in the arraylist it displays hit else a miss

* after all three input given by the user matches with the numbers present in the array list it displays kill and the number of hits..
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so does it work now or not, with the latest change that you posted, or do you still have a question?
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Ok, so does it work now or not, with the latest change that you posted, or do you still have a question?




no the program is not working after the modifications..

its just displaying the following output:

ENTER YOUR GUESS FROM 1 TO 6
1
ENTER YOUR GUESS FROM 1 TO 6
2
ENTER YOUR GUESS FROM 1 TO 6
3
ENTER YOUR GUESS FROM 1 TO 6
4
ENTER YOUR GUESS FROM 1 TO 6
5
ENTER YOUR GUESS FROM 1 TO 6
6
ENTER YOUR GUESS FROM 1 TO 6
7
ENTER YOUR GUESS FROM 1 TO 6
8
ENTER YOUR GUESS FROM 1 TO 6
9
ENTER YOUR GUESS FROM 1 TO 6
0
you took 10 guesses


but its not what i want....

can you please modify the codes which i provided and explain me where i made mistakes
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:* the program stores three consecutive numbers from 1 to 6 in an ArrayList


No it doesn't...and that's your basic problem. It stores three Strings in your ArrayList; and Strings are NOT numbers.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Jesper said, your current code can pick 0. So that output doesn't show that it's behaving incorrectly, except for that - maybe it picked zero?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:
Problem: why am i not getting three random numbers...



Because you are not printing the three random numbers...
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:

abrar alvi wrote:
Problem: why am i not getting three random numbers...



Because you are not printing the three random numbers...




Go through my code and the program description and help me out to get the required output...
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:Go through my code and the program description and help me out to get the required output...


I'm pretty sure that that's what everybody is trying to do.

And I'll repeat my advice: DON'T USE STRINGS.

Winston
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok trying to fix my code again....

can anyone fix the code which i provided in the above post..

i have given all my three classes and described the expected output and the wrong output which i am getting
in my posts...

so please help and modify the given code...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:so please help and modify the given code...


We don't fix people's code here, we help them fix their own. It's a much more effective learning tool.

As my previous post said, one problem is that you are generating a random number between 0 and 4, not 1 and 6. This means that the rest of the program may actually be working (or, at least, the output you're showing doesn't indicate it isn't). So fix that first, and see if there's still a problem.
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all,


At last my program is working fine also i am getting my required output....


I am really feeling happy now....

thanks again
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic