Dave Murph

Greenhorn
+ Follow
since Oct 06, 2021
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
10
Given in last 30 days
10
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dave Murph

Wow - lot to go through, thanks all for the input, a lot of feedback makes sense so I will go back and do some tweaks.
10 hours ago
Am working off creating a class based on this uml and I think I have the bulk of it right in my head since I am new to creating class's,  - a colleague advised me on the @Overide section by toString() but was unclear why so any advise on the format /improvements below would be appreciated in terms of good coding practice.
Game
players: int
genre: String
name: String
--------------
Game(int, String, String)
play()
play(int)
toString(): String
print()

The first play() method outputs “Playing Game” whereas the second inputs the number of players and outputs i.e. “Playing with my 2 friends!” .



To test it I created another class, called TestGame that shows how to create a new Game object , print the details of a Game object and shows the use of both play methods above and the last part is to check that the following code works with the Game class.



As below

12 hours ago
Thanks - duh - I missed that extra line lol. I can change the variable so something that makes more sense, like student1, student2 etc since the plan was to allow for maybe collating the data from 3 or 4 student, running them through the if/else scenario and getting the final output at the end, john doe got a grade xx, jane doe got a grade y etc etc

Still thinking about how I achieve that.



Thinking it out logically, i think if I want  to enter a grade for two students  I should use a loop., then expand the loop to enter grade for 5 students

-> then ask the user to enter a number in console for how many students to enter a grade for and this variable will determine when the loop will end

-> should I  include input validation (do...while loop) to only accept a number greater than or qual to 5.
2 weeks ago
Thanks for all the help so far -

trying to output "grade" in the seems to throw - The local variable grade may not have been initialized, I think I read it might have to redefined elsewhere?

2 weeks ago

Randy Tong wrote:After  int s3 = scan.nextInt(); , add this lineIt helps in clearing out the buffer.



Thanks, that was super helpful - added to my notes

as to trying to simplify it, I totally agree - it looks as clunky as hell and not scalable I guess. Its my first week of trying JAVA so if's and else's are a bit .....

I think If I could improve it by rearranging that part a bit -



Still a bit confused about correlating all the inputs for multiple students and getting one output at the end
2 weeks ago

Hi all - am working my way through "if" and "else and have the basic's I think for a user to input a first name, second name and exam score. Output looks like this:



first name of student1: john
Second name of student1: doe
Enter mark  of student1: 67
john doe received a H2.2 for his mark of 67

now I would like to see if I could input more data, like for student2 and student 3 - then have the results as a total at the end.I tried to add a student2 as an input, like below-



but the output just adds like this -

first name of student1: john
Second name of student1: doe
Enter mark  of student1: 55
first name of student2: Second name of student2:

I am still on the basics at the moment but if someone could guide me towards what I could use in this instance to take multiple inputs and after running through the if/else   --> then display the result at the bottom
2 weeks ago

Campbell Ritchie wrote:You appear to be changing “bottlesȁ to “bottle” one line too late; go through the loop and work out where the best place for that change would be. Otherwise I can't see anything wrong with the output.
Don't use \n; use the printf() method and %n for line end.



Thanks - it seems I had the new line separator in the wrong spot - format now is much better , thanks all for the assistance.

2 bottles of beer on the wall
2 bottles of beer
Take one down.
Pass it around.
1 bottles of beer on the wall.

1 bottle of beer on the wall
1 bottle of beer
Take one down.
Pass it around.
No more bottles of beer on the wall
2 weeks ago

Campbell Ritchie wrote:No, don't tweak the code. Read it out. Copy it onto a sheet of paper. Count the variables with your fingers. Or any combination of the preceding, until you work out what I meant. Then you will be able to correct it is a few seconds, and you will have learnt lots about reading code.



Thanks ! With a bit of patience I managed to get it working as below - still a bit messy and still not sure why I have an extra line of " x bottles of beer on the wall" in the output that appears after the linebreak

How many bottles are on the wall:
12
Beer Song

-----------
12 bottles of beer on the wall
12 bottles of beer
Take one down.
Pass it around.

11 bottles of beer on the wall
11 bottles of beer on the wall
11 bottles of beer
Take one down.
Pass it around.

10 bottles of beer on the wall
10 bottles of beer on the wall
10 bottles of beer
Take one down.
Pass it around.



2 weeks ago

Campbell Ritchie wrote:You are reading the input correctly, but because you applied it to a different variable, the number used in the song is still 99.



Thanks for the reply - still gone a bit code blind and not seeing the errors of my ways, I will keep tweaking until its straight in my head!
2 weeks ago

Campbell Ritchie wrote:Welcome to the Ranch

Have you found the documentation for Scanner? There is an example just like what you want near the beginning of that documentation.



Thanks, I did some tweaks and by adding in scanner snippet and made some progress

It will ask for input as below but then still start off at 99

***********
How many bottles are on the wall:
5
Beer Song

-----------
99 bottles of beer on the wall
99 bottles of beer
Take one down.
etc
etc

********

2 weeks ago


just starting to work on this 99 beer bottle task. Whilst not the prettiest it runs and generally outputs what I want. How would I consider changing it to include a user input when the user can input what number to start at, instead of 99 after if I imported the scanner util? Thanks

2 weeks ago