Java String read from console and check its length

Question

We would like to write a program that prompts the user to enter a string.

Display its length and its first character.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String s = input.nextLine();/*w w w . ja  v a  2s. c om*/
    
    //your code here    
  }
}




import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String s = input.nextLine();
    
    System.out.println("The length of the string is " + s.length());
    System.out.println("The first character in the string is " + s.charAt(0));    
  }
}



PreviousNext

Related