我有一个文本文件,其中包含土耳其语中的所有五个字母的单词。从那里,我想画一个随机的词。所有的单词都是AssetManager丝网印刷,但不知怎的,我没有掉一个单词。我怎么能用循环?
如果你能提供帮助,我会很高兴。
来源:
public class MainActivity extends Activity {
Button degistir;
TextView kelime;
EditText ykelime;
Random rnd =new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
degistir=(Button)findViewById(R.id.btngiris);
kelime=(TextView)findViewById(R.id.kelime);
ykelime=(EditText)findViewById(R.id.giris);
AssetManager assetManager = getAssets();
// To load text file
InputStream input;
try {
input = assetManager.open("5HarfliKelimeler.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
String text = new String(buffer);
int sayi=rnd.nextInt(text.length());
kelime.setText(text);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
degistir.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
kelime.setText(ykelime.getText());
ykelime.setText(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
首先,如果您的文件内容不会被更改,那么您必须计算最大行数并将其写为常量:
private String getRandomLine() {
final int FILE_MAX_LINE_INDEX = 5000;//if your file has 5000 lines, and it's content never be changed
Random rnd = new Random();
final int lineIndex = rnd.nextInt(FILE_MAX_LINE_INDEX);
try {
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
String s;
while ((s = lnr.readLine()) != null) {
if (lnr.getLineNumber() == lineIndex) {
lnr.close();
return s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
或者,如果您的内容可能会被更改,并且您不知道文件中将包含多少行:
private String getRandomLine() {
List<String> fiveLetterWords = new ArrayList<String>(5000);
try {
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
String s;
while ((s = lnr.readLine()) != null) {
fiveLetterWords.add(s);
}
lnr.close();
} catch (IOException e) {
e.printStackTrace();
}
Random rnd = new Random();
return fiveLetterWords.get(rnd.nextInt(fiveLetterWords.size()));
}
答案 1 :(得分:0)
逐行读取文件并将所有单词列入列表。(How to read line by line | check here)
然后,为每个按钮单击指定随机索引。最后,使用生成的索引显示列表中的单词。 (索引范围必须从0到列表大小)
- 我编辑了你的代码,没有任何编辑器。 -
public class MainActivity extends Activity {
Button degistir;
TextView kelime;
EditText ykelime;
List<String> wordList=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
degistir=(Button)findViewById(R.id.btngiris);
kelime=(TextView)findViewById(R.id.kelime);
ykelime=(EditText)findViewById(R.id.giris);
AssetManager assetManager = getAssets();
// To load text file
InputStream input;
try {
input = assetManager.open("5HarfliKelimeler.txt");
wordList= new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(input , charset.forName("UTF-8")));
String line = "";
while ((line = reader.readLine()) != null) {
wordList.add(line);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
degistir.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(wordList!=null){
Random rnd =new Random();
Integer generatedIndex = rnd.nextInt(wordList.size());
String selectedWord=wordList.get(generatedIndex);
kelime.setText(selectedWord);
// ykelime.setText(null);
}
else{
//DISPLAY MSG ETC.
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}