Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When the following code is ran, it counts all of the prepositions together? i ne

ID: 3628548 • Letter: W

Question

When the following code is ran, it counts all of the prepositions together? i need it to count each preposition (at, about, in, of, until) individually. thanks.


package simplewordprocessor;

/**
*
*
*/
import java.util.*;
import java.io.*;


public class SimpleWordProcessor {


public static void main(String[] args)throws FileNotFoundException
{String input,word="";
int totalcharacters=0,words=0,totalwordcharacters=0;
int i,j,n,spaces=0,prep=0,the=0;

char ch;

//open files
PrintStream output=new PrintStream(new File("modified_State_Of_The_Union_2011"
+ "_Address.txt"));
{ Scanner finput=new Scanner(new File("State_Of_The_Union_2011_Address"
+ ".txt"));
if(!finput.hasNext()) //anything in the file?
{System.out.println("file empty-program aborting");
System.exit(1);

}

//keep reading while more data in the file
while(finput.hasNextLine())
{ //read a line
input=finput.nextLine();
n=input.length(); //get number of characters in line

word=""; //start word as empty
for(i=0;i<=n;i++) //for each character in the line
{if(i!=n) //if not done
ch=input.charAt(i); //get the character
else
ch=' '; //if are done set ch to blank
if(Character.isWhitespace(ch)) //if the character is end of word
{words++; //count the word
spaces++; //and spaces



totalwordcharacters+=word.length(); //add the characters

//if it's one of the "special words"
if(word.compareToIgnoreCase("at")==0||
word.compareToIgnoreCase("about")==0||
word.compareToIgnoreCase("in")==0||
word.compareToIgnoreCase("of")==0||
word.compareToIgnoreCase("until")==0)
prep++; //count the preposition
if(word.compareTo("the")==0)
{the++; //or the
word="a"; //if the chage to a
}
else if(word.compareTo("The")==0) //with appropriate case
{the++;
word="A";
}
output.print(word+" "); //output the word followed by a space
word="";
}
else
word+=ch; //if not end of word, "build" the word
}
output.println();
}
//output
System.out.println("total number of words: "+(words-1));
System.out.println("total number of prepositions: "+prep);
System.out.println("total number of the or The: "+the);
System.out.println("total number of characters (spaces included): "+
(totalwordcharacters+spaces));
output.close();
finput.close();
System.exit(0);
}
}
}

Explanation / Answer

please rate - thanks

/**
*
*
*/
import java.util.*;
import java.io.*;


public class SimpleWordProcessor {


public static void main(String[] args)throws FileNotFoundException
{String input,word="";
int totalcharacters=0,words=0,totalwordcharacters=0;
int i,j,n,spaces=0,at=0,about=0,in=0,of=0,until=0,the=0;

char ch;

//open files
PrintStream output=new PrintStream(new File("modified_State_Of_The_Union_2011"
+ "_Address.txt"));
{ Scanner finput=new Scanner(new File("State_Of_The_Union_2011_Address"
+ ".txt"));
if(!finput.hasNext()) //anything in the file?
{System.out.println("file empty-program aborting");
System.exit(1);

}

//keep reading while more data in the file
while(finput.hasNextLine())
{ //read a line
input=finput.nextLine();
n=input.length(); //get number of characters in line

word=""; //start word as empty
for(i=0;i<=n;i++) //for each character in the line
{if(i!=n) //if not done
ch=input.charAt(i); //get the character
else
ch=' '; //if are done set ch to blank
if(Character.isWhitespace(ch)) //if the character is end of word
{words++; //count the word
spaces++; //and spaces



totalwordcharacters+=word.length(); //add the characters

//if it's one of the "special words"
if(word.compareToIgnoreCase("at")==0)
        at++;
else if(word.compareToIgnoreCase("about")==0)
        about++;
else if(word.compareToIgnoreCase("in")==0)
        in++;
else if(word.compareToIgnoreCase("of")==0)
        of++;
else if(word.compareToIgnoreCase("until")==0)
        until++; //count the preposition
else if(word.compareTo("the")==0)
{the++; //or the
word="a"; //if the chage to a
}
else if(word.compareTo("The")==0) //with appropriate case
{the++;
word="A";
}
output.print(word+" "); //output the word followed by a space
word="";
}
else
word+=ch; //if not end of word, "build" the word
}
output.println();
}
//output
System.out.println("total number of words: "+(words-1));
System.out.println("total number of the preposition at: "+at);
System.out.println("total number of the preposition about: "+about);
System.out.println("total number of the preposition in: "+in);
System.out.println("total number of the preposition of: "+of);
System.out.println("total number of the preposition until: "+until);
System.out.println("total number of the or The: "+the);
System.out.println("total number of characters (spaces included): "+
(totalwordcharacters+spaces));
output.close();
finput.close();
System.exit(0);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote