简体   繁体   中英

cannot convert from bufferedreader to string

i want to load file from assets folder and use a string at stopword procces, this ini my code but i get error "cannot convert from bufferedreader to string"

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

i get error in the line String preposisi = {test}

StringBuilder text = new StringBuilder();

AND

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

"test" or "text"???

text is a StringBuilder which is not compatible with a String array . Did you mean

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

or simply

String[] preposisi = { test }; 

?

  1. Change your read loop:

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

    Otherwise your StringBuilder will always be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM