Complete the following program by writing method ProcessLines that reads any num
ID: 3834423 • Letter: C
Question
Complete the following program by writing method ProcessLines that reads any number of strings (lines) from the keyboard and counts short and long lines. A string under 15 characters long is considered a short line, otherwise it is considered a long line. Define your scanner inside the method. Display the output as follows:
Number of short strings: 13
Number of long strings: 28
import java.util.*;
public class ProcessInputLines {
public static void main(String[] args) {
ProcessLines();
}
public static void ProcessLines() {
int shortLinesCount = 0;
int longLinesCount = 0;
...
System.out.println();
System.out.println("Number of short lines = " + shortLinesCount);
System.out.println("Number of long lines = " + longLinesCount);
}
}
Explanation / Answer
import java.util.Scanner;
public class ProcessInputLines {
public static void ProcessLines(){
Scanner keyboard = new Scanner(System.in);
int shortLinesCount = 0;
int longLinesCount = 0;
String line;
while(true){
System.out.println("Enter line(exit to stop): ");
line = keyboard.nextLine();
if(line.trim().equalsIgnoreCase("exit"))
break;
if(line.length() < 15)
shortLinesCount++;
else
longLinesCount++;
}
System.out.println();
System.out.println("Number of short lines = " + shortLinesCount);
System.out.println("Number of long lines = " + longLinesCount);
keyboard.close();
}
public static void main(String[] args) {
ProcessLines();
}
}
/*
Sample run:
Enter line(exit to stop):
pravesh
Enter line(exit to stop):
this is the line th t
Enter line(exit to stop):
go and do goan tha t can rewbrew
Enter line(exit to stop):
f wkf w
Enter line(exit to stop):
dkfjnelfnek.fenl.enwf
Enter line(exit to stop):
exit
Number of short lines = 2
Number of long lines = 3
*/