如何使用java(android studio)读取文本文件? 我对android开发非常陌生,我一直在跟随斯坦福CS193A的讲座学习一些东西。在尝试创建字典时(不使用stand ford library),我觉得我做的大部分事情都是对的,但我无法在手机上启动应用程序。这里是我的java代码。请帮帮我。 package com.hfad.dictionarytwo; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.ListView; 导入android.widget.TextView; 导入android.widget.Toast; 导入androidx.appcompat.app.appcompat活动; 导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.HashMap; 导入java.util.Scanner; 公共类MainActivity扩展了AppCompatActivity{ 私有HashMap字典; 私有数组列表; 专用阵列适配器; 私人ArrayList fiveDefns; @凌驾 受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dictionary=newhashmap(); 列表=新的ArrayList(); fiveDefns=新的ArrayList(); readWordsFromFile(); 挑选单词(); } 私有void readWordsFromFile(){ Scanner scan=new Scanner(getResources().openRawResource(R.raw.grewords)); //以下去麻器用于在制表符处拆分 while(scan.hasNextLine()){ String line=scan.nextLine(); String[]parts=line.split(“\t”); 如果(零件长度>=2){ 字符串字=部分[0]; 字符串defn=零件[1]; 列表。添加(word); 字典.put(单词,defn); } } } 私有无效字(){ //下面的代码洗牌所有单词,并取前三个单词 ArrayList threeWords=新的ArrayList(); 集合。洗牌(列表); 对于(int i=0;i=1){ 字符串字=部分[0]; 字符串defn=零件[1]; 列表。添加(word); 字典.put(单词,defn); } } } 挑选单词(); } 私有无效字(){ //下面的代码洗牌所有单词,并取前三个单词 ArrayList threeWords=新的ArrayList(); 收藏 abcd kolkata valsad 123 auto vapi 789 neha palghar klj ma ghfgh mnk fgdgdf papa public class MainActivity extends AppCompatActivity { private HashMap<String, String> dictionary; private ArrayList<String> list; private ArrayAdapter<String> adapter; private ArrayList<String> fiveDefns; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dictionary = new HashMap<String,String>(); list = new ArrayList<String>(); fiveDefns = new ArrayList<String>(); readWordsFromFile(); } private void readWordsFromFile(){ Scanner scan = new Scanner(getResources().openRawResource(R.raw.grewords)); //following delimator is for spliting at tab while (scan.hasNextLine()){ String line = scan.nextLine(); if(line.contains(" ")) { String[] parts = line.split(" "); if (parts.length >= 1) { String word = parts[0]; String defn = parts[1]; list.add(word); dictionary.put(word, defn); } } } pickRandomWords(); } private void pickRandomWords() { //folowing code shuffles all the words and takes the first three words ArrayList<String> threeWords = new ArrayList<String>(); Collections.shuffle(list); for (int i = 0; i < 3; i++) { threeWords.add(list.get(i)); } //display the first word, after shuffling ofcourse final String theWord = threeWords.get(0); TextView theWordView = findViewById(R.id.the_word); theWordView.setText(theWord); fiveDefns.clear(); for (String word : threeWords) { fiveDefns.add(dictionary.get(word)); } //shuffle it again Collections.shuffle(fiveDefns); //make adapter to show three definitions on screen if (adapter == null) { adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fiveDefns); } else { adapter.notifyDataSetChanged(); } ListView listView = findViewById(R.id.word_list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //get the posiiton of definition user clicked from threeDefn arraylist String defnClicked = (String) fiveDefns.get(position); //want to know if it is right definition String rightAnswer = dictionary.get(theWord); if (defnClicked.equals(rightAnswer)) { //note that just 'this' will not work b/c we want to refer to over all activity //not this little activity, which is small activity inside overall activity Toast.makeText(MainActivity.this, "You are awesome", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "You Suck", Toast.LENGTH_SHORT).show(); } pickRandomWords(); } }); } }

如何使用java(android studio)读取文本文件? 我对android开发非常陌生,我一直在跟随斯坦福CS193A的讲座学习一些东西。在尝试创建字典时(不使用stand ford library),我觉得我做的大部分事情都是对的,但我无法在手机上启动应用程序。这里是我的java代码。请帮帮我。 package com.hfad.dictionarytwo; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.ListView; 导入android.widget.TextView; 导入android.widget.Toast; 导入androidx.appcompat.app.appcompat活动; 导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.HashMap; 导入java.util.Scanner; 公共类MainActivity扩展了AppCompatActivity{ 私有HashMap字典; 私有数组列表; 专用阵列适配器; 私人ArrayList fiveDefns; @凌驾 受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dictionary=newhashmap(); 列表=新的ArrayList(); fiveDefns=新的ArrayList(); readWordsFromFile(); 挑选单词(); } 私有void readWordsFromFile(){ Scanner scan=new Scanner(getResources().openRawResource(R.raw.grewords)); //以下去麻器用于在制表符处拆分 while(scan.hasNextLine()){ String line=scan.nextLine(); String[]parts=line.split(“\t”); 如果(零件长度>=2){ 字符串字=部分[0]; 字符串defn=零件[1]; 列表。添加(word); 字典.put(单词,defn); } } } 私有无效字(){ //下面的代码洗牌所有单词,并取前三个单词 ArrayList threeWords=新的ArrayList(); 集合。洗牌(列表); 对于(int i=0;i=1){ 字符串字=部分[0]; 字符串defn=零件[1]; 列表。添加(word); 字典.put(单词,defn); } } } 挑选单词(); } 私有无效字(){ //下面的代码洗牌所有单词,并取前三个单词 ArrayList threeWords=新的ArrayList(); 收藏 abcd kolkata valsad 123 auto vapi 789 neha palghar klj ma ghfgh mnk fgdgdf papa public class MainActivity extends AppCompatActivity { private HashMap<String, String> dictionary; private ArrayList<String> list; private ArrayAdapter<String> adapter; private ArrayList<String> fiveDefns; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dictionary = new HashMap<String,String>(); list = new ArrayList<String>(); fiveDefns = new ArrayList<String>(); readWordsFromFile(); } private void readWordsFromFile(){ Scanner scan = new Scanner(getResources().openRawResource(R.raw.grewords)); //following delimator is for spliting at tab while (scan.hasNextLine()){ String line = scan.nextLine(); if(line.contains(" ")) { String[] parts = line.split(" "); if (parts.length >= 1) { String word = parts[0]; String defn = parts[1]; list.add(word); dictionary.put(word, defn); } } } pickRandomWords(); } private void pickRandomWords() { //folowing code shuffles all the words and takes the first three words ArrayList<String> threeWords = new ArrayList<String>(); Collections.shuffle(list); for (int i = 0; i < 3; i++) { threeWords.add(list.get(i)); } //display the first word, after shuffling ofcourse final String theWord = threeWords.get(0); TextView theWordView = findViewById(R.id.the_word); theWordView.setText(theWord); fiveDefns.clear(); for (String word : threeWords) { fiveDefns.add(dictionary.get(word)); } //shuffle it again Collections.shuffle(fiveDefns); //make adapter to show three definitions on screen if (adapter == null) { adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fiveDefns); } else { adapter.notifyDataSetChanged(); } ListView listView = findViewById(R.id.word_list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //get the posiiton of definition user clicked from threeDefn arraylist String defnClicked = (String) fiveDefns.get(position); //want to know if it is right definition String rightAnswer = dictionary.get(theWord); if (defnClicked.equals(rightAnswer)) { //note that just 'this' will not work b/c we want to refer to over all activity //not this little activity, which is small activity inside overall activity Toast.makeText(MainActivity.this, "You are awesome", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "You Suck", Toast.LENGTH_SHORT).show(); } pickRandomWords(); } }); } },android,android-studio,Android,Android Studio,在您的活动中,在readWordsFromFile()中更改条件,并从readWordsFromFile()调用pickRandomWords()。 请参阅下面的代码 确保您的文件包含以下内容: I am very new to android development and I was following Stanford CS193A lectures to learn something. While trying to create dictionary(without using st

在您的活动中,在readWordsFromFile()中更改条件,并从readWordsFromFile()调用pickRandomWords()。 请参阅下面的代码

确保您的文件包含以下内容:

I am very new to android development and I was following Stanford CS193A lectures to learn something. While trying to create dictionary(without using stand-ford library), i feel like I did most of the things right but I am not able to launch app on phone. Here my java code. Please help me.

    package com.hfad.dictionarytwo;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import androidx.appcompat.app.AppCompatActivity;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Scanner;

    public class MainActivity extends AppCompatActivity {

        private HashMap<String, String> dictionary;
        private ArrayList<String> list;
        private ArrayAdapter<String> adapter;
        private ArrayList<String> fiveDefns;

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

            dictionary = new HashMap<String,String>();
            list = new ArrayList<String>();
            fiveDefns = new ArrayList<String>();
            readWordsFromFile();

            pickRandomWords();
        }

        private void readWordsFromFile(){
            Scanner scan = new Scanner(getResources().openRawResource(R.raw.grewords));
            //following delimator is for spliting at tab
            while (scan.hasNextLine()){
                String line = scan.nextLine();
                String[] parts = line.split("\t");
                if (parts.length >= 2){
                String word = parts[0];
                String defn = parts[1];
                list.add(word);
                dictionary.put(word,defn);
                }

            }
        }

        private void pickRandomWords() {
            //folowing code shuffles all the words and takes the first three words
            ArrayList<String> threeWords = new ArrayList<String>();
            Collections.shuffle(list);
            for (int i = 0; i < 5; i++) {
                threeWords.add(list.get(i));
            }
            //display the first word, after shuffling ofcourse
            final String theWord = threeWords.get(0);
            TextView theWordView = findViewById(R.id.the_word);
            theWordView.setText(theWord);

            fiveDefns.clear();
            for (String word : threeWords) {
                fiveDefns.add(dictionary.get(word));
            }

            //shuffle it again
            Collections.shuffle(fiveDefns);
            //make adapter to show three definitions on screen
            if (adapter == null) {
                adapter = new ArrayAdapter<>(this,
                        android.R.layout.simple_list_item_1, fiveDefns);
            } else {
                adapter.notifyDataSetChanged();

            }


            ListView listView = findViewById(R.id.word_list);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //get the posiiton of definition user clicked from threeDefn arraylist
                    String defnClicked = (String) fiveDefns.get(position);
                    //want to know if it is right definition
                    String rightAnswer = dictionary.get(theWord);
                    if (defnClicked.equals(rightAnswer)) {
                        //note that just 'this' will not work b/c we want to refer to over all activity
                        //not this little activity, which is small activity inside overall activity
                        Toast.makeText(MainActivity.this, "You are awesome", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "You Suck", Toast.LENGTH_SHORT).show();
                    }

                    pickRandomWords();
                }
            });
        }

    }
注意:文本文件中的数据应采用上述格式。因为您使用空格(“\t”)拆分单词

public类MainActivity扩展了AppCompatActivity{
私有HashMap字典;
私有数组列表;
专用阵列适配器;
私人ArrayList fiveDefns;
@凌驾
受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dictionary=newhashmap();
列表=新的ArrayList();
fiveDefns=新的ArrayList();
readWordsFromFile();
}
私有void readWordsFromFile(){
Scanner scan=new Scanner(getResources().openRawResource(R.raw.grewords));
//以下去麻器用于在制表符处拆分
while(scan.hasNextLine()){
String line=scan.nextLine();
如果(第行包含(“”)){
String[]parts=line.split(“”);
如果(零件长度>=1){
字符串字=部分[0];
字符串defn=零件[1];
列表。添加(word);
字典.put(单词,defn);
}
}
}
挑选单词();
}
私有无效字(){
//下面的代码洗牌所有单词,并取前三个单词
ArrayList threeWords=新的ArrayList();
收藏
abcd kolkata valsad
123 auto vapi
789 neha palghar
klj ma ghfgh
mnk fgdgdf papa
public class MainActivity extends AppCompatActivity {

        private HashMap<String, String> dictionary;
        private ArrayList<String> list;
        private ArrayAdapter<String> adapter;
        private ArrayList<String> fiveDefns;

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

            dictionary = new HashMap<String,String>();
            list = new ArrayList<String>();
            fiveDefns = new ArrayList<String>();
            readWordsFromFile();

        }

        private void readWordsFromFile(){
            Scanner scan = new Scanner(getResources().openRawResource(R.raw.grewords));
            //following delimator is for spliting at tab
            while (scan.hasNextLine()){
                String line = scan.nextLine();
                if(line.contains(" ")) {
                    String[] parts = line.split(" ");
                    if (parts.length >= 1) {
                        String word = parts[0];
                        String defn = parts[1];
                        list.add(word);
                        dictionary.put(word, defn);
                    }
                }

            }
            pickRandomWords();
        }

        private void pickRandomWords() {
            //folowing code shuffles all the words and takes the first three words
            ArrayList<String> threeWords = new ArrayList<String>();
            Collections.shuffle(list);
            for (int i = 0; i < 3; i++) {
                threeWords.add(list.get(i));
            }
            //display the first word, after shuffling ofcourse
            final String theWord = threeWords.get(0);
            TextView theWordView = findViewById(R.id.the_word);
            theWordView.setText(theWord);

            fiveDefns.clear();
            for (String word : threeWords) {
                fiveDefns.add(dictionary.get(word));
            }

            //shuffle it again
            Collections.shuffle(fiveDefns);
            //make adapter to show three definitions on screen
            if (adapter == null) {
                adapter = new ArrayAdapter<>(this,
                        android.R.layout.simple_list_item_1, fiveDefns);
            } else {
                adapter.notifyDataSetChanged();

            }


            ListView listView = findViewById(R.id.word_list);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //get the posiiton of definition user clicked from threeDefn arraylist
                    String defnClicked = (String) fiveDefns.get(position);
                    //want to know if it is right definition
                    String rightAnswer = dictionary.get(theWord);
                    if (defnClicked.equals(rightAnswer)) {
                        //note that just 'this' will not work b/c we want to refer to over all activity
                        //not this little activity, which is small activity inside overall activity
                        Toast.makeText(MainActivity.this, "You are awesome", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "You Suck", Toast.LENGTH_SHORT).show();
                    }

                    pickRandomWords();
                }
            });
        }

}