我是否过度复杂化了Java中的简单解决方案?

我是否过度复杂化了Java中的简单解决方案?,java,Java,对于Java作业,我需要创建一个脚本,返回字符串中的第一个单词,作为第二部分,我还需要返回第二个单词。我目前正在进行第一部分的工作,我想我已经接近了,但我也在想,我的代码是否过于复杂了一点 public static void statements(){ Scanner userInput = new Scanner(System.in); char [] sentenceArray; String userSentence; char sentenceResul

对于Java作业,我需要创建一个脚本,返回字符串中的第一个单词,作为第二部分,我还需要返回第二个单词。我目前正在进行第一部分的工作,我想我已经接近了,但我也在想,我的代码是否过于复杂了一点

public static void statements(){
    Scanner userInput = new Scanner(System.in);
    char [] sentenceArray;
    String userSentence;
    char sentenceResult;
    System.out.print("Enter a complete sentence: ");
    userSentence = userInput.nextLine();
    for(int x = 0; x < userSentence.length(); x++){
        sentenceResult = userSentence.charAt(x);
        sentenceArray = new char[userSentence.length()];
        sentenceArray[x] = sentenceResult;
        if(sentenceArray[x] != ' '){
            System.out.print(sentenceArray[x]);
            //break; This stops the code at the first letter due to != ' '
        }
    }
}

就我个人而言,我认为你想得太多了。为什么不通读整行并用空格分隔字符串呢?这不是一个完整的解决方案,只是一个建议,你可以如何得到的话

public static void main(String[] args) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a complete sentence: ");
    try {
        String userSentence = reader.readLine();
        String[] words = userSentence.split(" ");
        System.out.println(words[0]);
        System.out.println(words[1]);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

因为这是你的家庭作业,如果我给你代码并帮你解决它,我会很难过

看起来你真的把它复杂化了,而且你也意识到了,所以这是个好兆头

我需要创建一个脚本,返回字符串中的第一个单词, 作为第二部分,我还需要返回第二个单词

因此,您有一个
字符串
对象,然后检查该类的

可以用两行代码来解决这个问题,但是:

  • 您必须了解String类的一个特殊方法,最有用的方法将是能够以某种方式
    为您拆分字符串的方法
  • 您需要了解-单词之间用
    空格隔开
  • 拆分
    字符串后,您应该得到一个数组,通过数组的
    索引访问
    第一个
    第二个
    元素就足够了

  • 我会这样做的。为什么不把所有的话都还给我

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * Add something descriptive here.
     * User: MDUFFY
     * Date: 8/31/2017
     * Time: 4:58 PM
     * @link https://stackoverflow.com/questions/45989774/am-i-over-complicating-a-simple-solution
     */
    public class WordSplitter {
    
        public static void main(String[] args) {
            for (String arg : args) {
                System.out.println(String.format("Sentence: %s", arg));
                List<String> words = getWords(arg);
                System.out.println(String.format("# words : %d", words.size()));
                System.out.println(String.format("words   : %s", words));
            }
        }
    
        public static List<String> getWords(String sentence) {
            List<String> words = new ArrayList<>();
            if ((sentence != null) && !"".equalsIgnoreCase(sentence.trim())) {
                sentence = sentence.replaceAll("[.!?\\-,]", "");
                String [] tokens = sentence.split("\\s+");
                words = Arrays.asList(tokens);
            }
            return words;
        }
    }
    
    以下是我得到的结果:

    Sentence: The quick, agile, beautiful fox jumped over the lazy, fat, slow dog!
    # words : 12
    words   : [The, quick, agile, beautiful, fox, jumped, over, the, lazy, fat, slow, dog]
    
    Process finished with exit code 0
    

    为什么不使用
    Scanner.next()
    ?扫描器最适合这种情况。您只需将一个字符串传递给构造函数,然后执行下一步读取。下一个最好的选择是在空格
    \s+
    上拆分输入。不确定要求是什么(您没有提到任何要求),但如果您可以使用库函数,请查看可能的重复项,因为您正在为循环中的每个字符创建数组。Move
    sentenceArray=new char[UserSession.length()]到循环之外,我认为只有这样才能帮助您确定下一步要做什么…尝试用数组替换列表。他可能还没学会,它会留下来。有弹性的目标;很容易理解。如果单词被多个空格字符分隔,这将不起作用,因此在
    拆分
    方法中,regexp
    \\s+
    会更好。这很有帮助。我搜索了所有的方法,正如你提到的,使用了split。我对此一无所知,它正是我所需要的。谢谢!
    "The quick, agile, beautiful fox jumped over the lazy, fat, slow dog!"
    
    Sentence: The quick, agile, beautiful fox jumped over the lazy, fat, slow dog!
    # words : 12
    words   : [The, quick, agile, beautiful, fox, jumped, over, the, lazy, fat, slow, dog]
    
    Process finished with exit code 0