Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList problem  RSS feed

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are generating only one random number, in line 1.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MY FULL PROGRAM WITH ALL THE CLASSES:







 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55735
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to use a java.util.Random object, surely?
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!