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.
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
• Number of slices to send:
Optional 'thank-you' note:

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

Problem: why am i not getting three random numbers...

Java Cowboy
Posts: 16084
88
• 2
• Number of slices to send:
Optional 'thank-you' note:
Because you are generating only one random number, in line 1.

abrar alvi
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
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
• 1
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
MY FULL PROGRAM WITH ALL THE CLASSES:

Jesper de Jong
Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:

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).

generates one random number between 0 and 4 (inclusive). Not a random number between 1 and 6.

abrar alvi
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Better to use a java.util.Random object, surely?

abrar alvi
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• 1
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• 1
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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...

Matthew Brown
Bartender
Posts: 4568
9
• 2
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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