Displaying only the part of the line of txt file - java

I have a txt file with 200.000 lines. I want to show in AutoCompleteTextView only city name and country. I have idea (Show only characters) How to do that?
Example of line: (4463523 Denver 35.531250 -81.029800 US).
In this example i want to show Denver and US.

Well, unfortunately, there will not be a good way except reading the whole file in (use, for example, a Scanner). Then you can, for example, store the parts you want to access quickly in a HashMap. Depends on your specific usecase and how much memory/cpu time you want to spend.

Related

is it possible to have java fill in the blanks on a word document

I am making a java program where I input answers for a friendship survey. It spits out the student's top ten friends. However I need to print out the results and give them to the students. The old of doing it was to have the java program write to write html then we would open each file one at a time and print out the page. However, having 400+ students to do it for takes a while.
So since I am re making the program I would like to make it so I can just have it on word files and print them all out at once. However, I don't know how to write to a word file and notepad isn't stylish enough. Anyone know how to make this possible or another way that is easier?
I did a similar thing some years ago, using Rich Text Format. Its advantage is that it's a plain text format that can easily be manipulated.
I created the form document in Word with some unique placeholder strings where I'd later fill in the actual data and saved it as RTF.
With a text editor, I made sure that Word didn't split the placeholders by inserting some junk formatting directives, and corrected that manually where necessary.
Then, filling in the actual data just meant to do some simple text replacement (in my case, there was no risk to interfere with the formatting directives), and saving the resulting RTF file.
As Word typically opens RTF files just as easy as DOC or DOCX ones, this was an easy working solution for me.

Can you translate data pulled with NFC to a different language?

Is there a way for the data I read from a NFC tag to then be able to translate that data into a choice of my own? For example, I scan a tag with direction data on it. How can I choose which language I wish to read that data in?
NFC tags don't have much memory. And the ones that do have more memory are much more expensive.
And so sometimes data is represented with a short hexadecimal number. In the case of transit data for instance, in San Francisco this application called Farebot will be able to tell you which Bart Station you got in and which Bart Station you exited (BART stands for Bay Area Rapid Transit, it's a type of medium-range subway train that connects multiple cities within the Bay Area).
But instead of storing "16th Street Mission" inside the transit card, it might only store a shorter hexadecimal number like B2
And inside the Farebot application, there would already be an internal table that knows that B2 is equivalent to "16th Street Mission" for instance, but depending on the language setting of your app, there is no reason that you couldn't have a French table that tells you that B2 corresponds to "16eme Rue Mission" also.
Does my explanation make sense? Unfortunately, with the limited memory of NFC tags, you can't store too much data within the tag/card itself, and so storing multiple translations inside the tag itself wouldn't make a lot of sense.
It's the case that NFC tags can only hold limited amounts of data (think very small chunks of data) exactly as Stephan above said. To see you an example of another app made using NFC tags, kindly go to this repo and see the MainActivity, more precisely what's inside the if-statement starting from line 114 (I would've pasted the code, but it's not formatted properly). What it does is it gets the data from the tag and esentially translates it into a string that, if I remember well, was only something like (LR1, LR2, LR3...) and so on. How you "interpret" this tiny bit of information should be entirely on your app's backend code (e.g. we knew that LR1 was lecture room 1 so we had an idea of what to do).
What I'm trying to say is that you can encode hexadecimal strings of limited length on the NFC and then convert it back to whatever data structure you want to use, but the logic and interpretation take place on your app's side. The repo above shows an example on how to do this in java.
If I understand your question fine.
** NFC tags have very little memory, so you might have to build a dictionary in your application
For ex:
A1 is mapped to Right
B1 is mapped to Left. etc
Then you store A1 or B1 for example on your tags.
Then what you need to do is, when you resolve the A1 to Right after that you need to call a Translate API like
Google translate
Bing FREE

How can I change written data in different positions of a file?

I am writing some information in a file and I want to update some part of this information. For instance:
If we assume the current context of the file is following sentences:
This cake is made by Mary. 12
Students play football.12908
She is a teacher.546
Then I want to change 12908 to 765 in that file and write two new lines.The context of the file after changes would be like this:
This cake is made by Mary. 12
Students play football. 765
She is a teacher.546
I love my mother.
The sky is bule. 897
I want to update information many times in a file. How can I do this?
If we are talking about reading and writing so few lines you can use java's class RandomAccessFile which fits exactly to your needs, but by experience i can tell that if you need to deal with a considerable amount of data IO, this is very inefficient . In that case as suggested by alfasin: "Start by reading the file line-by-line and writing each line (or corrected line) into another file, and take it from there".
Here's a tutorial on how to use RandomAccessFile.

Regex: Negating a whole word (needed to optimize a file)

I am trying to do a simple weather widget for Android, that provides temperatures just for my country (Jordan). The website I am using for the weather records provides a JSON file with country regions data for many countries. The problem is that the file contains 2500+ objects, and it takes a really long time to be parsed. Thus, and as I actually need <100 of them (the regions of my country), I thought that I could optimize the file before passing it to the JSON parser, by taking off all of the records I don't need. I don't know if it's a good solution, but it was what I thought of. Anyway, my problem now is getting the right Regex.
This is the URL of the JSON file.
As you can see, every object has four items. The one I need to check for is "icon", which specifies the country of that region.
EXAMPLE:
{"value":"khalda","icon":"Jordan","label":"khalda","desc":"Amman & Madaba"},
What I could came up with so far is the pattern of the object I actually need. However, I need to get the ones I don't need to be able to delete them. Here is the pattern: \{[^\{]*Jordan*[^\}]*\}, (This has to be modified so it validates when "Jordan" does NOT exist, which I couldn't figure out.)
Any help/hint is highly appreciated.
Thanks.
Rather than matching and deleting the objects you don't need, match and extract the single(?) object that you do need. It will be faster.
(And I agree with minitech's comment. Parsing the JSON file is unlikely to be the real bottleneck.)

Writing one file per group in Pig Latin

The Problem:
I have numerous files that contain Apache web server log entries. Those entries are not in date time order and are scattered across the files. I am trying to use Pig to read a day's worth of files, group and order the log entries by date time, then write them to files named for the day and hour of the entries it contains.
Setup:
Once I have imported my files, I am using Regex to get the date field, then I am truncating it to hour. This produces a set that has the record in one field, and the date truncated to hour in another. From here I am grouping on the date-hour field.
First Attempt:
My first thought was to use the STORE command while iterating through my groups using a FOREACH and quickly found out that is not cool with Pig.
Second Attempt:
My second try was to use the MultiStorage() method in the piggybank which worked great until I looked at the file. The problem is that MulitStorage wants to write all fields to the file, including the field I used to group on. What I really want is just the original record written to the file.
The Question:
So...am I using Pig for something it is not intended for, or is there a better way for me to approach this problem using Pig? Now that I have this question out there, I will work on a simple code example to further explain my problem. Once I have it, I will post it here. Thanks in advance.
Out of the box, Pig doesn't have a lot of functionality. It does the basic stuff, but more times than not I find myself having to write custom UDFs or load/store funcs to get form 95% of the way there to 100% of the way there. I usually find it worth it since just writing a small store function is a lot less Java than a whole MapReduce program.
Your second attempt is really close to what I would do. You should either copy/paste the source code for MultiStorage or use inheritance as a starting point. Then, modify the putNext method to strip out the group value, but still write to that file. Unfortunately, Tuple doesn't have a remove or delete method, so you'll have to rewrite the entire tuple. Or, if all you have is the original string, just pull that out and output that wrapped in a Tuple.
Some general documentation on writing Load/Store functions in case you need a bit more help: http://pig.apache.org/docs/r0.10.0/udf.html#load-store-functions

Categories

Resources