Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exception in thread main java.lang.arrayindexoutofboundsexception: 0 on line 47, whhhhhhhhy / public class Problem2 { public static boolean isSameWord(String str) { MyStack stack =

Exception in thread main java.lang.arrayindexoutofboundsexception: 0 on line 47, whhhhhhhhy \ /

public class Problem2 {

public static boolean isSameWord(String str) { MyStack stack = new MyStack(); int top = -1; //using MyStack for (int i = 0; i < str.length(); i++) { stack.push(str.charAt(i)); top++; } //reversing to check is its equal String equals = ""; try { while (!stack.isEmpty()) { equals += stack.pop(); } } catch (NoSuchElementException e) { }

return equals.equals(str); }

public static void main(String[] args){ AvlTree wordTree = new AvlTree<>(); //command line argument if (args.length == 0) { System.out.println("Please provide filename as an argument:"); return; } String filename = args[0]; try { //reading in a file BufferedReader br = new BufferedReader(new FileReader(filename)); String line; int WordCount = 0; while ((line = br.readLine()) != null) { //taking away all the punctuation and spaces and making the line all lowercase String stripped = line.replace(". ", "").replace(", ", "").toLowerCase(); if (isSameWord(stripped)) { //declaring we found a match stripped = stripped.replaceFirst("^[^a-zA-Z']+", "").replaceAll("[^a-zA-Z']+$", "").toLowerCase(); wordTree.insert(stripped, WordCount); WordCount++; } wordTree.printTree(); } //if the input argument has no duplicate words at all if (WordCount == 0) { System.out.println("There are no duplicate words."); } br.close(); } catch (FileNotFoundException e) { System.out.println(filename + " does not exist."); } catch (IOException exception) { exception.printStackTrace(); } } }

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

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

7. Where Do We Begin?

Answered: 1 week ago