Need help writing these methods in Java any help would be gr
Need help writing these methods in Java, any help would be greatly appreciated :)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.BadLocationException;
public class textEditor implements ActionListener, DocumentListener {
JTextArea txtInput;
JTextField txtFind;
JLabel lblWordCount, lblCharacterCountAll, lblCharacterCountNWS;
// FIX ME: Write the method definition for a getNumOfNonWSCharacters method
// that has a String parameter, and returns the number of non white-space
// characters in the string
// FIX ME: Implement a findText method that has two String parameters. The
// method returns the number of times the first string is found in the
// second string.
// FIX ME: Write the method definition for a method called getNumOfWords that
// has a single String parameter, and returns the number of words in the
// String.
// FIX ME: Implement a getIndices function that has two String parameters and
// an integer parameter for the size of the area that will be created. The method
// creates an integer array of the appropriate size, which will store
// the indices corresponding to all occurrences of the first string in the
// second string.
// FIX ME: Write the method definition for a singleSpace method that
// has a single String parameter, and replaces each occurence of \"\ \ \" with
// \"\ \", and returns the updated string. In other words, the method
// takes in text and converts each double spaced block of text
// to a single-spaced version.
// FIX ME: Write the method definition for a doubleSpace method that
// has a single String parameter, and replaces each occurence of \"\ \" with
// \"\ \ \", and returns the updated string. In other words, the method
// takes in text and converts each line break to a double spaced format
Solution
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.BadLocationException;
public class textEditor implements ActionListener, DocumentListener {
JTextArea txtInput;
JTextField txtFind;
JLabel lblWordCount, lblCharacterCountAll, lblCharacterCountNWS;
public int getNumOfNonWSCharacters(String s) {
return(s.replaceAll(\"\\\\s+\", \"\").length());
}
public int findText(String first, String second) {
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = second.indexOf(first,lastIndex);
if(lastIndex != -1){
count ++;
lastIndex += first.length();
}
}
return(count);
}
public int getNumOfWords(String s) {
String[] words = s.split(\"[^a-zA-Z]+\");
return (words.length);
}
public int[] getIndices(String first, String second, int size) {
int[] indices = new int[size];
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = second.indexOf(first,lastIndex);
indices[count] = lastIndex;
if(lastIndex != -1){
count ++;
lastIndex += first.length();
}
}
return(indices);
}
// FIX ME: Write the method definition for a singleSpace method that
// has a single String parameter, and replaces each occurence of \"\ \ \" with
// \"\ \", and returns the updated string. In other words, the method
// takes in text and converts each double spaced block of text
// to a single-spaced version.
// FIX ME: Write the method definition for a doubleSpace method that
// has a single String parameter, and replaces each occurence of \"\ \" with
// \"\ \ \", and returns the updated string. In other words, the method
// takes in text and converts each line break to a double spaced format

