Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with finding initials in my Java program...

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with finding initials in my Java program...

    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.
    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
    Runner(main class file)
     
    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with finding initials in my Java program...

    What does this code do?

    Take a step back from the computer and think about what you're trying to do. How do you do it without a computer? Pretend you have a really dumb friend who has no idea how to figure out what the initials of a given set of words or names are. Write out step-by-step instructions that your friend could follow (remember how dumb he is). When you have that, you'll have an algorithm that you can start writing code for.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with finding initials in my Java program...

    Since you never change the value of new_word or original_ in your main method what is the purpose of passing empty Strings to your Acronym object?
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with finding initials in my Java program...

    new_word is for the initials like "rain every few" new_word would equal "rew" and original_ is supose to get me the end initals but backwords so original should return "wyn". I didnt mention this other part of the program but i figured i could figure that out once ive figured the rest of the program out.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  2. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  3. JAVA game black jack need help on finding an error!!
    By Clyde90 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 13th, 2012, 08:40 AM
  4. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  5. How do you write a Java program that displays your initials?
    By kala99 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 30th, 2011, 02:12 PM