字符串从android中的文件进行标记(String Tokenizing from file in android)

编程入门 行业动态 更新时间:2024-10-28 03:30:23
字符串从android中的文件进行标记(String Tokenizing from file in android)

我有一个存储在手机“SaveTT.txt”中的文件。 文件中的每个单词都用空格分隔。 我希望从文件中检索每个单词并在单独的textViews中显示每个单词。 这个怎么做。 请帮忙

我能够使用以下代码将文件的内容检索为字符串

try { BufferedReader inputReader = new BufferedReader(new InputStreamReader( openFileInput("SAVETT.txt"))); StringBuffer stringBuffer = new StringBuffer(); while ((inputString = inputReader.readLine()) != null) { stringBuffer.append(inputString + "\n"); //EditText txt = (EditText) findViewById(R.id.temp); //txt.setText(inputString); } } catch (IOException e) { e.printStackTrace(); }

使用此代码在此之后将整个字符串输入inputString。 我使用以下代码对字符串进行标记

StringTokenizer tokenizer = new StringTokenizer(inputString); String[] arr = new String[tokenizer.countTokens()]; while(tokenizer.hasMoreElements()) { arr[i]= tokenizer.nextToken(); i++; }

使用上面的代码我试图在数组中保存eack令牌。 接下来,我尝试在Textviews中显示文本。

我不知道我的错误在哪里。 活动停止并显示NullPointer异常。

I have a file stored in my phone "SaveTT.txt" . Every word in the file is seperated by spaces. i wish to retrieve each word from the file and display every word in seperate textViews. How to do this. please help

I am able to retrieve the contents of the file into a string with the following code

try { BufferedReader inputReader = new BufferedReader(new InputStreamReader( openFileInput("SAVETT.txt"))); StringBuffer stringBuffer = new StringBuffer(); while ((inputString = inputReader.readLine()) != null) { stringBuffer.append(inputString + "\n"); //EditText txt = (EditText) findViewById(R.id.temp); //txt.setText(inputString); } } catch (IOException e) { e.printStackTrace(); }

with this code get the entire string into inputString after this . i am tokenizing the string with the following code

StringTokenizer tokenizer = new StringTokenizer(inputString); String[] arr = new String[tokenizer.countTokens()]; while(tokenizer.hasMoreElements()) { arr[i]= tokenizer.nextToken(); i++; }

with the above code i am trying to save eack token in an array. Next I try to display the text in Textviews.

I dont know where i am goig wrong. the activity is stopped and a NullPointer exception is displayed.

最满意答案

我自己想出了问题。 问题是inputString的值在Try catch块之外是不可访问的,因为它是在块内部设置的;

所以,如果我在try catch中编写字符串标记化块,它可以正常工作

I Figured out the problem myself. The problem was that the value of inputString is inaccessible from outside the Try catch block since it is set inside the block;

so if i write the string tokenisation block inside try catch it works perfectly fine

更多推荐

本文发布于:2023-08-03 09:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   标记   文件   android   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!