Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Split a string in list array

Posted on November 11, 2022 By

Solution 1 :

    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?

Solution 2 :

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.

Problem :

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;
    }
}

Comments

Comment posted by minimal reproducible example

please add an

READ  [FIXED] android - How To Do Facebook Login in React Native?
Powered by Inline Related Posts

Comment posted by BigBeef

Can you provide your QuestionModel1 class, or at least a part of it? What are the getter methods of the class called?

Comment posted by Kazz Domi

@BigBeef i added the class

Comment posted by Kazz Domi

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

Comment posted by Kazz Domi

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

Android Tags:android, java

Post navigation

Previous Post: [FIXED] android – LottieAnimationView doesn’t show in my_layout.xml
Next Post: [FIXED] android – Firestore conditional array query

Related Posts

[FIXED] java – play and stop sound by different button click in same time Android
[FIXED] android – Kotlin: Override by a function with reified type parameter Android
[FIXED] android – How do I make the Recyclerview(in a fragment) clickable Android
[FIXED] reactjs – React native 0.61.2 Alert.alert not working on Android Android
[FIXED] java – My app force close when call putString on SharedPreference.Editor Android
[FIXED] Internal Error occurred while analyzing this expression error in Android Studio Dolphin 2021.3.1 Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme