• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keyboard response from user  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a "very" basic blackjack game, and I'm stuck on how to get a user response to hit and stick game play.

I know people will want me to tidy it up, or do it "their" way, but honestly I just need method of getting the user to press h or s and have the program continue under it's own steam.

Thanks for the help.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We prefer you do it the right way. Yes, it does need tidying up. The worst part is the repetition in lines 17-29. The next worst part so the length of the method.

The compile also prefers you do it the right way. You will never persuade it to compile if (h), never mind whether it is followed by an assignment or not. The only thing that can go in the () after if is a boolean expression (or Boolean).
You will never get Scanner#nextLine to work after nextAnythingElse like that. Try Scanner#next instead. There doesn't appear to be a Scanner#nextChar method but you can use next and then charAt to get a particular char from the input.

I think you need to refresh your memory about choice statements: have a look in the Java® Tutorials.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for the reply, not a lot of information to go on there but I take your point.

I removed the if and if else statements and the game continued to the next part of the code. I was hoping there would be a way for the program to only accept H or S as keyboard inputs, but that it seems is still far beyond my range, I am a first year student and find using arrays at this point to be too confusing to attempt just yet.

My instructor told me it would be "ok" to use many int variables instead of the array function as it was basically the same thing. But if you don't mind a follow up question, how would I (you) tidy up lines 17-29?

Thanks again.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can reduce it to about 4 lines with an array. You can reduce it to 1 line with a Stream but that only works in Java8 and I am not sure how to restrict the values in the Stream to < 20. There is an example for longs here. For ints you could try

myRandom.ints(0, 20).limit(20L).toArray()

The 20L bit is because that method takes a long as its parameter and it will give you a 20‑element array pseudo‑random between 0 and 19 inclusive. At least I think it will; I have never tried. I shall let you try it.

This code will only work in Java8.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found another overloading of the ints method. Try
myArray = myRandom.ints(20L, 0, 20).toArray();
Again 20‑element array.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read "hit" and "stick" as “tokens” with your Scanner. Then you can find their first letter with a method of the String class. Then you can use that letter to control your choices.

[edit]Change stand to stick.[/edit]
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will definitely try that when I get J8 installed. But I am limited to the functions that were covered in the course in college, and further limited by my comprehension of those functions, these are my first baby steps in to java and although I get the "jist" of what your saying, and I realize how valuable the information will be to me in the future. For right now I need to keep it as simple as possible until I get a working product, and then I can start trying to employ arrays and the long function you mentioned.

I'm honestly not sure if I'm even allowed to use J8 for my project but you've been a great help to me, and don't think it goes unappreciated !!

Thanks Vincent
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create your int[] in Java1.0. You could also write a for loop which allows you to put a “random” number in each of its elements in Java1.0.
You are right that you might not be allowed to use Java8‑specific features, but you will see how much neater it is, and easier to read.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Tyson wrote: . . . you've been a great help to me, . . .
Thanks Vincent
What a nice thing to say
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can read "hit" and "stick" as “tokens” with your Scanner. Then you can find their first letter with a method of the String class. Then you can use that letter to control your choices.

[edit]Change stand to stick.[/edit]


I've attempted to try this, and it is so increasingly frustrating to a (newb) like me, just when I think I understand the logic, I'm further away than I thought I was. I''m not going to tell you about the headache I have right now, just so far as to mention a solution would go a long way to relieving it.

I know it's barmy, but could you please pop it in an emulator and see if you can do something with it.

Thanks.



I will try to learn as much as I can about arrays and the more complicated method of dealing cards, tomorrow maybe... it's getting late.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the == operator on Strings or other reference types.

You are reading "hit" from the keyboard and the asking whether a is the same as "h". You need to check equality to "hit". Try nextLine().toLowerCase() instead so you can write HiT or hIt and it will still work.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use the == operator on Strings or other reference types.

You are reading "hit" from the keyboard and the asking whether a is the same as "h". You need to check equality to "hit". Try nextLine().toLowerCase() instead so you can write HiT or hIt and it will still work.


Oh, I was trying something like .ignoreUpperCase but it wouldn't work for me. Then I watched a video on it, and seen the == expression is like a question, is this ==(the exact same as) this, and if it's not it will return negitive. So instead I tried



and that seemed to clear up one problem, now I just have to get this segment into a loop with a rule to bust at 22++ , I'm open to ideas

Thanks for the help, I know it ain't easy watching me flap around like a beached fishy, but without this thread I'd of been dead in the water anyway.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the switch statement? Did you know that you can use String literals for the cases? At least you can in Java7+.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen switch statements on other peoples published blackjack codes, but that is far beyond me still. I really need the most simple execution possible for the moment.

I've had a pop at while loops


can you give me a short example of a switch statement I can try to implement in my code, when I read the other posted codes, I see a lot of case() lines and I'm not certain what it all means.
I know you like to encourage people to do their own research, but in this case, java may as well be in German for all the good it's doing me, I need working examples to understand the logic before I can write my own.

Thanks again for the support here,
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® Tutorial section about switch. Note you need break at strategic intervals otherwise the whole switch will go horribly wrong.

Where are you setting playing to false? If you don't, the loop will keep going until something else goes wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!