Java中的正则表达式和字符串数组

Java中的正则表达式和字符串数组,java,regex,string,nested-loops,Java,Regex,String,Nested Loops,我正在尝试制作一个简单的程序,它将接收一个包含4个单词的字符串,然后将该字符串拆分为4个单词,并打印这4个单词的所有可能排列 这是来源,正则表达式在第21行,我不相信它是正确的。而且它真的不喜欢我的嵌套for循环 /** * Author: peaceblaster * Date 9/10/2013 * Title: hw3 * Purpose: To take in 4 words as a single string, then prints all permutations

我正在尝试制作一个简单的程序,它将接收一个包含4个单词的字符串,然后将该字符串拆分为4个单词,并打印这4个单词的所有可能排列

这是来源,正则表达式在第21行,我不相信它是正确的。而且它真的不喜欢我的嵌套for循环

 /**


 * 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
                    }
                }
            }
        }
    }
}
/**
*作者:和平战士
*日期2013年9月10日
*标题:hw3
*目的:将4个单词作为单个字符串,然后打印这4个单词的所有排列
*/
//进口货物
导入java.util.Scanner;
公共级hw3{
公共静态void main(字符串[]args){
//声明:
扫描仪输入=新扫描仪(系统输入);
字符串输入=新字符串();
String[]wordArray=新字符串[4];
INTA、b、c、d;
//输入
System.out.println(“输入4个字:”);
input=in.next();
//正则表达式
wordArray=input.split(“^*[^\s]|\s*\s |*$”;//将字符串拆分为包含每个单词作为字符串的数组
//^*查找第一个单词\s*\s查找被空格包围的单词*$查找最后一个单词
//输出
对于(a=1;a=通过

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.

Java的方法需要一个描述“分隔符”的正则表达式字符串,而不是您想要保留的字符串。您似乎假设了后者。另外请注意,由于您描述的字符串是分隔单词的字符串,因此不需要考虑在目标的开头或结尾没有分隔符的可能性。同样,如果分隔符字符串确实出现在开头或结尾,
split
对于这两种情况都不会返回空字符串。

数组基于0,您的for循环错误。我还添加了一些条件,使您的排列不能多次使用同一个单词。它应该如下所示:

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]);
            }
        }
    }
}

你的循环方法不会复制单词吗?这就是你想要的吗?暂时不要管正则表达式和嵌套循环,你需要完成Java语法的学习。当你给我们的代码甚至不会编译时,我们应该如何帮助你?非常感谢,尽管它仍然没有占用我的for循环…我会仔细检查我的语法
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]);
            }
        }
    }
}
wordArray = input.split("\\s+");