Question
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.
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started