I've been currently assigned to read from a .txt, and make a structure with what I've read. This is an example of what I should read:
Name1$Surname1$Programming&5.0#Mathematics&6.5#Algebra&7.2#History&6.7#Biology&6.9
I have no problems whatsoever when it comes to read the first two strings, however, from that point on i don't know how to manage, in order to properly split it and make new objects with them.
Any tips/ suggestions on how to do it pls?
Weird structure.
Split at '$'
First element of that split is the Name, second the Surname.
Split the third element at '#'.
Split each element of the result of step 3 again at '&' to get course and grade.
See here how to split strings.
Related
I am writing some values in csv file but the value containing commas get split into >1 once
e.g. a,b,c is one value and should appear in 1 cell but it's appearing in 3 cells.
writer.append(node.getLongName());
this is how I am writing data into csv files using FileWriter. If node.getLongName() gives me value having commas then value is split according to internal comma.
Can anyone please tell how to make this work and avoid splitting of value.
You are writing in to a CSV file but do you know out of your source file which fields should not be separated. If you do then you can change the seperator for that field from comma to some other seperator like '+' and than append with the other element of the CSV. As an example:
10/09/2016, cycling club,(sam+1000+oklahoma),(henry+ 1001+california),( bill+1002+NY)
Here inside the parenthesis It has the details of students. They were command separated before but I changed it to plus sign.
Although is can be manipulated by hand for trivial tasks, CSV format is tricky as soon as you need to process delimiter or new line escaping.
Unless you want to do the heavy testing yourself for all corner cases, you best bet is to rely on a well known CSV library like the one from apache.
Here it is still simple enough (assuming you only need to escape commas), and the common usage is to quote fields containing blanks or delimiters. That means to not write a,b,c but "a,b,c":
writer.append("\"" + node.getLongName()+ "\"");
I am building a sorting program for a class, and this whole week I've been stuck on how to read in the text file. The text file will be specified as a command argument on command line, and it will consist of hospital records. it will be 4 pieces of data separated by comma on each line. it will be someones last, and first name, room number, and age. I have to read in this data somehow line by line. number for peoples records aren't specified. I know how to sort them, I just havent been able to figure out how to read in the data.
this is an example of what it looks like.
Costanza,George,122,53
Poppins,Mary,123,72
You could read in the file line by line, splitting the line at the commas, storing the split string in an array, and setting fields accordingly. Do you have a class for the patient?
Here is an example that uses similar methods that may be applicable to your situation.
while(in.hasNextLine()){
line = in.nextLine();
studentTraits = line.split(" \\| ");
...}
//studentTraits is an array with 5 indexes, and
//each line of the file has 5 sections separated by the pipe character
Hope this helps. Next time you ask a question, it will be much more helpful if you asked a much more specific question. Here, you did not exactly ask a question, you just pretty much asked for someone to write some code for you, and it doesn't look like you put any effort into solving the problem for yourself. Please show what you know, and ask about what you are stuck on.
I need to write a parser for textfiles (at least 20 kb), and I need to determine if words out of a set of words appear in this textfile (about 400 words and numbers). So I am looking for the most efficient possibilitie to do this (if a match is found, i need to do some further processing of this and it's previous line).
What I currently do, is to exclude lines that do not contain any information for sure (kind of metadata lines) and then compare word by word - but i don't think that only comparing word by word is the most efficient possibility.
Can anyone please provide some tips/hints/ideas/...
Thank you very much
It depends on what you mean with "efficient".
If you want a very straightforward way to code it, keep in mind that the String object in java has method String.contains(CharSequence sequence).
Then, you could put the file content into a String and then iterate on your keywords you want to check to see if any of those appear in String, using the method contains().
How about the following:
Put all your keywords in a HashSet (Set<String> keywords;)
Read the file one line at once
For each line in file:
Tokenize to words
For each word in line:
If word is contained in keywords (keywords.containes(word))
Process actual line
If previous line is available
Process previous line
Keep track of previous line (prevLine = line;)
What will be the most eficient way to split a file in Java ?
Like to get it grid ready...
(Edit)
Modifying the question.
Basically after scouring the net I understand that there are generally two methods followed for file splitting....
Just split them by the number of bytes
I guess the advantage of this method is that it is fast, but say I have all the data in a line and suppose the file split puts half the data in one split and the other half the data in another split, then what do I do ??
Read them line by line
This will keep my data intact, fine, but I suppose this ain't as fast as the above method
Well, just read the file line by line and start saving it to a new file. Then when you decide it's time to split, start saving the lines to a new place.
Don't worry about efficiency too much unless it's a real problem later.
My first impression is that you have something like a comma separated value (csv) file. The usual way to read / parse those files is to
read them line by line
skip headers and empty lines
use String#split(String reg) to split a line into values (reg is chosen to match the delimiter)
I have 100 words. All 100 words are look like this.
EnglishWord,EngMeaning,NumberofW… meaning,31
In that I want to retrieve EnglishWord, e.g. Friendship alone for 100 words by using Java program.
I am assuming you have a "body" (main string), containing a list of substrings and you want to retrieve any specific one substring from within.
This looks a lot like homework/exercise, so I'll avoid giving you a ready-to-roll answer, since you need to achieve a solution yourself for it to be of any value, but the general steps you will need are the following:
1:
Be able to separate each substring (entry) from the others (the base string) in an organized fashion.
This can be done (for the string case), as #kylc said, with String's split function, which uses a REGEX (PATTERN) to define divisors (one or more), that then is/are used to divide the string into an array of multiple substrings.
String[] arrayOfEntries /*something to hold the result*/ = yourStringVar.split("," /*your split regex pattern*/);
NOTE: For more information on these, here are the links: String's split function, Pattern.
2:
Be able to acquire any specific entry withing an array of entries.
This is best done with a function you can reuse for other works. You need to define a "target" (what/which is going to be acquired) and a "source" (group of entries to acquire "target" from).
All you have to do is loop the "source", and for each entry there, compare to "target" for a match; When a match is found, just return it.
That's it! The rest is up to you!