Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am very close with this program I just need it to not output the blank line at the end of the text file that

I am very close with this program I just need it to not output the blank line at the end of the text file that I have current output is

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

C:\Users\Jack\Desktop>java -jar SentenceUtility.jar cats.txt ---------------------------------------------------------

COP3330 Sentence Utility Program by Jack Black

Input file name: cats.txt Number of sentences: 4

Sentence 0 > The cat in the hat

Tokens: 0:The 1:cat 2:in 3:the 4:hat

Shingles: 'Th' 'he' 'e ' ' c' 'ca' 'at' 't ' ' i' 'in' 'n ' ' t' 'th' 'he' 'e ' ' h' 'ha' 'at'

Sentence 1 > The cat sat on the mat

Tokens: 0:The 1:cat 2:sat 3:on 4:the 5:mat

Shingles: 'Th' 'he' 'e ' ' c' 'ca' 'at' 't ' ' s' 'sa' 'at' 't ' ' o' 'on' 'n ' ' t' 'th' 'he' 'e ' ' m' 'ma' 'at'

Sentence 2 > Pigs in a blanket

Tokens: 0:Pigs 1:in 2:a 3:blanket

Shingles: 'Pi' 'ig' 'gs' 's ' ' i' 'in' 'n ' ' a' 'a ' ' b' 'bl' 'la' 'an' 'nk' 'ke' 'et'

Sentence 3 >

Tokens: 0:

Shingles: Exception in thread "main" java.lang.NullPointerException at SentenceUtils.report(SentenceUtils.java:42) at SentenceUtilsTest.main(SentenceUtilsTest.java:45)

the code for the classes are as followed

===============================================================================================================================================

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner;

public class SentenceUtilsTest {

private static List slist = new ArrayList();

public static void main(String[]args) { if (args.length == 0) { System.out.println("provide input filename as cmd line argument"); System.exit(1); } System.out.println("--------------------------------------------------------- "); System.out.println("COP3330 Sentence Utility Program by Jack Black"); System.out.println(" Input file name: " + args[0]); int sentenceCount = 0; try { File file = new File(args[0]); Scanner scanner = new Scanner(file);

while(scanner.hasNextLine()) { String sentence = scanner.nextLine(); slist.add(new SentenceUtils(sentence)); sentenceCount++; } scanner.close(); } catch(FileNotFoundException e) { e.printStackTrace(); }

System.out.println("Number of sentences: " + sentenceCount); System.out.println(); for(int i = 0; i < sentenceCount; i++) { System.out.println("Sentence " + i + " >"); slist.get(i).report(); System.out.println(); }

}

}

==============================================================================================================================================

public class SentenceUtils {

private String sentence; private String[] tokens; private String[] shingles;

public SentenceUtils(String s) { sentence = s; generateTokens(); generateShingles(); }

private void generateTokens() { tokens = sentence.split(" ");

}

private void generateShingles() { if(sentence==null || sentence.trim().length()<=0) return; shingles = new String[sentence.length()-1]; int index = 0; for(int i = 0; i < sentence.length()-1; i++) { shingles[index++] = sentence.substring(i, i+2); } }

public void report() { System.out.println(sentence); System.out.println(); System.out.println("Tokens:"); for(int i = 0; i < tokens.length; i++) { System.out.println(i + ":" + tokens[i]); }

System.out.println(); System.out.println("Shingles:"); for(int i = 0; i < shingles.length; i++) { System.out.print("'" + shingles[i] + "' "); } System.out.println(); }

}

I need the program to recognize that the last line is a blank line and that there is no sentence their so the part that says

Sentence 3 >

Tokens: 0:

Shingles:

dosn't show up

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Identify four applications of HRM to healthcare organizations.

Answered: 1 week ago