I need to make a Java program called TextFormatter and TextFormatterDriver. The
ID: 3762003 • Letter: I
Question
I need to make a Java program called TextFormatter and TextFormatterDriver.
The TextFormatter class has 3 methods: findString, countStrings, convertItalics.
findString: takes a string length to find in the string the location of an element. It requires the element to be a single instance of itself. So 2 same elements together will not count. The method should return the position of the element as an integer. If the element does not exist within the position called, it should return -1. counting the elements starts at 0. Ex) "aabaccb"
findString("a", 0); result = 3;
findString("b", 4); result = 6
findString("c", 0); result =-1;
countStrings takes a string length to find in the line. It will return the number of times instances of the String appear.
convertItalics: If the String has an even amount of single "_". A line with those underscores conveted to alternating <I> and </I> tages should be returned. The first "_" should be converted to <I>, the second to </I>. If the line does not have an even amount of "_". The original line should be returned.
Ex.) "This is _very_ good.; "This is <I>very</I> good.
Ex.) "_This_ is very _good_."; <I>This</I> is very <I>good</I>.
Ex.) "This is very _good."; This is very _good.
Below is a starting skeleton of the lab.
public class TextFormatter
{
private String line; // The line to format
public TextFormatter (String line)
{
this.line = line;
}
public int findString (String str, int start)
{
/*To be implemented in part a */
}
public int countStrings (String str)
{
/*To be implemented in part b */
}
public String convertItalics ()
{
/*To be implemented in part c */
}
}
public class TextFormatterRunner {
public static void main(String[] args) {
TextFormatter line = new TextFormatter("aabaccb");
System.out.println(line.findString("a", 0));
}
}
Explanation / Answer
public class TextFormatter {
private String line; // The line to format
public TextFormatter(String line) {
this.line = line;
}
public int findString(String str, int start) {
/* To be implemented in part a */
int index = -1;
for (int i = 0; i < line.length()-str.length()+1; i++) {
if (i>=start && line.substring(i, i+str.length()).equals(str)) {
index=i;
}
}
return index;
}
public int countStrings(String str) {
/* To be implemented in part b */
int count=0;
for (int i = 0; i < line.length()-str.length()+1; i++) {
if (line.substring(i, i+str.length()).equals(str)) {
count++;
}
}
return count;
}
public String convertItalics() {
/* To be implemented in part c */
int count = 0;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '_') {
count++;
}
}
if (count%2==0) {
StringBuilder temp = new StringBuilder();
int flag = 1;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '_' && flag%2 != 0) {
temp.append("<l>");
flag++;
}
else if (line.charAt(i) == '_' && flag%2 == 0) {
temp.append("</l>");
flag++;
}
else {
temp.append(line.charAt(i));
}
}
return temp.toString();
}
return line;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
public class TextFormatterRunner {
public static void main(String[] args) {
TextFormatter line = new TextFormatter("This is _very_ good.;");
System.out.println(line.findString("a", 0));
System.out.println(line.countStrings("ood"));
System.out.println(line.convertItalics());
}
}