I've created a Java program in Eclipse. The program counts the frequency of each word. For example if the user entered 'I went to the shop' the program would produce the output '1 1 1 2' that is 1 word of length 1 ('I') 1 word of length 2 ('to') 1 word of length 3 ('the') and 2 words of length 4 ('went' , 'shop').
These are the results I'm getting. I don't want the output with a 0 to be shown. How can I hide these and only have the results with 1,2,3,4,5 shown.
The cat sat on the mat
words[1]=0
words[2]=1
words[3]=5
words[4]=0
words[5]=0
import java.util.Scanner;
import java.io.*;
public class mallinson_Liam_8
{
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(new File("body.txt"));
while(scan.hasNext())
{
String s;
s = scan.nextLine();
String input = s;
String strippedInput = input.replaceAll("\\W", " ");
System.out.println("" + strippedInput);
String[] strings = strippedInput.split(" ");
int[] counts = new int[6];
int total = 0;
String text = null;
for (String str : strings)
if (str.length() < counts.length)
counts[str.length()] += 1;
for (String s1 : strings)
total += s1.length();
for (int i = 1; i < counts.length; i++){
System.out.println("words["+ i + "]="+counts[i]);
StringBuilder sb = new StringBuilder(i).append(i + " letter words: ");
for (int j = 1; j <= counts[i]; j++) {
}}}}}