Java Arena
Step 3 of 5

Taking text apart

Input often packs several values into one line of text. A program cuts the line into parts, turns the parts that are numbers into numbers, and asks questions about the parts that are words.

The tools

Tool What it does Taught in
line.split(",") cuts a line at every comma, into an array of parts module 16, Working with strings
parts.length, parts[0] how many parts there are, and one part module 15, Arrays
Integer.valueOf(parts[1]) a part that holds a whole number, as an int module 3, Variables and types, and module 16
text.equals(other) the same text? Never compare text with == module 6, Conditions and logic
contains, startsWith, endsWith yes-or-no questions about text module 16, Working with strings
text.trim() the text without spaces at either end module 16, Working with strings

Words on a line

Chess players write down their games move by move. Each move is a short code, such as Nf3 (a knight moves to the square f3). An x in a move means it captures a piece, and a + at the end means it puts the other king in check. Here are the first moves of a game, one code for each player's move, separated by spaces:

String moves = "e4 e5 Nf3 Nc6 Bb5 a6 Bxc6 dxc6";
String[] parts = moves.split(" ");
int captures = 0;
for (String move : parts) {
    if (move.contains("x")) {
        captures++;
    }
}
System.out.println(parts.length + " moves, " + captures + " captures");
System.out.println("Last move: " + parts[parts.length - 1]);
Output
8 moves, 2 captures
Last move: dxc6
  • split(" ") gives an array of Strings, one per move, and a for-each loop goes through them.
  • contains("x") asks whether the letter appears anywhere in the move. It counts capital letters apart from small ones: contains("X") would find nothing here.
  • The last part is at the index parts.length - 1.

A line of comma-separated values

A climbing gym's member list has one line per member: the name, the age and the number of visits this month, separated by commas. Someone typed a space after each comma. The gym calls a member with 8 visits or more in a month a regular.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] parts = scanner.nextLine().split(",");
        String name = parts[0].trim();
        int age = Integer.valueOf(parts[1].trim());
        int visits = Integer.valueOf(parts[2].trim());
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Visits: " + visits);
        System.out.println("Regular: " + (visits >= 8));
    }
}
Input
Mira Koski, 34, 12
Output
Name: Mira Koski
Age: 34
Visits: 12
Regular: true
  • The parts are Strings, even when they hold digits. Integer.valueOf turns them into ints. Without it, parts[2].trim() + 1 would join the texts and give 121.
  • split(",") cuts only at the commas, so the second part is " 34", with the space in front. Integer.valueOf(" 34") throws a NumberFormatException, and trim() removes the space first (module 16).
  • A line with fewer than three parts would crash at parts[2] with an ArrayIndexOutOfBoundsException. When lines can be broken, check parts.length first and skip the line (module 29).

Your task

Challenge 1 of 3

The program reads one line of chess moves, separated by spaces, and prints how many moves there are, how many of them capture a piece (they contain an x), and how many give check (they end with +). Fill in the blanks. For the input shown, the program prints:

Moves: 12
Captures: 3
Checks: 1

Challenge 2

A woodworking student writes down each piece to cut: its name, its length and its width in centimeters. What does this program print?

Challenge 3

At a chess tournament, the games are written down one per line: the player with the white pieces, the player with the black pieces, and the result, separated by commas, like Mira,Jonas,1-0. The result 1-0 means that white won, 0-1 that black won, and 1/2-1/2 is a draw. A win is worth 1 point, a draw half a point, and a loss nothing.

The first line of input is a player's name. Then come games, until an empty line. Print how many games that player played, and their points, which are a double. Skip the games the player wasn't in. A name must match in full: Ada isn't in a game of Adam. For the input shown, the program prints:

Mira played 4 games
Points: 2.5