Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

while trying to run code below I get the cmd line errors what do I need to change to have the program run C:UsersJoeDesktop>java -jar

while trying to run code below I get the cmd line errors what do I need to change to have the program run

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

COP3330 Sentence Utility Program by Joe Doe

Input file name: cats.txt Exception in thread "main" java.lang.NegativeArraySizeException: -1 at SentenceUtils.generateShingles(SentenceUtils.java:20) at SentenceUtils.(SentenceUtils.java:11) at SentenceUtilsTest.main(SentenceUtilsTest.java:30)

Code below it's in two classes

class 1

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 Joe Doe"); 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(); }

}

}

class 2

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() { 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(); }

}

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

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions