BaşlayınÜcretsiz başlayın

Character matching

Your sales data sometimes contains typos or formatting errors that could break your analysis. Before calculating total revenue, you need to verify that all sales quantities contain only valid numeric characters and decimal points. You've extracted some sales quantities (in millions of units) from the video game dataset.

Bu egzersiz, kursun bir parçasıdır

Cleaning Data in Java

Kursa Göz Atın

Egzersiz talimatları

  • Check if quantity contains any digits (0 to 9).
  • Look for the decimal separator in quantity.
  • Detect any alphabetic characters in quantity (lowercase or uppercase).

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

public class PatternValidation {
    public static void main(String[] args) {
        List quantities = Arrays.asList("41.49", "29.08", "15.85", "12.5x");

        for (String quantity : quantities) {
            // Check if quantity contains any digits
            boolean hasDigits = ____.____('0', '9').matchesAnyOf(quantity);
            // Look for decimal separator in quantity
            boolean hasDecimal = CharMatcher.is('.').____(quantity);
            // Detect any alphabetic characters in quantity
            boolean hasInvalidChars = CharMatcher.inRange('____', '____')
                    .or(CharMatcher.inRange('____', '____')).matchesAnyOf(quantity);

            boolean isValid = hasDigits && hasDecimal && !hasInvalidChars;
            System.out.println(quantity + " has valid characters: " + isValid);
        }
    }
}
Kodu Düzenle ve Çalıştır