Complete the code to counts the number of lines input by a user until the user e
ID: 3549792 • Letter: C
Question
Complete the code to counts the number of lines input by a user until the user enters the string ENDOFDATA (must be uppercase letters, no spaces) on a line by itself. Make sure you display the number of lines found. Use the nextLine() method of Scanner class to read entire lines of data. (Hint, this is like counting the number of integers entered, but reading Strings and using String comparison to check for equality).
String SENTINEL = "ENDOFDATA";
Scanner keyboard = new Scanner(System.in);
System.out.println( "Enter lines of data or " + SENTINEL + " to quit" );
Explanation / Answer
package com.chegg;
import java.util.Scanner;
public class Counting {
public static void main(String[] args){
final String SENTINEL = "ENDOFDATA";
Scanner keyboard = new Scanner(System.in);
System.out.println( "Enter lines of data or " + SENTINEL + " to quit" );
int count=0;
String line;
do{
System.out.print("Enter a line");
line = keyboard.nextLine();
count++;
System.out.println("You entered:: "+line);
}
while(!line.equals(SENTINEL));
keyboard.close();
System.out.println("Total no of lines entered by you::"+count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.