I need help on a code where i get my input from a text document(i cant remember what the input is called) and from there i must get the first initials of each word.For example "John The Kid" output should be JTK. I think my main problem im having is the for loop and or maybe the code for actually getting the initials of each word.
I can only use charAt indexOf and substring for the code.As much explanation would help i really would like to understand what i need to help and why i am doing it.Thanks
This is my object file.
Runner(main class file)import static java.lang.System.*; public class Acronym { //begin Acronym //variables public String name = ""; public String newword = ""; public String original = ""; char ch = '?'; public void setData(String names,String new_word,String original_) { //begin setData name = names; newword = new_word; original = original_; } //end setData public boolean fowardAcronym() { //begin fowardAcronym boolean yes = false; String acro = ""; int space = 0; ch = name.charAt(0); acro += ch; space = name.indexOf(" "); ch = name.charAt(space + 1); newword += ch; if(!yes) { yes = true; } return yes; } //end fowardAcronym public boolean reverseAcronym() { //begin reverseacronym boolean yes = false; String acroed = ""; int space = 0; ch = name.charAt(0); acroed += ch; space = name.indexOf(" "); ch = name.charAt(space + 1); original += ch; if(!yes) { //begin if yes = true; } //end if return yes; } //end reverseacronym public void printAcronym() { //begin printAcronym if(fowardAcronym()&& reverseAcronym()) { //begin if out.printf("Given the word %S\n",name); out.printf("The forword acrony is %S\n",newword); out.printf("The reverse acrony is %S \n",original); } //end if } //end printAcronym } //end Acronym
import java.util.Scanner; import static java.lang.System.*; import java.io.File; import java.io.IOException; public class AcronymRunner { //begin AcronymRunner public static void main(String[] args) throws IOException { //begin main Scanner infile = new Scanner (new File("c:\\csa\\Acronym.txt")); Acronym ac = new Acronym(); //variables String names = ""; String new_word = ""; String original_ = ""; int named = 0; named = infile.nextInt(); for(int i = 0; i < named; i++) { //begin for names=infile.next(); ac.setData(names,new_word,original_); ac.printAcronym(); } //end for } //end main } //end AcronymRunner