LoslegenKostenlos loslegen

How long is a String?

String is a collection of characters and oftentimes, you'll need to find its length. Instead of counting though, there is the .length() method!

Diese Übung ist Teil des Kurses

Introduction to Java

Kurs anzeigen

Anleitung zur Übung

  • Use the .length() method to find out the length of the bookName variable, saving the result as bookNameLength.
  • Find the length of the author's name, "Terry Pratchett", and save it using the correct primitive type.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

class HowLong {
    public static void main(String[] args) {
    
        String bookName = "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch";
        
        // Find the length of bookName
        int bookNameLength = bookName.____();
        
        // Find the length of the String and save it
        ____ authorNameLength = "Terry Pratchett".____;
        
        System.out.println("The length of the book title is " + bookNameLength);
        System.out.println("The length of the author's name is " + authorNameLength);
    }
}
Code bearbeiten und ausführen