Serkan Tarakcı
Serkan Tarakcı

Reputation: 305

Read lines from an EditText and add strings to the ArrayList

Hello this is the Java code that I want to rebuilt in Android but I'm having some problems. The java code:

package pickrandom;

import static java.lang.System.in;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Random;

public class Pickrandom {
    static Random randomGenerator;

    static Scanner userInput = new Scanner(System.in);
    static List<String> strings = new ArrayList<String>();

    private static void displayList() {
        System.out.println(strings);
    }

    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("Please enter your strings:");
        String names;
        String item;
        while (true){
            names = userInput.next();
            if(names.equalsIgnoreCase("quit"))
                break;
            else
                strings.add(names);
        }
        displayList();

        randomGenerator = new Random();

        int index = randomGenerator.nextInt(strings.size());
        item=strings.get(index);

        System.out.println("Picked name is: " + item);
    }

}

And this is the output:

Please enter your strings: 
cafe 
bar 
cinema 
quit 
[cafe, bar, cinema] 
Picked name is: cafe

I want to write the similar code in my Android program. I want to write some names to my edittext line by line and choose one name randomly.

This is my XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="3dp"
    android:text="Enter all names in the field below, each on a separate line:"
    android:textColor="#000000"
    android:textSize="21sp"
    android:textStyle="bold"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"/>
<EditText
    android:id="@+id/itemList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PICK ONE"
    android:layout_weight="0"/>
<TextView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="3dp"
    android:text=""
    android:textColor="#000000"
    android:textSize="21sp"
    android:textStyle="bold"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"/>

And my mainactivity:

public class MainActivity extends AppCompatActivity {

    static Random randomGenerator;
    static Scanner userInput = new Scanner(System.in);
    static List<String> strings = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.picker);

        final EditText itemList = (EditText) findViewById(R.id.itemList);
        final TextView myTextView = (TextView)findViewById(R.id.result);

        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                String items = itemList.getText().toString();

                String pickedItem;
                while (userInput.hasNextLine()){
                    strings.add(items);
                }
                randomGenerator = new Random();
                int index = randomGenerator.nextInt(strings.size());
                pickedItem=strings.get(index);

                myTextView.setText("Result: \n" + pickedItem);
            }
        });
    }
}

As I mentioned in the title I think I'm having problems about reading lines from an EditText and adding strings to the ArrayList. Thanks in advance for your help

Upvotes: 0

Views: 850

Answers (1)

vincrichaud
vincrichaud

Reputation: 2208

You should not use a Scanner anymore.

Once you get your String from your EditText, you should split it to obtain every line. Then add all substring to your List.

btn.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        String items = itemList.getText().toString();
        String[] subStrings = items.split("\n"); //split your String at every new line
        String pickedItem;
        strings.clear();
        for(String s : subStrings){ //run through all substrings to add them to the list
            strings.add(s);
        }
        randomGenerator = new Random();
        int index = randomGenerator.nextInt(strings.size());
        pickedItem=strings.get(index);

        myTextView.setText("Result: \n" + pickedItem);
    }
});

Upvotes: 1

Related Questions