Reading fixed width text file with different length [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a text file that is in fixed width format, except each column has a different length. I have about 100 columns in all.
For example, the first few columns of text have the following width:
30
4
188
Let's assume I read in the first row from the file. How do I seperate/map the text into 100 different size columns?

We just dealt with this at work within the last few weeks. The way we went about solving the problem was to create an enum class with corresponding "indexes" to represent the start and end positions of the fields that needed to be extracted. This enum is loaded into a map of FIELD_NAME --> RANGE (i.e. 0:8) upon instantiation of the class that parses the message.
High-level, upon receipt of a message on the queue:
convert TextMessage to string
read line
for each field, get the corresponding range from the map
split the range on ":" to get the indexes
extract the values from the String using substring(index1,index2)
perform transformations (string to date, string to numbers, etc)
persist to database

Related

How to get average in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to get average using functional programming in java?
This is what I tried ...
It seems like its not working at IntStream.of
I would like to get average from a specific row of the array
public static void average(List<List<String>> rows){
IntStream stream = IntStream.of(e -> Integer.parseInt(e.get(2)));
OptionalDouble obj = stream.average();
if (obj.isPresent()) {
System.out.println(obj.getAsDouble());
}
else {
System.out.println("-1");
}
}
rows is the array are rows read from an excel file.
Stream.of(elem1, elem2) creates a stream with the stated elements.
Imagine you have a box with 100 fotos in it.
If you do Stream.of(box), you get a stream of boxes, returning 1 box.
What you wanted was a stream of fotos. To get that, you want box.stream(), not Stream.of(box).
Your next problem then is that you don't seem to understand what reduce does. You need to tell the system how to integrate two results, not just how to get a result.
What you want here isn't reducing in the first place, you want to map a given 'foto' (a List of string in your case) to an integer, which requires not just e.get(), but also an Integer.parseInt, and you want map, not reduce.

How to generate unique alphanumeric ID 11 characters long in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to work on a framework for Unique ID generation. Currently, a reference number is 11 characters with 5 characters Julian date, 6th character specific to a datacenter and other 5 - uniquely generated sequence of alpha characters. This make the algorithm to generate 20 million unique records.
I do not want UUID format. Need a more readable format potentially with composition of a date that can represent when it's generated and uniquely generated characters/numbers.
Just wanted to go over the potential algorithms.
Sorry, unlikely to be very unique.
UUID is unbeatable but verbose (database/java). It gives 128 bits. Encode those with URL-safe Base64 and you get 22 almost alphanumeric chars (with two extras like - _).
Rolling your own: System.nanos().

Finding minimum String length of all lines in a CSV and printing them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So let's say I have a CSV file of random alphabets in columns for example's sake:
a
wrrq
jt
pkto
b
They are in String format and I want to be able to find the minimum String length in this CSV list. In the example given, the minimum being 1 (the maximum being 4). Then I want to print all the minimums in order, resulting in:
a
b
This is my code that reads each line from the top and it's barely anything to a helpful guideline but I'm kind of stuck right now.
BufferedReader br = new BufferedReader(new FileReader("Example.csv"));
while ((nextLine = br.readLine()) != null) {
...
}
Is there a way to do this a simple way without using fancy 3rd party scripts. Maybe putting them into an array and then working from there perhaps(?) Keeping in mind that the minimum may not always be 1.
For a single-pass solution…
Create an empty collection to store results. Read first line, and store it in collection.
Read another line.
If this line is the same length as previously stored line, add to collection.
If longer, ignore.
If shorter, empty collection and then add.
Lather, rinse, repeat.

how to count duplicate Name in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have a string as input which has names separated by the underscore character. Write a program that outputs the same string after removing any duplicate names in it (retain first occurrence when multiple occurrences are present), and adds the number of times the name occurs, immediately after the name. For example, Tom_Dick_Harry_Joe_Dick_Chris becomes Tom1_Dick2_Harry1_Joe1_Chris1.
I will provide no code but the following might help :
Split the array with '_'
Maintain a Map<String,Integer> occurrences for counting the occurrences of each name and a List<String> apparitionOrder
For each word of the array :
if the word is in occurrences, increment the value associated to the key
else, put this name in the map associated with 0 and append it to apparitionOrder
Instantiate a new array of the size of apparitionOrder
Fill the new array accordingly to apparitionOrder and occurrences

Aggregation in MapReduce [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can we find tha maximum and minimum element of a column in a .csv.
What should we pass into context.write(key,value) of mapper.
Whether it is each column of that csv file?
Solution
This is a bit broad for an SO question but I'll bite.
Your mapper is for mapping values to keys. Lets say your CSV has 4 columns with numeric values:
42, 71, 45, 22
You map a key to each value; effectively what would be like the header in the CSV. Lets say column 4 represented "Number of widgets". You'd map "number_of_widgets" as the key to the value of column 4 in your mapper.
The reducer is going to get all the values for a given key. That's where you figure out your min/max. You just iterate though all the values for the key and keep track of the min and max.
Mapper should transpose the file - for each line read, emit the key as the column number and the value as the value of the column.
Reducer should min/max. For each input key, emit the min and max value found.

Categories

Resources