What's the best way to split this text file? - java

basically i have this java assignment in which I need to take the following text file:
https://drive.google.com/file/d/1NqWtApSHovOfSXzVCeU_GPtsCo_m6ZtJ/view?usp=sharing
The legend for the text file is here:
E: did the elector result in promotion (1) or not (0)
T: time election was closed
U: user id (and screen name) of editor that is being considered
for promotion
N: user id (and screen name) of the nominator
V: vote(1:support, 0:neutral, -1:oppose), user id, time, screen_name
And create the following two methods:
Given a user id, output the total number of times the user has voted to support or be neutral or oppose the candidate considered for promotion
for all people the user has voted for collectively in all the elections.
Given a user id considered for promotion, output the user id and
screen name of the nominator. For multiple nominations, you will
output all nominators. If the user is not nominated ever, output
an appropriate message.
I'm really lost on how I should go about splitting the text file into pieces to help me obtain the information needed to create the two methods. Any insight would really be great

Basically you need to create different string reader loops that iterate over the text file line by line. Nobody here will do this for you because it´s to much work, and it´s your work!
Here´s a discussion about iterating-over-the-content-of-a-text

You can use the str.split() method. You write the point in the text at which you would like to split in the brackets.
For example if you have the following;
String str "Hello world";
String [] arrayString = str.split (" ");// splits the string at the space
for( String a:arrayString);
System.out.println(a);
Output:
Hello
World

Related

A pair of strings as a KEY in reduce function - HADOOP

Hello I am implementing a facebook-like program in java using hadoop framework (I am new to this). The main idea is that I have an input .txt file like this:
Christina Bill,James,Nick,Jessica
James Christina,Mary,Toby,Nick
...
The 1st is the user and the comma separated are his friends.
In the map function I scan each line of the file and emit the user with each one of his friends like
Christina Bill
Christina James
which will be converted to (Christina,[Bill,James,..])...
BUT in the description of my assignment it specifies that the Reduce function will receive as key the tuple of
two users, following by both their friends, you will count the
common ones and if that number is equal or greater than a
set number, like 5, you can safely assume that their
uncommon friends can be suggested. So how exactly do I pass a pair of users to the reduce function. I thought the input of the reduce function has to be the same as the output of the map function. I started coding this but I don't think this is the right approach. Any ideas?
public class ReduceFunction<KEY> extends Reducer<KEY,Text,KEY,Text> {
private Text suggestedFriend = new Text();
public void reduce(KEY key1,KEY key2, Iterable<Text> value1,Iterable<Text> value2,Context context){
}}
The output of the map phase should, indeed, be of the same type as the input of the reduce phase. This means that, if there is a requirement for the input of the reduce phase, you have to change your mapper.
The idea is simple:
map(user u,friends F):
for each f in F do
emit (u-f, F\f)
reduce(userPair u1-u2, friends F1,F2):
#commonFriends = |F1 intersection F2|
To implement this logic, you can just use a Text key, in which you concatenate the names of the users, using, e.g., the '-' character between them.
Note that in each reduce method, you will only receive two lists of friends, assuming that each user appears once in your input data. Then, you only have to compare the two lists for common names of friends.
Check if you can implement custom record reader, read two records at once from input file in mapper class. And then emit context.write(outkey, NullWritable.get()); from mapper class. Now in reducer class you need to handle two records came as a key(outkey) from mapper class. Good luck !

Check if array contains string

Ok, I have this program that is supposed to store information about CDs that are available in txt file. The file stores the data in format 'artist[tab]album' on the same line. What I want to do, is to have user input search query, and the program return if the CD is in the database. So let's say we have Green Day[tab]American Idiot in .txt file on some line, and when users types in Green, the programs checks that file and returns true. But my problem is, my current algorithm requires the string to completely match, instead of partial. So the users needs to type Green Day[tab]American Idiot to get true on the query. How to fix it? Thanks. I am sure it is something I don't see as beginner.
This is the part of the program that manages the search in the array Artists, that contains all the data currently stored in the .txt file
for (String e : artists){
if(Arrays.asList(e).contains(search)){
contains=true;
}
Why are you creating a list? You should use artist.contains(search) (yes, try to choose more relevant variable names). Also make sure you don't have null elements in the array or search is not null etc, but you can do something like:
for(String artist : artists) {
if(artist.toLowerCase().contains(search.toLowerCase()) {
contains = true;
// break; <- you may want to break here
}
}
You may want to toLowerCase() both of them for case-insensitive search.

can i use the text from a pastie to extract some values

i am a newbie to flash builder! i am trying to create an app for IOS, on my school project. the problem that i faced is: can i select a few items value from a text input field and to implement them in a new view where i use some formulas! text input will look like this example :
aaa 23
bbb 45
ccc 56
i need the aaa and ccc that i want to use on my formulas!
about my first question! on my main view i have 2 buttons : 1 - Manual 2 - Auto . on manual view i have fields aka: aaa = ccc= ... and so on. in auto mode i was thinking of auto input method like this : to select the needed values for formulas from a text input area where the user will paste the text, for avoiding the manual input on each field.
PS:and can someone to tell me how correctly implement math simple formulas in flash builder, here i mean the syntax . like textinput1+textinput2/textinput3!?
From what I understand, you have two questions...
First, it doesn't quite make sense to me but I think you are asking if you can do math on letters??? No. But maybe I am just not understanding the question.
Second, you want to know how to do the math in ActionScript. Its fairly simple. Basic math functions are just like any other language. Take the string, convert it to a number then do your basic math:
Number(textInput1) + Number(textInput2) = result;
Using Number() will convert your string to a number so you can perform the math. Then, to display it, just convert your result back to a string:
textResultField.text = result.toString();
Or you could get fancy (AKA: harder to modify later) and do it all on one line:
textResultField.text = (Number(textInput1) + Number(textInput2)).toString();
Again, this will work for all BASIC math function (+, -, *, /). Really depends on what you are trying to do.

cleanly lining up output

I have a text file that contains data that was saved using the cvs encryption and I want to open it in java and display it lined up perfectly. I have come to the point of reading it from the text file but now I want it to be split at the commas and now I want to display it all perfectly aligned.
Last, First, car year, car model
barry, john, 1956, chevy impala
and I want it to display like this:
last First car year car model
barry john 1956 chevy impala
and I am just using the scanner class to get the data from the text file.
Determine the max lengths of the column values (including column headers), then create a format String and use that format string to build the aligned rows:
// some easy magic first
String[][] values = getCsvValues(file);
int[] maxLengths = determineMaxLengths(values);
// create formatstring, something like "%10s %5s %10s %n"
StringBuilder formatBuilder = new StringBuilder();
for (int maxLength:maxLengths)
formatBuilder.append("%").append(maxLength).append("s ");
formatBuilder.append("%n"); // newline
// output
for (String[] row:values)
System.out.printf(formatBuilder.toString, row);
depending on how certain you need to be that it all lines up, you might have to go through and find the longest string for each column, but I would use tabs: "\t". Two or three between columns usually works for me, for simple debugging that's what I use.
If you're really serious about it being right all the way down, try looking at Formatters and printf.

How to read single question from text file containing several question and options as well as information?

I have a Text file which contains data (4 to 5 paragraph) and then contain 100 question along with each containing four option (a,b,c,d). Later it contain answer for above question (1.a,2.b and so on). My problem is I want to read each time a single question and options to display. Here when the user selects the option I want to check whether he choose the right one or not. Similarly I want to do this for all questions. And at last I want to display how many question are correct and how many are false.
My question is, how can I get each question and option separately? Similarly the answer. How to get information present in text file separately when required?
my file
Section I – General Questions
Instructions: choose the most correct answer.
1. The International Labour Organization’s first Maternity Protection Convention was issued in:
A.) 1952.
B.) 2000.
C.) 1919.
D.) 1997.
2. Beyond the 5th month of pregnancy, noise in excess of _______________________ may cause
hearing loss in the fetus:
A.) 90 dBA TWA per OSHA requirements
B.) 155 dBC peak and 115 dBC TWA per ACGIH® TLV® Booklet notes
C.) 75 dBA TWA per American Academy of Pediatrics guidelines
D.) 65 dBC peak per EPA studies
ANSWERS – Section I General Questions
1. C
2. B
I wouldn't worry about the format of the file at this point too much. Design your objects (presumably things like Question, Answer, Student etc etc). Get that all working with some test data and then worry about parsing the file. Your program objects don't need to know what the file will look like.
I imagine this text file uses some kind of delimiters to indicate the end of onw questions and the beginning of the next one. If so, I suggest:
Open the text file at the beginning of your program.
When you need to display the first question, read until the first delimiter (the one that occurs between the end of the 1st question and the beginning of the 2nd question).
When you have to show the next question, start from your current position (at the end of question #1), and read until the next delimiter (which would occur after question #2).
The answers can also be read similarly. If you have a special delimiter (say, an XML tag) which indicates the start of the answers, skip ahead up to that position. Then start reading the entries one-by-one, and parsing them.
Sorry for the somewhat vague answer :) If you could be more specific about your requirements, and the delimiters, etc., we can provide more detailed answers.
Use regular expression.
I do not know exact format of your file. I is easier if question can be recognized somehow. For example if the file has format like this:
This is the first question
a. aaaaa
b. bbbbb
c. ccccc
1.b
This is the second question
a. aaaaa
b. bbbbb
c. ccccc
2.c
You can write expression like this:
Pattern p = Pattern.compile("^\\d+\\.\\s+(.+?)(?:[abc]\\.\\s+(.+)){3}", Pattern.MULTILINE)
Now just iterate over the pattern:
Matcher m = p.matcher(text);
while (m.find()) {
String questionNumber = m.group(1);
String questionText = m.group(2);
String answerA = m.group(3);
String answerB = m.group(4);
String answerC = m.group(5);
// your code
}
Note that I have not debugged this pattern, so it obviously does not work. But I believe it gives you enough tips, so it is a good start.

Categories

Resources