I have a method that counts the occurrences of words in a text file, and returns the number of time the word is found on a particular line. However, it doesn't keep track of which line number the words are located. i have a separate method that counts the number of lines in the text file and i would like to combine the two methods into a method that tracks the line numbers, and keeps a log of the words occurrences on each line.
here are the two methods i would like to combine to give a result something like "Word occurs X times on line Y"
public class Hash
{
private static final Object dummy = new Object(); // dummy variable
public void hashbuild()
{
File file = new File("getty.txt");
// LineNumberReader lnr1 = null;
String line1;
try{
Scanner scanner = new Scanner(file);
//lnr1 = new LineNumberReader(new FileReader("getty.txt"));
// try{while((line1 = lnr1.readLine()) != null)
// {}}catch(Exception e){}
while(scanner.hasNextLine())
{
String line= scanner.nextLine();
List<String> wordList1 = Arrays.asList(line.split("\\s+"));
Map<Object, Integer> hm = new LinkedHashMap<Object, Integer>();
for (Object item : wordList1)
{
Integer count = hm.get(item);
if (hm.put(item, (count == null ? 1 : count + 1))!=null)
{
System.out.println("Found Duplicate : " +item);
}
}
for ( Object key : hm.keySet() )
{
int value = hm.get( key );
if (value>1)
{
System.out.println(key + " occurs " + (value) + " times on line # "+lnr1.getLineNumber());
}
}
}
} catch (FileNotFoundException f)
{f.printStackTrace();}
}
}
here is my original line counting method
public void countLines()
{
LineNumberReader lnr = null; String line;
try
{
lnr = new LineNumberReader(new FileReader("getty.txt"));
while ((line = lnr.readLine()) != null)
{
System.out.print("\n" +lnr.getLineNumber() + " " +line);
}
System.out.println("\n");
}catch(Exception e){}
}
Why don't you just remember the line number in the while loop? Initialize a new variable and increase it when calling nextline.
Related
I want to read the file and add each entry to an arraylist on a date. But the date should also be included.
File Example:
15.09.2002 Hello, this is the first entry.
\t this line, I also need in the first entry.
\t this line, I also need in the first entry.
\t this line, I also need in the first entry.
17.10.2020 And this ist the next entry
I tried this. But the Reader reads only the first Line
public class versuch1 {
public static void main(String[] args) {
ArrayList<String> liste = new ArrayList<String>();
String lastLine = "";
String str_all = "";
String currLine = "";
try {
FileReader fstream = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fstream);
while ((currLine = br.readLine()) != null) {
Pattern p = Pattern
.compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
Matcher m = p.matcher(currLine);
if (m.find() == true) {
lastLine = currLine;
liste.add(lastLine);
} else if (m.find() == false) {
str_all = currLine + " " + lastLine;
liste.set((liste.indexOf(currLine)), str_all);
}
}
br.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.print(liste.get(0) + " "+liste.get(1);
}
}
I have solved my problem :)
public class versuch1 {
public static void main(String[] args) {
ArrayList<String> liste = new ArrayList<String>();
String lastLine = "";
String currLine = "";
String str_all = "";
try {
FileReader fstream = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fstream);
currLine = br.readLine();
while (currLine != null) {
Pattern p = Pattern
.compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
Matcher m = p.matcher(currLine);
if (m.find() == true) {
liste.add(currLine);
lastLine = currLine;
} else if (m.find() == false) {
liste.set((liste.size() - 1), (str_all));
lastLine = str_all;
}
currLine = br.readLine();
str_all = lastLine + currLine;
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.print(liste.get(1) + " ");
}
}
While reading the lines, keep a "current entry".
If the line read begins with a date, then it belongs to a new entry. In this case add the current entry to the list of entries and create a new current entry consisting of the read line.
If the line did not begin with a date, just add it to the current entry.
For this to work, you need to read the first line into the current entry before the loop. And after the loop you need to add the current entry to the list of entries. This in turn only works if there is at least one line and the first line begins with a date. So handle the special case of no lines specially (use if-else). And report an error if the first line does not begin with a date.
Happy coding.
I want to find the line number of a text file by each word, however, the method I wrote below only gives the first number while I need a list of line numbers.
For instance, if "a" occurs in lines: 1,3,5, it should have a list of [1,3,5]. This list result then will be passed into another method for further process. But, my result only shows [1] for "a".
Can someone help me fix this? Thank you!
public SomeObject<Word> buildIndex(String fileName, Comparator<Word> comparator) {
SomeObject<Word> someObject = new SomeObject<>(comparator);
Comparator<Word> comp = checkComparator(someObject.comparator());
int num = 0;
if (fileName != null) {
File file = new File(fileName);
try (Scanner scanner = new Scanner(file, "latin1")) {
while (scanner.hasNextLine()) {
String lines;
if (comparator instanceof IgnoreCase) {
lines = scanner.nextLine().toLowerCase();
} else {
lines = scanner.nextLine();
}
if (lines != null) {
String[] lineFromText = lines.split("\n");
List<Integer> list = new ArrayList<>();
for (int i = 0; i < lineFromText.length; i++) {
String[] wordsFromText = lineFromText[i].split("\\W");
num++;
for (String s : wordsFromText) {
if (s != null && lineFromText[i].contains(s)) {
list.add(num);
}
if (s != null && !s.trim().isEmpty() && s.matches("^[a-zA-Z]*$")) {
doInsert(s, comp, someObject, list);
}
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return someObject;
}
Does something like this work for you?
It reads in the lines one at a time.
Finds the words by splitting on spaces.
Then puts the words and the line numbers in a map where the
key is the word an the value is a list of line numbers.
int lineCount = 1;
String fileName = "SomeFileName";
Map<String, List<Integer>> index = new HashMap<>();
Scanner scanner = new Scanner("fileName");
while (scanner.hasNextLine()) {
//get single line from file
String line = scanner.nextLine().toLowerCase();
//split into words
for (String word : line.split("\\s+")) {
// add to lineNumber to map if List already there.
// otherwise add new List and then add lineNumber
index.compute(word,
(wd, list) -> list == null ? new ArrayList<>()
: list).add(lineCount);
}
// bump lineCount for next line
lineCount++;
}
Print them out.
index.forEach((k, v) -> System.out.println(k + " --> " + v));
I doing some coding related to my project. I need to assign simply a string value to a String array reading from a file.
But I can't understand why the value keeps always null. The values of string doesn't assign to the array. Can someone explains me the mistake I have done.
Here I have posted my code.
Test.java
public class Test {
public static void main(String[] args) throws IOException {
//Tuning Jaccard Coefficient algorithm for Training set
ReadFile_2 rf = new ReadFile_2();
rf.readFile("C:/Users/user/Desktop/Msc-2016/InformationRetrieval/project material/train.txt","Training");
}
}
ReadFile_2.java
class ReadFile_2 {
List<String> copying_strings1 = new ArrayList<>();
String[] Apparted_Strings = new String[3];
String[] copying_strings = new String[50];
int arryListSize = copying_strings.length;
static int value_of_shingle;
static int best_Shingle;
String[] fileType;
int fileType_size;
public void readFile(String fileName, String file_type) throws FileNotFoundException, IOException {
//Name of the file
try {
if (file_type.equals("Training")) {
best_Shingle = 2;
} else if (file_type.equals("Testing")) {
best_Shingle = value_of_shingle;
}
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
int r = 0;
while ((line = bufferReader.readLine()) != null) {
copying_strings[r] = line;
r++;
System.out.println("lll " + copying_strings[r]);
System.out.println("lll " +line);
//Apparted_Strings = sp.apart_Strings_3(line);
//CallingAlgo_4 c_a = new CallingAlgo_4(Apparted_Strings[0], Apparted_Strings[1], Apparted_Strings[2], best_Shingle, "Jaccard");
}
//Close the buffer reader
bufferReader.close();
} catch (Exception e) {
System.out.println("Error while reading file line by line:" + e.getMessage());
}
}
}
Can someone please let me know why the value of
System.out.println("lll " + copying_strings[r]);
prints always as null.
You increment the while loop variable (r) before printing the string value.
So it prints the next array value that is null.
So please increment the variable (r) after printing the string value as mentioned below,
while ((line = bufferReader.readLine()) != null) {
copying_strings[r] = line;
System.out.println("lll " + copying_strings[r++]);
System.out.println("lll " +line);
}
You have a mistake in your while-loop. The correct sequence is to read a line first, pass it to String, print it and finally increment the looped variable.
while ((line = bufferReader.readLine()) != null) { // read a line
copying_strings[r] = line; // pass to String
System.out.println("lll " + copying_strings[r]); // print for the 1st time
System.out.println("lll " + line); // print for the 2nd time
r++; // increment the looped variable
}
If you print the variable copying_strings after incrementing r++, you get obviously null because nothing has been passed in it.
The purpose of this program is to read an input file and parse it looking for words. I used a class and instantiated objects to hold each unique word along with a count of that word as found in the input file. For instance, for a sentence “Word” is found once, “are” is found once, “fun” is found twice, ... This program ignores numeric data (e.g. 0, 1, ...) as well as punctuation (things like . , ; : - )
The assignment does not allow using a fixed size array to hold word strings or counts. The program should work regardless of the size of the input file.
I am getting the following compiling error:
'<>' operator is not allowed for source level below 1.7 [line: 9]
import java.io.*;
import java.util.*;
public class Test {
public static void main(String args[]) throws IOException {
HashMap<String,Word> map = new HashMap<>();
// The name of the file to open.
String fileName = "song.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] words = line.split(" ");
for(String word : words){
if(map.containsKey(word)){
Word w = map.get(word);
w.setCount(w.getCount()+1);
}else {
Word w = new Word(word, 1);
map.put(word,w);
}
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
for(Map.Entry<String,Word> entry : map.entrySet()){
System.out.println(entry.getValue().getWord());
System.out.println("count:"+entry.getValue().getCount());
}
}
static class Word{
public Word(String word, int count) {
this.word = word;
this.count = count;
}
String word;
int count;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
You either need to compile with a JDK of version 1.7 or later, or change the line:
HashMap<String,Word> map = new HashMap<>();
to
HashMap<String,Word> map = new HashMap<String,Word>();
replace
HashMap<String,Word> map = new HashMap<>();
with:
HashMap<String,Word> map = new HashMap<String,Word>();
if anyone can help me please in java programme , I have file called "input.txt" which contains text like
Agent1 R F 2 0
Agent2 R B 4 5
Agent4 C E 2 2
Agent3 R F 3 11
I want to save all the lines in different variable and do work on them.
say here I want to save the first line into string which I will call line1, second line I will save in string called line2 third line I will save on string called line3 and so on.
is their any way to do that. my txt file can have any number of line and I want to save those lines as strings do work on them.
simply I need something like a loop which will keep on changing the name of variable . but i have no idea how to do it.
here is my code till now but instead of output i want to save lines as string of any data type
any help really appreciated.
You cannot create variables at the fly, like you mentioned just by changing the name. You can use List for storing the data from file and process it later.
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
List<String> names = new ArrayList<String>();
while (scanner.hasNext()) {
String line = scanner.nextLine();
names.add(line);
}
// Now all the lines from the file are stored in the list.
// You can do the processing you need to do.
Convert the List of Strings into String array and use the Arrays.sort method for sorting the array. We will provide the custom Comparator to sort the array as per our needs.
String nameArray[] = names.toArray(new String[names.size()]);
Arrays.sort(nameArray, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
String array1[] = o1.split(" ");
String array2[] = o2.split(" ");
return array1[3].compareTo(array2[3]);
}
});
The assumption here is lines will always contain the five elements and we are sorting by fourth position number in the line.
output:
Agent1 R F 2 0
Agent4 C E 2 2
Agent3 R F 3 11
Agent2 R B 4 5
Try this.
public static Map<String, String> loadFile(Reader reader)
throws IllegalArgumentException{
Map<String, String> mapList = new TreeMap<String, String>();
if(reader == null)
{
throw new IllegalArgumentException("Reader not valid");
}
String line;
innerReader = new BufferedReader(reader);
int countLine = 0;
try
{
while((line = innerReader.readLine()) != null)
{
if (line == null || line.trim().isEmpty())
throw new IllegalArgumentException(
"Line Empty");
mapList.put("line"+String.valueOf(countLine), line);
countLine++;
}
} catch (IOException e) {
}
return mapList;
}
In main add this to try your code.
Map<String, String> mapList = new TreeMap<String, String>(Collections.reverseOrder());
try {
mapList = loadFile(new FileReader("YourFile.txt"));
} catch (IOException e) {
e.printStackTrace();
}
for (Map.Entry entry : mapList.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
Ann this is output.
line0, Agent1 R F 2 0
line1, Agent2 R B 4 5
line2, Agent4 C E 2 2
line3, Agent3 R F 3 11
For print out in file add this:
private static PrintWriter innerWriter;
public static void printMap(Map<String, String> myMap, Writer writer)
throws IOException {
if(writer == null)
{
throw new IOException("Cannot open file");
}
innerWriter = new PrintWriter(writer);
for (Map.Entry entry : myMap.entrySet()) {
innerWriter.write(entry.getKey() + ", " + entry.getValue() + "\n");
//OR THIS FOR ONLY VALUES
// innerWriter.write(entry.getValue() + "\n");
}
innerWriter.close();
}
and this on main
try {
printMap(mapList, new FileWriter("FileOutput.txt"));
} catch (IOException e) {
e.printStackTrace();
}