2014-01-26 36 views
0

我想從資產文件夾中加載文件,並使用在停用詞procces一個字符串,這個INI我的代碼,但我得到錯誤「無法從BufferedReader中轉換爲字符串」不能從BufferedReader中轉換爲字符串

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if (v.getId()==R.id.button6){ 

     StringBuilder text = new StringBuilder(); 

     try { 
      BufferedReader reader = new BufferedReader(
       new InputStreamReader(getAssets().open("Preposisi.txt"), "UTF-8")); 

      // do reading, usually loop until end of file reading 
      String mLine = reader.readLine(); 
      while (mLine != null) { 
       //process line 

       mLine = reader.readLine(); 
      } 

      reader.close(); 
     } catch (IOException e) { 
      //log the exception 
     } 

     Intent intent = new Intent(this, PreposisiRemoval.class); 
     String test = ((TextView)findViewById(R.id.textView7)).getText().toString(); 
     String[] preposisi = {text}; "GET ERROR IN THIS LINE" 
     StringBuilder resultFilter = new StringBuilder(); 

     Scanner fip1 = new Scanner(test); 
      while (fip1.hasNext()){ 
        int flag = 1; 
        String s1 = fip1.next(); 
        for (int i = 0;i<preposisi.length; i++){ 
         if (s1.equals(preposisi[i])){ 
          flag=0; 
         } 
         } 
         if(flag!=0){ 
          System.out.println(s1); 
          resultFilter.append(s1+'\n'); 
         } 

         Intent intent2 = getIntent(); 
           String count = intent2.getStringExtra("result2"); 

        intent.putExtra("resultFilter", resultFilter.toString()); 
        intent.putExtra("count", count); 
        startActivity(intent); 
        } 
     } 
} 

我得到誤差在該行字符串preposisi = {}測試

+0

其中你正在從bufferedreader轉換到字符串的行 –

+0

你想在行'String [] preposisi = {text};'中做什麼? –

回答

0
StringBuilder text = new StringBuilder(); 

AND

String test = ((TextView)findViewById(R.id.textView7)).getText().toString(); 
String[] preposisi = {text}; "GET ERROR IN THIS LINE"; 

「測試」 或「特噸「???

+0

文本(這是從資產文件夾加載的文件) – fazhar

+0

文本是StringBuilder的一個實例,您正試圖將其轉換爲字符串。 String [] preposisi = {test};我認爲如此 – Luc

+0

我得到錯誤「不能從bufferedreader轉換爲字符串」如果使用String [] preposisi = {test}; – fazhar

0

textStringBuilder,它與String陣列不兼容。您的意思是

StringBuilder text = new StringBuilder(); 
String[] preposisi = {text.toString()}; 

或者乾脆

String[] preposisi = { test }; 

0
  1. 更改您的讀取循環:

    String mLine = reader.readLine(); 
    while (mLine != null) { 
        mLine = reader.readLine(); 
        text.append(mLine); //You missed this line 
    } 
    

    否則你的StringBuilder的將永遠是空的。