1

I am attempting to make a simple program which would take in a string containing 4 words, then split this string into the 4 words, and print all possible arrangements of these 4 words

Here is the source, the regex is on line 21, which I'm not confident is correct. Also it really doesn't like my nested for loops

 /**


 * Author: peaceblaster
 * Date 9/10/2013
 * Title: hw3
 * Purpose: To take in 4 words as a single string, then prints all permutations of the four words
 */

//import stuff
import java.util.Scanner;
public class hw3 {
    public static void main(String[] args){
        //declarations:
        Scanner in = new Scanner(System.in);
        String input = new String();
                String[] wordArray = new String[4];
                int a,b,c,d;
        //input
        System.out.println("Input 4 words: ");
        input = in.next();
        //regex
        wordArray = input.split("^*[^\s]|\s*\s|*$"); //splits string into array containing each words as a string
                                    // ^* finds first word \s*\s finds words surrounded by spaces *$ finds final word
        //output
        for (a=1; a=<4; a++){
            for (b=1; b=<4; b++){
                for (c=1; c=<4; c++){
                    for (d=1; d=<4; d++){
                        System.out.println(wordArray[%a] + " " + wordArray[%b] + " " + wordArray[%c] + " " + wordArray[%d]);  //uses nested for loops to print permutations as opposed to hard-coding
                    }
                }
            }
        }
    }
}
2
  • Isn't your looping method going to duplicate words? Is that what you want? Commented Sep 10, 2013 at 17:24
  • Never mind regexes and nested loops for the moment, you need to finish learning Java syntax. How are we supposed to help you when you give us code that won't even compile? Commented Sep 10, 2013 at 18:21

3 Answers 3

1

Get input through

String words[] = new String[4];

for (int i = 0; i < words.length; ++i) {
    words[i] = in.nextLine(); 
}

//or
String words[] = in.nextLine().split("\\s+"); //space separated words.

You don't need to use % before variable names

System.out.println(wordArray[a] + " " + wordArray[b] + " " + wordArray[c] + " " + wordArray[d]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much though it's still not taking my for loops... I'll double check my syntax
1

Arrays are 0 based, your for loops are wrong. I also added conditions to not use the same word more than once for you permutations. It should look like:

for (a = 0; a < 4; a++) {
    for (b = 0; b < 4; b++) {
        if (b == a) continue;
        for (c = 0; c < 4; c++) {
            if (c == a || c == b) continue;
            for (d = 0; d < 4; d++) {
                if (d == a || d == b || d == c) continue;
                System.out.println(wordArray[a] + " " + wordArray[b] + " " + wordArray[c] + " " + wordArray[d]);
            }
        }
    }
}

Also, like bsd mentioned, your split expression should be:

wordArray = input.split("\\s+");

Comments

0

Java's split method expects a regex which describes the "separator" string, not the strings you want to be left over. You seem to be assuming the latter. Note also that since you're describing strings that separate words, you don't need to account for the possibility of no separator at the beginning or end of the target. By the same token, if a separator string does occur at the beginning or end, split will not return an empty string for either of those cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.