Unable to split mix latin & arabic string from a file in android - android

Need to parse this file (mixed latin & arabic):
1|حِيمِ
2|الَمِينَ
The file was saved as UTF8 in notepad++, and put in android asset folder.
Expected result: for line1, the entries are "1" and "حِيمِ" (split by "|").
AssetManager manager = context.getAssets();
InputStream inStream = null;
inStream = manager.open("file.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line = "";
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
if (columns.length>=3) {
Toast toast = Toast.makeText(context, "Line: " + columns[0] + " and " + columns[1], Toast.LENGTH_LONG);
toast.show();
}
}
Actual Result:
columns[0] = "1" ok, but
columns[1] = "%D8%AD%D9..." not Ok, expected "حِيمِ".
How to fix this, or is there better way? Please help. Thanks in advance.

Solved, changing:
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
into
while ((line = buffer.readLine()) != null) {
String[] columns = line.split("\\|");

Related

Comparing Saved TXT and EditText text

I Want to Make A System that compares password (4 letter numeric).
Input Code (I Already Have fis)
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while((text = br.readLine()) != null)
{
sb.append(text).append("\n");
}
String finalinput = sb.toString();
String finalpassinput = password.getText().toString();
Toast.makeText(this, "finalinput:"+finalinput+"finalpassinput:"+finalpassinput,Toast.LENGTH_LONG).show();
if(finalinput.equals(finalpassinput))
{
Toast.makeText(this,"Login!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, HomeActivity.class));
}
else
{
Toast.makeText(this,"Incorrect Password",Toast.LENGTH_SHORT).show();
}
And This Source Code will keep say that the two passwords are different.
How I Wrote My File :
fos = openFileOutput(FILE_NAME_PW, MODE_PRIVATE);
fos.write(encodedpw.getBytes());
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME_PW, Toast.LENGTH_LONG).show();
I am curious if encodepw.getBytes() will change anything to the string (Including null characters, etc.) and if Java won't think them the same.
Thank you.
I found myself the answer to it. The finalinput variable had some null charachter before it, so you can get pure string using this function.
public static String FileStringParse(String FileString)
{
FileString = FileString.replaceAll("\\D+","");
return FileString;
}

Android open UTF-8 XML

Hello I get some xml file
They are on UTF-8 so i follow some sample and my code look like this
String text = "";
String str;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(Path), "UTF-8"));
while ((str = in.readLine()) != null) {
text += str;
}
return text;
And then i try to parse the code with the dom parser
Document doc = parser.getDomElement(result);
And this fail
I have check my xml file with a hexeditor
I have the following charcode before "<": ef bb bf
What have i miss? why getDomElement tell me
Unexpected token (position:TEXT #1:2)
text += str + "\n";
If there was a line break in a tag:
<img
src="smile.jpg"/>
you could get:
<imgsrc="smile.jpg">
And some other cases.
StringBuilder text = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(Path), "UTF-8"))) {
String str;
while ((str = in.readLine()) != null) {
text.append(str).append("\n");
}
} // Does an in.close()
return text.toString();

only last entry is shown in csv file during reading?

i am reading a file using the following code
`FileReader fr=new FileReader("/mnt/sdcard/content.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];`
and i have also tried using the opencsv class but with both the method i am only able to read the last entry in the file..
what am i missing?can someone explain to me?
FileReader fr=new FileReader("/mnt/sdcard/playlist_record.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];
type= RowData[2];
update= RowData[3];
server= RowData[4];
t1.setText(id);
// t1.append(path);
// t1.append(type);
// t1.append(server);
}
in.close();
this is my code i am using to read the file

Extracting words from a website

Hi I want to try making a simple application for android phones for which I will be requiring a dictionary. I thought of using urbandictionary.com as the reference site. Is there any technique by which I can extract all the words with the definitions and their respective words in the thesaurus ?
I was checking out the Google example found at
http://developer.android.com/resources/samples/SearchableDictionary/index.html
It appears that they just add their words with this example
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2) continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
However, when I look for R.raw.definitions that directory is empty.
http://developer.android.com/resources/samples/SearchableDictionary/res/raw/index.html

Android URL Content to String

I would like to connect to a Website, filter some content and then put it in a String but I don´t know how to do this.
public void zahlenLaden (View view) throws Exception {
URL oracle = new URL("http://www.blabla.de");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//What I have to write here?
}
Declare a String to output to before the while loop:
String output = "";
Then just append to that String in each iteration:
output += inputLine + "\n"; (don't forget the omitted newline)
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine + "\n");
}
then just do sp.toString();
Nikola's answer is OK, just an improvement on the use of StringBuilder:
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\n");
}

Categories

Resources