DEV Community

Cover image for Case Studies on Strings and Characters
Paul Ngugi
Paul Ngugi

Posted on

Case Studies on Strings and Characters

Strings are fundamental in programming. The ability to write programs using strings is essential in learning Java programming.
You will frequently use strings to write useful programs. This section presents three examples of solving problems using strings.

Case Study: Guessing Birthdays

You can find out the date of the month when your friend was born by asking five questions. Each question asks whether the day is in one of the five sets of numbers.

Image description

The birthday is the sum of the first numbers in the sets where the day appears. For example, if the birthday is 19, it appears in Set1, Set2, and Set5. The first numbers in these three sets are 1, 2, and 16. Their sum is 19.

package demo;
import java.util.Scanner;

public class GuessBirthday {

    public static void main(String[] args) {
        String set1 = " 1  3  5  7\n" + " 9 11 13 15\n" + "17 19 21 23\n" + "25 27 29 31";
        String set2 = " 2  3  6  7\n" + "10 11 14 15\n" + "18 19 22 23\n" + "26 27 30 31";
        String set3 = " 4  5  6  7\n" + "12 13 14 15\n" + "20 21 22 23\n" + "28 29 30 31";
        String set4 = " 8  9 10 11\n" + "12 13 14 15\n" + "24 25 26 27\n" + "28 29 30 31";
        String set5 = "16 17 18 19\n" + "20 21 22 23\n" + "24 25 26 27\n" + "28 29 30 31";

        int day = 0;

        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to answer questions
        System.out.print("Is your birthday in Set1?\n");
        System.out.print(set1);
        System.out.print("\nEnter 0 for No and 1 for Yes: ");
        int answer = input.nextInt();

        if(answer == 1)
            day += 1;

        // Prompt the user to answer questions
        System.out.print("\nIs your birthday in Set2?\n");
        System.out.print(set2);
        System.out.print("\nEnter 0 for No and 1 for Yes: ");
        answer = input.nextInt();

        if(answer == 1)
            day += 2;

        // Prompt the user to answer questions
        System.out.print("\nIs your birthday in Set3?\n");
        System.out.print(set3);
        System.out.print("\nEnter 0 for No and 1 for Yes: ");
        answer = input.nextInt();

        if(answer == 1)
            day += 4;

        // Prompt the user to answer questions
        System.out.print("\nIs your birthday in Set4?\n");
        System.out.print(set4);
        System.out.print("\nEnter 0 for No and 1 for Yes: ");
        answer = input.nextInt();

        if(answer == 1)
            day += 8;

        // Prompt the user to answer questions
        System.out.print("\nIs your birthday in Set5?\n");
        System.out.print(set5);
        System.out.print("\nEnter 0 for No and 1 for Yes: ");
        answer = input.nextInt();

        if(answer == 1)
            day += 16;

        System.out.println("\nYour birthday is " + day + "!");
    }

}

Enter fullscreen mode Exit fullscreen mode

This game is easy to program. You may wonder how the game was created. The mathematics behind the game is actually quite simple. The numbers are not grouped together by accident—the way they are placed in the five sets is deliberate. The starting numbers in the five sets are 1, 2, 4, 8, and 16, which correspond to 1, 10, 100, 1000, and 10000 in binary. A binary number for decimal integers between 1 and 31 has at most five digits. For example, number 19 is binary 10011, so it appears in Set1, Set2, and Set5. It is binary 1 + 10 + 10000 = 10011 or decimal 1 + 2 + 16 = 19. Number 31 is binary 11111, so it appears in Set1, Set2, Set3, Set4, and Set5. It is binary 1 + 10 + 100 + 1000 + 10000 = 11111 or decimal 1 + 2 + 4 + 8 + 16 = 31.

Image description

Case Study: Converting a Hexadecimal Digit to a Decimal Value

The hexadecimal number system has 16 digits: 0–9, A–F. The letters A, B, C, D, E, and F correspond to the decimal numbers 10, 11, 12, 13, 14, and 15. We now write a program that prompts the user to enter a hex digit and display its corresponding decimal value.

Image description

The program reads a string from the console (line 9) and checks if the string contains a single character (line 12). If not, report an error and exit the program (line 14). The program invokes the hexString.charAt(0) method to obtain the character ch (line 18). If ch is between 'A' and 'F' (line 19), the corresponding decimal value is ch – 'A' + 10 (line 20). Note that ch – 'A' is 0 if ch is 'A', ch – 'A' is 1 if ch is 'B', and so on. When two characters perform a numerical operation, the characters’ Unicodes are used in the computation.
The program invokes the Character.isDigit(ch) method to check if ch is between '0' and '9' (line 23). If so, the corresponding decimal digit is the same as ch (lines 24).
If ch is not between 'A' and 'F' nor a digit character, the program displays an error message (line 27).

Case Study: Revising the Lottery Program Using Strings

The lottery program below generates a random two-digit string, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule:

  • If the user input matches the lottery string in the exact order, the award is $10,000.
  • If all the digits in the user input match all the digits in the lottery string, the award is $3,000.
  • If one digit in the user input matches a digit in the lottery string, the award is $1,000.

Image description

The program generates two random digits and concatenates them into the string lottery (lines 8). After this, lottery contains two random digits.
The program prompts the user to enter a guess as a two-digit string (line 32) and checks the guess against the lottery string in this order:

  • First check whether the guess matches the lottery exactly (line 26).
  • If not, check whether the reversal of the guess matches the lottery (line 28).
  • If not, check whether one digit is in the lottery (lines 30).
  • If not, nothing matches and display “Sorry, no match” (line 33).

Top comments (0)