gpt4 book ai didi

java - 从 EditText 读取行并将字符串添加到 ArrayList

转载 作者:行者123 更新时间:2023-12-02 11:25:36 24 4
gpt4 key购买 nike

您好,这是我想在 Android 中重建的 Java 代码,但我遇到了一些问题。 java代码:

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

}

这是输出:

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

我想在我的Android程序中编写类似的代码。我想在我的编辑文本中逐行写入一些名称,并随机选择一个名称。

这是我的 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"/>

我的主要 Activity :

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

正如我在标题中提到的,我认为我在从 EditText 读取行并将字符串添加到 ArrayList 时遇到问题。预先感谢您的帮助

最佳答案

您不应该再使用扫描仪。

从 EditText 获取字符串后,您应该将其拆分以获取每一行。然后将所有子字符串添加到您的列表中。

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

关于java - 从 EditText 读取行并将字符串添加到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49652208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com