• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Calling an int from an void to a string?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys!
Brand new here. I'm trying to create a game of Lucky Sevens for a beginning Java class, and I'm having some trouble. I'm tried to define the variable numofTurns by looping the "game" until you run out of money. (This is seen in playGame). The variable numofTurns will be how long it takes until you run out of money. Then, in getGameReport, i'm trying to return the amount of turns it took. I'm not sure how to link these values together, and I'd love some help!


If there are any other mistakes then please let me know. I'm pretty new to Java, and saw this website when I was trying to find some tips online and figured i'd give it a shot!
Thanks in advance!
John
 
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how to link these values together,


Can you explain what that means?



Note: There are several variables declared with very similarly spelled names that can be a confusion.  Java is case sensitive so spellings need to  be checked very closely.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can you explain what that means?


I'm trying to call the amount of turns from playGame to getReport. For example, if you last 10 turns in the game, when you call the report It should return "you lasted 10 rounds"
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the code do now?  
I see a return statement near the end of the program with the message to be shown,  There needs to be code to print the String that is returned.

See the end of my last post for more comments.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attached is the youtube video of the current state of the code.
As you can kind of see in the video, my goal is that the game asks you how much money you want to start with. During the game, if you roll a 7, you get four dollars. If you roll anything other then 7 (from 1-12) then you lose one dollar. This game will run in a loop until you run out of money, and tell you how many turns you lasted for. I tried to intialize numofTurns in playGame, however it doesn't work in getGameReport.
I can't even run the right code as it is a syntax error of having an undefined variable. Instead I just deleted the code and put return "hi" so that it could run and you could see the rest of the program. However, what's weird to me is that at the end of playGame(), it prints "good game" and then calls gameReport. The game displayed the "good game" from playGame, however did not display "hi" from getGameReport.
Anyways, the return hi is not really important. I just need to figure out how to get a value for numofTurns during playGame, and call it on getGameReport so I can tell the player how long they survived.
Thanks!

 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how to get a value for numofTurns during playGame, and call it on getGameReport so I can tell the player how long they survived.


Where is that variable declared?
Where does the code add to that variable?  
Where does the code print out the value in that variable?

Earlier I said:

Note: There are several variables declared with very similarly spelled names that can be a confusion.  Java is case sensitive so spellings need to  be checked very closely.


Did you check the variables names to see if there was a problem?
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where is that variable declared?
Where does the code add to that variable?  
Where does the code print out the value in that variable?


I first declare it in line 8. Then in playGame(),

I initialize it at 0, then after you playTurn(); - which basically just rolls the dice and edits balance - for each turn where your balance is greater than zero, it adds a turn to numofTurns. This should repeatedly add one turn until you run out of money. Then once you run out, it prints "Good Game." and calls GameReport, where I believe is where my problem is. My code as of right now is

My goal is to return "you lasted a whopping " + number of turns taken to lose all your money + " rounds";. I tried to define the variable again so I can use numofTurns in the return statement, but I'm pretty sure I messed that part up. I just don't know how to fix it!
And yes, I checked the variable names and although they are all pretty similar, there is no problem there.

 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the variable declared on line 8 used anywhere?  If you comment it out does the compiler give an error message?

Does the compiler give an error message for the code on line 55?  What is that code supposed to do?

Did you see this:

Note: There are several variables declared with very similarly spelled names that can be a confusion.  Java is case sensitive so spellings need to  be checked very closely.


Did you check the variables names to see if there was a problem?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 See below
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 8 of the LuckySevens class you declare a variable named numOfTurns, wjicjh is never used anywhere.
In playTurn() you declare a new local variable called numofTurns which is unused.
In playGame() you also declare another new local variable numofTurns which you increment in the loop but never use again.
Then in getGameReport() you again declare another new local variable numofTurns.

As Norm Radder said, pay attention to the spelling of variables, and don't keep re-declaring new ones that hide the instance variable declared on line 8.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is the variable declared on line 8 used anywhere?  If you comment it out does the compiler give an error message?


It was used in my playTurn() where I set it to 0. This line was not needed and I have no idea why it was in there, so I deleted it but nothing changed. I think it's because I already set it to 0 during playGame(). Regardless, I commented line 8 out and there was no error message from the compiler.

Does the compiler give an error message for the code on line 55?  What is that code supposed to do?


Yes, it says "variable numofTurns might not have been intialized". The code was really just because in line 56 I was trying to call numofTurns from playGame and it said the variable didn't exist. I added in 55 to try and somehow call the value from playGame? I honestly have no idea what the purpose of it is, i'm just trying to call the value from  playGame, and I have no idea how to do it.

Did you see this:


Yes, I checked the other variables and names throughout the program and there are no issues with spelling or variable names.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clearly I did not check the spelling of the other variables. I apologize, that was totally my fault haha. I  deleted the line 55, and now my getGameReport is simply
When I compile it there is no issues, but when I run the program it just displays "Good Game!" but doesn't call getGameReport even though at the end of playGame it calls it?
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Regardless, I commented line 8 out


This is the only declaration of it you should KEEP!

And getGamereport() does nothing but return a String; you never do anything with that string.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clear up any confusion, my new code is:

The issue now is when I run the program, it doesn't call getGameReport. It just stops at "Good Game" from the previous method
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

doesn't call getGameReport


The code on line 49 called it.  Was that line removed?
There is no code on line 49 to print the String returned by the method.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is no code on line 49 to print the String returned by the method.


And getGamereport() does nothing but return a String; you never do anything with that string.


So how would I call getGameReport after playGame?
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how would I call getGameReport after playGame?


getGameReport  is called on line 49.  The problem is nothing is done with the String that is returned by that method.  Add some code to print the String that is returned if you want to see it.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you need to change the declaration of getGameReport() so it will take one int parameter.  Second, you need to pass numofTurns to the getGameReport() method so you can use it in the method.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I changed the code to

Now when I run the program I get this:

Now it prints what I want, but now the value of numofTurns is just reverting to 0. How would I fix this?

First you need to change the declaration of getGameReport() so it will take one int parameter.  Second, you need to pass numofTurns to the getGameReport() method so you can use it in the method.


I tried to change the declaration of getGameReport() to getGameReport (int numofTurns) but that just caused an error when I try to call it from playGame.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your playGame() method declares and uses a local variable called numOfTurns, which goes out of scope when the method ends.
You later print the instance variable numofTurns, which was never initialized nor modified.
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the variable: numofTurns declared?
Where is it incremented?

I see that the name was changed from numOfTurns.

You need to post all of the code so we can see how the variable is used.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Where is the variable: numofTurns declared?
Where is it incremented?

I see that the name was changed from numOfTurns.

You need to post all of the code so we can see how the variable is used.



Here is the code that it is in right now! This is the only place that numofTurns is defined. Note that it is defined in playGame as 0, and then upon calling playGame it should call playTurn in a loop that would add 1 to numofTurns as long as we have balance remaining.

When you run the program right now, it just returns " you lasted 1 turns" every time, regardless of input. I think there is something wrong with how I incoporated the numofTurns = numofTurns + 1 but I can't figure it out haha i've been playing with it for a while.
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the code that it is in right now!


You need to post ALL of the code, not just some parts of it.
 
John Noneya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You need to post ALL of the code, not just some parts of it.


Sorry!

Sometimes when run it will say "you lasted a whopping x rounds" and give me an practical answer, just like it should. However if you rerun it it will just revert to 1. It only gives me an practical answer a few times when I run it over and over, so I really have no idea where it's messing up. It works sometimes, but I need it to work everytime haha
 
Norm Radder
Master Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print out the values of balance and numofTurns at the end of playTurn so you can see what is happening.
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Noneya wrote:Sometimes when run it will say "you lasted a whopping x rounds" and give me an practical answer, just like it should. However if you rerun it it will just revert to 1. It only gives me an practical answer a few times when I run it over and over, so I really have no idea where it's messing up. It works sometimes, but I need it to work everytime haha


Well, this is expected.  The only way you can last more than one round is to roll a seven your first turn.  That doesn't happen all that often.

Did you mean to have a balance entered through the constructor?  Because right now that isn't happening.  You would need to change the constructor like this:
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Completely different issue, I amafraid. I don't like arithmetic with Math.random(). I think it is far better to use a Random object, as described here. Also please put some spaces in that code. You formatted all the rest of the code so well, but missed out those two lines:-You can shorten that method like this:-It would be better form to avoid the magic number by declaring FACES_ON_DIE as a constant (6).
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic