Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that keeps a count of the lines in the input and prints out the

ID: 3797447 • Letter: W

Question

Write a program that keeps a count of the lines in the input and prints out the line number, the number of characters in the line followed by the line itself.

Input -- The input will be one or more lines of characters that have been typed in by the user.

Output --The output will have for each line, the line number followed by a space, followed by the number of characters in the line, followed by a space, followed by the line that was entered

Sample Input

hello there!

I am a bee

Sample Output

1 12 hello there!

2 10 I am a bee

--HINT Have a variable (say named count) that you keep incrementing by 1 every time you read a new line. Initial value should be 1. Also, use the String's length method to get the length of the line.

Explanation / Answer

Please find below Java code according to your requirement.

Code:

import java.util.Scanner;

public class Demo {
   public static void main(String[] args) {
       for(;;){
   Scanner scanner= new Scanner(System.in); //For taking user input
   String str;
     
   System.out.println("Enter the string:");
   str=scanner.nextLine(); // To get next lines
   System.out.print(str.length());
   System.out.print(' ');
   System.out.println(str); // To display output
   str = null;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote