Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,890 members, 7,802,864 topics. Date: Friday, 19 April 2024 at 11:58 PM

Retrieving Text From Data File. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Retrieving Text From Data File. (1340 Views)

Retrieving The COM Class Factory For Component With CLSID {587CCA26-D3FA-402C-B5A0-259DF7E96D55} Failed Due To The Following Error: 80040154. / Retrieving/selecting A Particular Number Of Random Rows From A Database Table / Inserting And Retrieving Images To/From Database In ASP.NET (2) (3) (4)

(1) (Reply) (Go Down)

Retrieving Text From Data File. by Danyl(m): 10:38am On Nov 12, 2011
Helo house,i have a data file named text.db and i want to retrieve the text stored in the data file and display the lines and filter. How can I go about this.
Re: Retrieving Text From Data File. by Fayimora(m): 10:42am On Nov 12, 2011
Using what language? How is the data arranged? Is it a table or just some lines of information?
Re: Retrieving Text From Data File. by Danyl(m): 2:17pm On Nov 12, 2011
Using Java,am trying to retrieve the lines of text and display them one after the other on a GUI.
Its arranged according to the format below
query:options:correct-response-to-query
Re: Retrieving Text From Data File. by Fayimora(m): 11:49pm On Nov 12, 2011
Danyl:

Using Java,am trying to retrieve the lines of text and display them one after the other on a GUI.
Its arranged according to the format below
query:options:correct-response-to-query
Well displaying them I shall leave for you to handle. Lets work through the format of the file.
You have a query  and then option separated by :
You the have : and then another string 'correct response to query'

Just to be sure, an example would be:
SELECT * FROM 'blog_posts' WHERE`ID`=45:LIMIT 0,30:Yes the posts exists

Am i correct? If No then pls provide an example. The reason why I want an example is because the easiest and most reasonable way to tackle this is with a regular expression. I can provide that for you.

Another way would be to use the split function in the String class.

The way it works is, you take a String and split with a delimiter. The result of the split gets stored in an array.

For instance:
String line = "query:options:correct-response-to-query";
String[] values = line.split(":+");

Here are the elements of values:
values[0] = query
values[1] = options
values[2] = correct-response-to-query


Note that I assume you know how to read from a file. If you cant then this would be of help.
Scanner reader = new  Scanner(new FileReader("filename.db")); //open the file and get ready to read it.
while(reader.hasNextLine()){ //loop while you still have a line in the file.
     String line = reader.nextLine(); //read in a line into the string
   
    // process each line here
}
Re: Retrieving Text From Data File. by Danyl(m): 2:26pm On Nov 13, 2011
Thanx fayimora 4ur response,i tried to use the Scanner class readline to display a text.txt file,n display it on a text area component on a simple GUI. But i want to deal with a .db file with d folowing format;
1. what is your age?:23~22~32~40:40.
2. What is ur birth year?:1980~1981~1982:1981.
I want to retrieve the questions,store them in a String object and d options and answers into an int primitive type and manipulate them on the GUI,I Can handle the GUI bt the retrieval in order is the issue am trying to solve. Thanx
Re: Retrieving Text From Data File. by Fayimora(m): 2:51pm On Nov 13, 2011
Ok cool so use what i have written above. As long as you know the format of the file, the extension doesn't stop you from accessing it like any other file.However, you would add some extra bits . . . .
Remember that values[1] = options;

However, 'options' also contain something else that you need to split up,  Try writing just a line of code that gives you an array of options, so if you have

String [] options;
options[0] = 23;
options[1]  = 22;
options[2] = 32;
options[3] = 40;

and then you can use your values there.
The previous code i wrote would be of help,  Try it out!
Re: Retrieving Text From Data File. by Danyl(m): 8:38pm On Nov 13, 2011
Thnx alot fayimora,4 your responses.,what about the delimiter "~" i hope it doesnt .the code doesnt have to be changed due to that
Re: Retrieving Text From Data File. by Fayimora(m): 10:34pm On Nov 13, 2011
Yes your delimiter would now be '~', however if you noticed I used the wildcard '+'. It just means 1 or more. So it takes care of a situation where you have more than 1 '~'. FOr instance '23~24~~27'. Let me know if you have any problems.
Re: Retrieving Text From Data File. by Danyl(m): 8:12am On Nov 14, 2011
It worked out,when i retrieved the options with the split(),i had to further split the options into As,Bs and Cs. String[] options=lines[1].split("~+"wink; (where line[1] is the array location for the options retrieved frm the lines in the text file) to remove the delimiter and extract the options.
Options[0] is the array location for options A,
options[1] for Bs.
Here is the output from the console:
A.ayo
A.1985
A.20 for the As.
I want to split the As into single options, i tried to use the split("\n"wink function bt it didnt work how do i work this out if its possible.
Re: Retrieving Text From Data File. by Fayimora(m): 11:19am On Nov 14, 2011
I don't think i ununderstand wat you want to do. What As are you talk about?
Re: Retrieving Text From Data File. by Danyl(m): 12:43pm On Nov 14, 2011
From d text file file.db
the text line is
1. What is ur age?:A.23~B.24~C.25:A.22
So am tlkn abt the A(s) options. For each line of text.
Re: Retrieving Text From Data File. by Fayimora(m): 4:41pm On Nov 14, 2011
Well split it with .
Re: Retrieving Text From Data File. by Danyl(m): 5:30pm On Nov 14, 2011
Thanx alot i got it,
Re: Retrieving Text From Data File. by Fayimora(m): 1:46pm On Nov 16, 2011
Anytime
Re: Retrieving Text From Data File. by gigabytes: 6:48pm On Nov 17, 2011
I have openned a .db file(s) using common notepad. You can try it. it works on win 7

(1) (Reply)

OPEN SOURCE Has Been Evil Too! / Program Mentorship For Those Programmers Who Are Motivated And Want To Improve / I Need Help With The Dark Web

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.