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 5 of 5

Thread: Please Help (StringBuffer, Instantiable Class)

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Please Help (StringBuffer, Instantiable Class)

    Develop an application that accepts one word from the user as input. The application should then translate and output the word in a secret language according to the following rules: The first letter of the word is placed at the end and the letters ‘ay’ are added to the end.
    The application should make use of instantiable classes.

    myCODE(Instantiable class):
     public class PigLatin{
    	 private String word;
    	 private String newWord;
    	 private StringBuffer strBuff;
     
    	 public PigLatin(){
    		 word = " ";
    		 newWord = " ";
    		 strBuff = new StringBuffer();
    	 }
     
    	 public void setWord(String word){
    		 this.word = word;
    	 }
     
    	 public void compute(){
    		 for(int i = 1; i < word.length(); i = i +1){
    			 strBuff.append(word.charAt(i));	
    		}
    		 strBuff.append(word.charAt(0));
    		 strBuff.append("ay");
    		 newWord = strBuff.toString();
     
    	 }
     
     
    	 public String getNewWord(){
    		 return newWord;
    	 }
     }
     
     
    [QUOTE]AppClass:[/QUOTE]
     
    import javax.swing.JOptionPane;
     public class PigLatinApp{
    	 public static void main(String args[]){
    		 String word, newWord;
     
    		 PigLatin myPigLatin = new PigLatin();
     
    		 word = JOptionPane.showInputDialog(null,"Enter a word");
    		 myPigLatin.setWord(word);
     
    		 myPigLatin.compute();
     
    		 newWord = myPigLatin.getNewWord();
    		 JOptionPane.showMessageDialog(null, "That translates to "+newWord);
    	 }
     }

    There's no problem to this code as it functions properly.
    For example, Hello becomes elloHay.


    However, I have difficulties modifying this code so that it allows the user to translate a sentence made up of many words separated by spaces.
    For example, Hello world becomes elloHay orldway.

    I think I'll have to store newWord into an array and then loop through that array to print it back out. How do I store this newWord variable into an array??


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please Help (StringBuffer, Instantiable Class)

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (StringBuffer, Instantiable Class)

    i need answers! :S

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please Help (StringBuffer, Instantiable Class)

    A sentence of multiple words separated by spaces can be stored into an array of String objects using:

    String[] wordArray = JOptionPane.showInputDialog(null,"Enter a word or a sentence").split( " " );

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (StringBuffer, Instantiable Class)

    i see i get that..

    but i need to go through the loop first

    public void compute(){
    		 for(int i = 1; i < word.length(); i = i +1){
    			 strBuff.append(word.charAt(i));	
    		}
    		 strBuff.append(word.charAt(0));
    		 strBuff.append("ay");
    		 newWord = strBuff.toString();
     
    	 }

    before I store it into the array so I have the sentence: "Hello world" into "elloHay orldWay"..

Similar Threads

  1. StringBuilder Vs StringBuffer
    By qazi in forum Java Theory & Questions
    Replies: 0
    Last Post: July 28th, 2013, 08:31 AM
  2. StringBuffer Problem
    By msinc210 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 30th, 2012, 12:46 PM
  3. How to I convert this Sentence String to StringBuffer?
    By iCitationNeeded in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2012, 11:45 AM
  4. StringBuffer Implemenatation
    By tcstcs in forum Java Theory & Questions
    Replies: 5
    Last Post: May 5th, 2011, 01:44 PM
  5. String Vs StringBuffer
    By kalees in forum Java SE APIs
    Replies: 5
    Last Post: November 6th, 2009, 03:27 AM

Tags for this Thread