I have some problems with my code while reading the file, this is the entries of my file:
Want
Serious
Try
I Want
I Try
and here is my code
//open the file
openFile();
while(x.hasNext()){
//pass the word into variable
String a = x.next();
//store in array list
Streams.add(a);
}
Since my entries are contains of single-word, and two-words(bigram), the script cannot read it, my script will make a word like "I want" stored in two different array, while i actually want to store the single-word and the bigram into an index.
So, the first index will contain : want, 2nd Serious, 3rd Try, 4th I want, 5th I try
Sorry for the confusing question, any help will be appreciated, thanks :)
Are the entries in your file present in seperate lines. If so, you could use nextLine() instead of next().
Related
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.
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.
Quick enquiry..
I have created an array and it will be populated by a scanner passing reading through information from a .txt file. The .txt file has a specific structure:
<job role> <years of experience> <name>
( this is an example ). This will be inputted by more than one person so there will be multiple of these in the text file. So, I now need to find a way to gather them into an array into an ordered structure. The order should be based on the first alphabetic letter of their job role. I was
thinking about implementing a comparator would this be possible/efficient to do?
So my idea would be use a comparator somehow on specifically and compare them will all other job role entries..
thanks and sorry if it's a brief or not very clear found it difficult describing the situation...
In order to accurately read in the text file, you would need a delimiter. A delimiter is a character that tells the computer that you are moving to the next data entry.
I recommend the following: for a new person, I recommend a new line \n as the delimiter. To tell the difference in the objects job, years, and name, I recommend using a comma, ",", as it does not appear that the the data set requires the use of that.
Once you have decided on a delimiter, I would recommend looking into the class File, FileInputStream and FileOutputStream. These classes are data streams that can read information from files saved on the computer.
I'm working on my second bigger programming project at the moment and I got stuck. I'm using Processing for this project.
What I'm trying to do is retrieve information (used to assign a certain color palette to the individual 'lines' of a horizontal bar chart) from an external text file that contains the following line, using an instance of the java.util.Properties class:
formating = p;p;n;n
My code snippet for importing it looks like this (using a class named 'Import' that handles the BufferedInputStream, etc.):
Import imp = new Import();
Properties properties = imp.importSettings();
The next step reads the 'formating' line from the text file and puts it into a four element String array, using the Semicolon as a delimiter.
String[] formating = properties.getProperty("formating").split(";");
I was expecting for this String array to be identical to the one I would get by creating it in my source code using:
String[] formating2 = {"p", "p", "n", "n"};
But it isn't. It tried a number of things already, including checking for unwanted characters (blanks for example) in each element of my String array, converting my text file or the characters I use for comparison to Unicode, converting the elements of the String array to Chars.
What I can't seem to get working is the following comparison:
for(int i=0;i < formating.length;i++){
println(formating[i]==formating2[i]);
}
which returns 'false' for each iteration of the for-loop.
I'm sure it's just some rookie mistake but it would be nice if someone could point me in the right direction. Thanks in advance!
Nick
comparing strings using == is not safe since Strings are possibly different objects and comparing them, no matter if they contain the same "text" does not compare the texts but the objects. So, you should try it like this:
println(formating[i].equals(formating2[i]));
or if you want to avoid excess spaces and tabs all-together you can also do:
println(formating[i].trim().equals(formating2[i].trim()));
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;)