1

How can I split each of the sentence in the arraylist? what i'm trying to make is, a different set of sentence to be splitted and shuffle, and student will arrange it in correct order

public class QuestionActivity1 extends AppCompatActivity {

    private int presCounter = 0;
    private int maxpresCounter;

    private List<QuestionModel1> list;

    TextView textScreen, textQuestion, textTitle;
    Button submitBtn;


    @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question1);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        submitBtn=(Button) findViewById(R.id.submitBtn);


        list=new ArrayList<>();
        list.add(new QuestionModel1("The kids are playing"));
        list.add(new QuestionModel1("The kids are sleeping"));
        list.add(new QuestionModel1("The kids are dancing"));
        

        keys=shuffleArray(keys);
        for (String key : keys){
            addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) findViewById(R.id.editText)));
        }
        maxpresCounter= keys.length;

    }

I can only try one

private String sentence="The kids are playing";
private String[] keys=sentence.split(" ");

QuestionModel Class

public class QuestionModel1 {
    private String sentence;

    public QuestionModel1(String sentence) {
        this.sentence = sentence;
    }

    public String getSentence() {
        return sentence;
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }
}

3
  • please add an minimal reproducible example Commented Nov 4, 2022 at 22:38
  • Can you provide your QuestionModel1 class, or at least a part of it? What are the getter methods of the class called? Commented Nov 4, 2022 at 23:29
  • @BigBeef i added the class Commented Nov 4, 2022 at 23:34

2 Answers 2

2
    for (QuestionModel1 questionModel1 : list) {
        String sentence = questionModel1.getSentence();
        String[] keys = sentence.split(" ");
        // do what you need below for each key.
        // shuffleArray(keys); <--- A stab at what you need ??? 
    }

Something like this?

Sign up to request clarification or add additional context in comments.

1 Comment

ill try this one, i think i can almost figure it out
1

So, assuming we have the following array list:

List<QuestionModel1> list = new ArrayList();
list.add(new QuestionModel1("The kids are playing"));
list.add(new QuestionModel1("The kids are sleeping"));
list.add(new QuestionModel1("The kids are dancing"));

you could split it with the following snippet, assuming you're using at least Java 8

List<List<String>> newList = list.stream()
                .map(item -> Arrays.asList(item.getSentence().split(" ")))
                .collect(Collectors.toList());

and this would yield the following List<List<String>> as a result

[[The, kids, are, playing],[The, kids, are, sleeping],[The, kids, are, dancing]]

Now, if you just want the words in one long list (including duplicates), you could just add one additional .flatMap(...) operation

List<String> keys = list.stream()
                .map(item -> Arrays.asList(item.getSentence().split(" ")))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

which would yield the following List<String> as a result:

[The, kids, are, playing, The, kids, are, sleeping, The, kids, are, dancing]

This isn't the only approach (you could use .reduce(...) instead of .flatMap(...), for example), but it should fit your problem.

1 Comment

and how can i implement that in this for (String key : keys){ addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) findViewById(R.id.editText))); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.