0

課題用に簡単な Java CLI プログラムを作成しましたが、今はそれを楽しみ/体験用の Android アプリにしようとしています。しかし、それは機能していません...これは翻訳アプリであり、辞書に載っていることがわかっている単語を入力すると、ユーザーが無効な単語を入力した場合に入力したメッセージが表示されます. Eclipse の LogCat が実行時に次のメッセージを表示しているため、ディクショナリ ファイルを適切に読み取っていないことが原因だと思います。

public class MainActivity extends Activity {
TreeMap<String, String> tree = new TreeMap<String, String>();
public final static String EXTRA_MESSAGE = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Scanner dictinput = null;
 try{
     getAssets().open("dict.txt");
    dictinput = new Scanner(getAssets().open("dict.txt"));
 }
 catch (Exception e) {
        System.err.println("Could not open file");
        System.exit(-1);            

 }//Puts the dictionary in a stringbuffer
 StringBuffer dict = new StringBuffer();
 String[] arrtemp = new String[1600];
 while(dictinput.hasNext()) {
     dict.append(dictinput.nextLine());
     dict.append("|");

 }//Then puts it in a string so it can be split at the |'s using .split
 String strstr = dict.toString();
 arrtemp = strstr.split("\\|");

 //puts the dictionary into a treemap
 for(int i=0; i<arrtemp.length-1; i++) {
     if(i==0)
         tree.put(arrtemp[i].toLowerCase(), arrtemp[i+1]);
     else {
         i++;
         tree.put(arrtemp[i].toLowerCase(), arrtemp[i+1]);
     }

 }
}

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);

    EditText editText = (EditText) findViewById(R.id.editmessage);//The only way I've         found to get the text from the user.
    String temp = editText.toString();
    Scanner scan = new Scanner(temp);
    String finalmessage = null;
    while(scan.hasNext()){

     String temp1 = scan.next();
     if (tree.containsKey(temp1.toLowerCase()))
        finalmessage = temp;
     else
         finalmessage = "No match found, try another word or phrase";
 }



    intent.putExtra(EXTRA_MESSAGE, finalmessage);
    startActivity(intent);
}

どんな助けでも大歓迎です。

4

1 に答える 1

1

テキストファイルはどこにありますか? 問題は、プロジェクトフォルダーの /res/raw の下に保存されていない可能性があると思います。その後、次のような入力ストリームを使用できます。

        InputStream is = this.getResources().openRawResource(
            R.raw.textfilename);
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while(blah){
           do stuff
        }
    }
    catch(Exception e){
        handle errors
    }
于 2013-04-11T23:54:53.113 に答える