In: Computer Science
I need know how to Write a java program called character length that prompts the user to enter a string and displays:
The length of the string is: xxxxx
The strings first character is: x
(where xxxxx is the length of the string and x is the first character.)
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new java program with name "CharacterLength,java" is created, which contains following code.
CharacterLength.java :
//import package
import java.util.*;
//Java class
public class CharacterLength {
//entry point of program, main method
public static void main(String[] args) {
//create object of scanner
class
Scanner sc=new
Scanner(System.in);
//asking user to enter string
System.out.print("Enter a string :
");
String s=sc.nextLine();//reading
string
//find and display length of
string
System.out.println("The length of
the string is:"+s.length());
//print first character
System.out.println("The strings
first character is:"+s.charAt(0));
}
}
======================================================
Output : Compile and Run CharacterLength.java to get the screen as shown below
Screen 1 :StringLength.java
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.