2016-08-04 11 views
0

私は人文とSTEMを混ぜるためにこのコードに取り組んでいます。私は非常に基本的なJavaコードを知っているので、私は現在Stringメソッドに固執しようとしています。私は配列の使用が簡単かもしれないが、私はよくそれらを使用する方法で学んでいないよ知っている。これまでは、削除する単語の数(半分)を決定するために、文字列内の単語を数えるコードを作成しました。次に、単語の半分をランダムに削除し、新しい文字列を返す方法を見つけ出す必要があります。ここでJavaの文字列中の単語の半分を無作為に削除する必要があります

は、これまでの私のコードです:

public class wordcount 
{ 
    public static void main (String[] args) 
    { 

    System.out.println("Simple Java Word Count Program"); 

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    int wordCount = wordArray.length; 

    System.out.println(str1 + ""); 

    System.out.println("Word count is = " + wordCount); 

    int wordCount2 = wordCount/2; 


} 

}

+0

何質問をしていますか?ランダム性を生成する方法や、配列からアイテムを削除する方法についていますか? – sprinter

+1

本当に明白な "あなたの質問はどういうもの"以外にも.. Javaコーディングスタイルのガイドを読んでください。クラス名は大文字で始まり、あなたもCamelCaseを使用しています...あなたがちょうど始まったときでも:あなたのものに有益な名前をつけてみてください。 「str1」は意図した使い方について何も言わない。それを "fullSentence"などの名前に変更してください。突然、それが何であるかは非常に明確です。そして、何かを呼び出さないでください。wordArray - コレクションのタイプが変わるかもしれません。たとえば「allTheWords」と呼んでください。 – GhostCat

答えて

2

私は、リストを反復処理し、ランダムな要素を削除するには、ArrayListに配列をコピーしました。私はこれがあなたが探している答えのタイプであることを願っています。

public static void main(String[] args) { 

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(wordArray)); 

    int wordCount = wordList.size(); 
    int halfWordCount = wordCount/2; 
    int tracker = 0; //counter for iterations in while loop 

    Random random = new Random(); 
    while(tracker < halfWordCount){ 
     int randomIndex = random.nextInt(wordList.size()); 
     wordList.remove(randomIndex); 
     tracker++; 
    } 
    System.out.println(wordList.toString()); 
} 
+0

私は実践的な結果を得ましたが、私は入力と修正が大好きです。私はまた、詩の入力を取る方法と、マウスのクリックの後に "削除方法"を行うことが必要です –

+0

私の答えはあなたのために働いたのですか?もしそうなら、それを受け入れてもらえますか? –

0
import java.util.Arrays; 
import java.util.* ; 
public class wordcount 
{ 
public ArrayList<Integer> test(Integer[] array) 
    { 
     ArrayList<Integer> list = new ArrayList<Integer>(); 
     for(int i=0; i<array.length; i++) 
      list.add(array[i]); 
     return list; 
    } 
public ArrayList<String> testS(String[] array) 
    { 
     ArrayList<String> list = new ArrayList<String>(); 
     for(int i=0; i<array.length; i++) 
      list.add(array[i]); 
     return list; 
    } 
public static void main (String[] args) 
{ 

    System.out.println("Removing random words in a Poem Program"); 

    String str1 = "Sample Poem by Noah Eli Gordon: Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    int wordCount = wordArray.length; 

    System.out.println(str1 + ""); 

    //System.out.println("Word count is = " + wordCount); 

    //System.out.println(wordArray); 
    //String[] ret = wordArray; 
    //for(String str : ret) 
    // System.out.print(str); 

    int wordCount2 = wordCount/2; 

    Integer[] myIntArray = new Integer[wordCount]; 
    //for(int i = 0; i<wordCount;i++) 
    // myIntArray[i] = i; 
    //for(int str : myIntArray) 
     //System.out.print(str); 

    wordcount w = new wordcount(); 
    String[] wordArray2 = new String[wordCount2]; 

    for(int i = 0; i <= wordCount2; i++) 
    { 
     int rand = (int)(Math.random()*(myIntArray.length-1)); 
     ArrayList<Integer> list = w.test(myIntArray); 
     list.remove(rand); 
     myIntArray = list.toArray(myIntArray); 
     ArrayList<String> listS = w.testS(wordArray); 
     listS.remove(rand); 
     wordArray2 = listS.toArray(wordArray); 


    } 
    List<String> list = new ArrayList<String>(); 

    for(String s : wordArray2) 
    { 
     if(s != null && s.length() > 0) 
     { 
      list.add(s); 
     } 
    } 

    wordArray2 = list.toArray(new String[list.size()]); 

    //for(int str : myIntArray) 
     //System.out.println(str); 
    System.out.println(); 
    String[] ret2 = wordArray2; 
    for(String str : ret2) 
     System.out.print(str + " "); 
} 
} 
関連する問題