Overclock.net banner

1 - 7 of 7 Posts

·
Registered
Joined
·
214 Posts
Discussion Starter · #1 ·
Just wondering if this needs any cleaning up? Or if there are any other ways to do this.

At the moment, I am using 19 loops within 1 loop, and 4 loops within loops within loops, and it just seems unconventional.

What it does:
Scrambles a word.
First and last letter stay in the same position.
4 letter words have middle two letter reversed.
1,2, and 3 letter words stay the same.

Known Problems:
Doesn't recognize "Enter" or a new line
Have to add sections for punctuation within the word such as hyphens, as well as punctuation before the word such as quotations.

Code:
Code:
        String[] result = jTextArea1.getText().split("\\\\s");

        jTextArea2.setText("");

        for ( int i = 0; i < result.length; i++) {
            ArrayList<Character> chars = new ArrayList<Character>(result[i].length());

                for ( char c : result[i].toCharArray() ) {
                    chars.add(c);
                }

                char x = 0;
                char a = chars.get(0);             
                char b = 0;
                String xb = null;

                if (chars.size() == 1)  {
                    //a = chars.get(0);
                    //chars.remove(0);
                    //System.out.println("Size is 1 so we only take the first letter");
                }
                else {
                    x = chars.get(chars.size()-1);               
                }

                if (!Character.isLetter(x)) {
                    if (chars.size() == 1)  {
                        a = chars.get(0);
                        chars.remove(0);
                        //System.out.println("Size is 1 so we only take the first letter");
                    }
                    else {
                        x = chars.get(chars.size()-1);
                        String s1 = Character.toString(x);

                        b = chars.get(chars.size()-2);
                        String s2 = Character.toString(b);

                        xb = (s2 + s1);

                        chars.remove(0);
                        chars.remove(chars.size()-1);
                        chars.remove(chars.size()-1);
                    }

                }

                else if (Character.isLetter(x)) {
                    if (chars.size() == 1)  {
                        chars.remove(0);
                        //System.out.println("The new size of this char is: " + chars.size());
                    }
                    else {
                        chars.remove(0);
                        chars.remove(chars.size()-1);                   
                        //System.out.println("The new size of this char is: " + chars.size());
                    }
                }

                if (xb == null) {
                    //System.out.println(chars + " First Letter: " + a + " " + "Last Letter: " + x);
                }
                else if (xb != null)    {
                    //System.out.println(chars + " First Letter: " + a + " " + "Last Letter: " + xb);
                }

                if (chars.size() == 2)  {
                    //System.out.println("Size is 2, lets reverse this");
                    Collections.reverse(chars);
                    //System.out.println("New combination" + chars);
                }
                else if (chars.size() != 2) {
                    Collections.shuffle(chars);
                }

                char[] shuffled = new char[chars.size()];
                    for ( int j = 0; j < shuffled.length; j++ ) {
                        shuffled[j] = chars.get(j);
                }

            String shuffledWord = new String(shuffled);

            if (xb == null) {
                if (chars.size() == 1)  {
                    jTextArea2.append(a + shuffledWord + x + " ");
                    //System.out.println("a = " + a + " - shuffled word = " + shuffledWord);
                }
                else {
                    jTextArea2.append(a + shuffledWord + x + " ");         
                    //System.out.println("a = " + a + " - shuffled word = " + shuffledWord);        
                }
            }

            /*
             * Test Text:
             * 
             * Easy Text:
             * I would very much appreciate it if you could point me to a guide tutorial or just share with me how to do these things.
             * 
             * Advanced Text:
             * The objective for this assignment is the beginning phase of a capstone project in which you will explore an issue that is currently occurring to a group of people who are being marginalized. 
             * You will explore-deeply-a topic of your choice related to the concept of marginalization and how different groups are targeted and are dealing with this concept. 
             * Rather than arguing a particular point of view in this essay, your objective is simply to "wallow in complexity:" 
             * to research and analyze, to posit thesis, antithesis, and synthesis; to challenge the common ways of looking at a problem.
             * 
             */

            else if (xb != null)    {
                if (chars.size() == 1)  {
                    System.out.println("a = " + a + " - shuffled word = " + shuffledWord);
                    jTextArea2.append(a + " ");
                }
                else {
                    System.out.println("a = " + a + " - shuffled word = " + shuffledWord);
                    jTextArea2.append(a + shuffledWord + xb + " ");                 
                }
                xb = null;
            }
        }
    }
 

·
Registered
Joined
·
3,233 Posts
Aye, I count three as well. What was this about "19 loops within 1 loop, and 4 loops within loops within loops?" lol
 

·
Premium Member
Joined
·
10,773 Posts
Quote:
Originally Posted by Plex;15258325
Aye, I count three as well. What was this about "19 loops within 1 loop, and 4 loops within loops within loops?" lol
Who knows, its got me stumped!
cool.gif


maybe he's referring to the nested conditionals, but I haven't been bothered to count them
tongue.gif
 

·
Registered
Joined
·
214 Posts
Discussion Starter · #5 ·
Quote:
Originally Posted by tompsonn;15258527
Who knows, its got me stumped!
cool.gif


maybe he's referring to the nested conditionals, but I haven't been bothered to count them
tongue.gif
I think that may have been what I was talking about. I actually don't remember why I posted this... Was probably not fully in a functional state of mind.
 

·
Premium Member
Joined
·
10,773 Posts
Lol.

For the record, it could probably do with some cleaning up, yes. But I'm not a Java programmer.

I could give you a C# implementation though! (If that'd be any help...)
 

·
Registered
Joined
·
214 Posts
Discussion Starter · #7 ·
Quote:
Originally Posted by tompsonn;15259291
Lol.

For the record, it could probably do with some cleaning up, yes. But I'm not a Java programmer.

I could give you a C# implementation though! (If that'd be any help...)
Why not :]

I might learn something
 
1 - 7 of 7 Posts
Top