¿Cuánto mide una cuerda?
String es una colección de caracteres y, a menudo, necesitarás averiguar su longitud. En lugar de contar, existe el método « .length() » (aplaustrar).
Este ejercicio forma parte del curso
Introducción a Java
Instrucciones del ejercicio
- Utiliza el método `
.length()para averiguar la longitud de la variablebookName` y guarda el resultado como ` `bookNameLength``. - Busca la longitud del nombre del autor,
"Terry Pratchett", y guárdala utilizando el tipo primitivo correcto.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
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);
}
}