logo Practice-It logo


Language/Type: Java file processing Scanner
Author: Marty Stepp

Write a method named blackjack that accepts as its parameter a Scanner for an input file containing a hand of playing cards, and returns the point value of the hand in the card game Blackjack.

A card has a rank and a suit. There are 13 ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. There are 4 suits: Clubs, Diamonds, Hearts, and Spades. A Blackjack hand's point value is the sum of its cards' point values. A card's point value comes from its rank; the suit is irrelevant. In this problem, cards are worth the following points:

Rank Point Value
2-10 The card's rank (for example, a 7 is worth 7 points)
Jack (J), Queen (Q), King (K) 10 points each
Ace (A) 11 points (for this problem; simplified compared to real Blackjack)

The input file contains a single hand of cards, each represented by a pair of " " tokens. For example:

5 Diamonds
Q Spades
2 Spades  3 Hearts

Given the above input, your method should return 20, since the cards' point values are 5 + 10 + 2 + 3 = 20.

The input can be in mixed casing, have odd spacing between tokens, and can be split across lines. For example:

2 Hearts
  j SPADES   a  Diamonds
2 ClUbS

Given the above input, your method should return 36, since the cards' point values are 2 + 10 + 11 + 2 + 11 = 36.

You may assume that the Scanner contains at least 1 card (two tokens) of input, and that no line will contain any tokens other than valid card data. The real game of Blackjack has many other rules that you should ignore for this problem, such as the notion of going "bust" once you exceed a score of 21.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.