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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 35

Thread: How to Create a Word Rectangle

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to Create a Word Rectangle

    Hello, I was wondering how to create the code for a word rectangle in java?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Can you define a "word rectangle" and show an example?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    When you enter a word it will display it as a regular word then shift a letter to the right, and it will be in rows and columns for the amount of letters the word has. Example:

    COMPUTERS
    OMPUTERSC
    MPUTERSCO
    PUTERSCOM
    UTERSCOMP
    TERSCOMPU
    ERSCOMPUT
    RSCOMPUTE
    SCOMPUTER
    Last edited by Pettsa; May 3rd, 2012 at 07:46 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    If the project is only changing the position of characters in a String, look at the String class's methods.
    And the StringBuilder and StringBuffer classes.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    Um, I understand those methods but I don't know where to start.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Start by removing one character from the start of the String.
    Concatenate that character at the end of the String.
    Print it.
    Do it again until done.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    Um, I am sorry, I understand the methods, really I do, this is all I have so far, could you please show me how to do it. My teacher doesn't teach us at all how to do these things and I am so stuck on how to do it, please if you can. Here is what I have so far:

    package wordrectangle;
    import javax.swing.JOptionPane;
    import java.lang.String;

    public class WordRectangle {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    //Declare variables
    int word, str;

    //Ask the user to enter a word from a string and convert to a double
    String inputWord = JOptionPane.showInputDialog("Enter a word and this "
    + "program will make a word rectangle with it:");

    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    I understand the methods,
    If you understand the methods and how to use them and what they do, what are you waiting for to use some of them to solve the problem?
    A hint:
    1)extract two Strings from the one String. One with a single character, the other with the rest of the characters.
    abcd -> a and bcd
    2) concatenate the single character String at the end of the other String ->bcda
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    I just don't know where they would go in my program.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Solve the problem one small step at a time.
    Take a String and move its first character to the end of the String. Can you do that?

    When that is done, what next?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    Ok I'm trying to figure this out......
    Here is what I have so far.... can you show me an example of what to do?

    package wordrectangle;
    import javax.swing.JOptionPane;
    import java.lang.String;
     
    public class WordRectangle {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //Declare variables
            int count = 0;
            char firstLetter;
     
            //Ask the user to enter a word from a string and convert to a double
            String word = JOptionPane.showInputDialog("Enter a word and this "
                    + "program will make a word rectangle with it:");
     
            //Declare the length that will find the number of vowels in the sentence
            for (int i = 0; i < word.length(); i++) {
            firstLetter = word.charAt(0);
            word = word.substring(1, word.length());
            System.out.println(firstLetter + word);        
                }
            }   
        }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    What prints out when you execute the program? Is the output what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    It outputs:

    computers
    omputers
    mputers
    puters
    uters

    And, no that is not what I want.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    It looks like you are getting the second part of the String OK. what is happening to the first letter that is supposed to be concatenated to the end of the String? It looks like you remove it and throw it away
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    How would I add it to the end?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Use the + operator:
    "sdd" + "e" -> "sdde"
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    On what line would I do that?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Have you tried it yet to see what happens? See posts #6 & #8 where I gave the steps the code should do.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    All I got it to work but... how would I loop it to make that word only have to right rows and columns for the specific word. Ex computers, 9 rows, 9 columns? Here is the code so far:
    package wordrectangle;
    import javax.swing.JOptionPane;
    import java.lang.String;

    public class WordRectangle {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    //Declare variables
    int count = 0;
    char firstLetter;

    //Ask the user to enter a word from a string and convert to a double
    String word = JOptionPane.showInputDialog("Enter a word and this "
    + "program will make a word rectangle with it:");

    //Declare the length that enable the word to be mixed up in the sentence

    for (int i = 0; i < word.length(); i++) {
    firstLetter = word.charAt(0);
    word = word.substring(1, word.length());
    System.out.println(firstLetter + word);
    i = word.indexOf(word);
    i = word.indexOf(firstLetter);
    word += firstLetter;
    */

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Please post the output from the program and add some comments describing what you want to change.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. You did not use code tags.


    Why does the code change the value of i two times inside of the loop?
    It is dangerous to change the loop control variable inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    public class WordRectangle {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //Declare variables
            int count = 0;
            char firstLetter;
     
            //Ask the user to enter a word from a string and convert to a double
            String word = JOptionPane.showInputDialog("Enter a word and this "
                    + "program will make a word rectangle with it:");
     
            //Declare the length that enable the word to be mixed up in the sentence 
     
            for (int i = 0; i < word.length(); i++) {
            firstLetter = word.charAt(0);
            word = word.substring(1, word.length());
            System.out.println(firstLetter + word);
            i = word.indexOf(word);
            i = word.indexOf(firstLetter);
            word += firstLetter;
     
     
     
     
                }
            }   
        }

    I want the program to stop when it makes a 9 x 9 rectangle. (Depending on the word, using a "computers" as an example. The program keeps running so its 9 x Inifinity.) If you could change it that would be awesome.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    The program keeps running so its 9 x Inifinity.
    Why does the code change the value of i two times inside of the loop?
    It is dangerous to change the loop control variable inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    I'm not sure, I am lost at what to do next. Do you think you can fix the code?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to Create a Word Rectangle

    Why does the code change the value of i two times inside of the loop?
            i = word.indexOf(word);
            i = word.indexOf(firstLetter);

    If your are not sure, then remove them.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Create a Word Rectangle

    Then what?

Page 1 of 2 12 LastLast

Similar Threads

  1. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  2. Replies: 2
    Last Post: October 31st, 2011, 09:19 AM
  3. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  4. Rectangle questions
    By stacksofamber in forum Java Theory & Questions
    Replies: 3
    Last Post: September 5th, 2011, 12:43 PM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM