2017-08-31 18 views
0

私のJavaの宿題では、文字列内の最初の単語を返すスクリプトを作成する必要があります。また、2番目の単語として2番目の単語も返す必要があります。私は現在、最初の部分で作業しています。私は近いと思いますが、私のコードを少し複雑にしているかどうかも疑問です。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 != ' ' 
     } 
    } 
} 

私はほとんどそれを得たと思う。私が作業をする必要があるのは、スペースがあることを認識したらforループが終了するだけですが、メッセージ全体に関係なくそれが表示されます。私はこれがもっと簡単にできるだけでなく、おそらく私が代わりにやり遂げることができることのヒント、あるいは仕上げの仕方のヒントかも知れません。

編集:splitメソッドを使用して割り当てを完了することができました。これは現在の外観です

public static void statements(){ 
    Scanner userInput = new Scanner(System.in); 
    String userSentence; 
    System.out.print("Enter a complete sentence: "); 
    userSentence = userInput.nextLine(); 
    String [] sentenceArray = userSentence.split(" "); 
    System.out.println(sentenceArray[0]); 
    System.out.println(sentenceArray[1]); 
    } 
} 
+0

'Scanner.nextを()'は使用しない理由:

"The quick, agile, beautiful fox jumped over the lazy, fat, slow dog!" 

ここで私が得る結果ですか? – shmosel

+1

スキャナはこれに最も適しています。コンストラクタに文字列を渡すだけで、次に読むことができます。次の最良のオプションは、入力を空白 '\ s +'で分割することです。 – Tezra

+0

要件が何であるかは不明ですが、ライブラリ関数を使用できる場合はhttps://stackoverflow.com/a/7683463/1566187 – Elyasin

答えて

1

それはあなたの宿題であるように、私はあなたのコードを与え、あなたのためにそれを解決することが悪いと感じます。

あなたは本当にそれを過度に複雑にしているように見えます。あなたはそれを知っているので、良い兆候です。私はパート2として、私もそう2番目の単語

を返却する必要がある、 、文字列内の最初の単語を返すスクリプトを作成する必要があると

、あなたはStringオブジェクトを持っていますそのクラスのmethodsをご確認ください。

コードの2行でそれを解決することは可能ですが、:

  1. あなたはStringクラスの一つの特別な方法を認識する必要がありますが、最も有用なものになることでし何とかsplitあなたのための文字列
  2. あなたはjava regular expressionsについてのある程度の知識を持っている必要があります - あなたはstringを分割した後、単語がspace
  3. で分離されている、あなたは、配列のwiのindexによってfirstsecond要素にアクセスして、配列を取得する必要があります十分にあるでしょう
+0

これは助けになりました。私は方法を探し、あなたが述べたように、分割を使用しました。私はそれについて全く知らなかったし、それはまさに私が必要としたことでした。どうもありがとう! – HoneybunHero

1

個人的には、あなたはそれを思い知らされていると思います。行全体を読み込み、文字列を空白で分割するのはなぜですか?これは完全な解決策ではなく、あなたがその言葉をどのように得ることができるかを示唆するものです。

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(); 
    } 
} 
+0

単語が2つ以上の空白文字で区切られているとうまくいかないので、regexp '\\ s +'は 'split'メソッドの方が良いでしょう – DevDio

+0

良い点@Devdio – Jessica

0

これは私がやる方法です。なぜ返さないでくださいすべて言葉?

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 
+1

リストを配列に置き換えてください。彼はおそらくそれをまだ学んでいないでしょう。 –

+0

それはそのままです。ストレッチの目標。把握しやすい – duffymo

関連する問題