简体   繁体   中英

I need to randomly remove half of the words in a string in java

Ok so I'm working on this code to blend humanities and STEM. I know very basic java code and so I'm currently trying to stick to String methods. I know using arrays may be easier but I'm not well learned in how to use them. So so far I've made code that counts the words in the string in order to determine how many words to remove (half of them). Next I need to figure out a way to randomly remove half of the words and return a new string, possibly with spaces replacing the removed letters.

Here is my code so far:

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;


}

}

I copied the array to an arrayList to then iterate through the list and delete random elements. I hope this is the type of answer you are looking for.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM