Find word in text file and count specific words - java

now I'll try to explain what I need to do.
I have file.txt file, It looks like:
John //first line - name
One
Three
Four
Peter //first line - name
Two
Three
Elisa //first line - name
One
Three
Albert //first line - name
One
Three
Four
Nicole //first line - name
Two
Four
So I have program's code:
public class Testing {
public static void main(String args[]) throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("Select word from list:");
System.out.println();
try {
FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
String stilius = input.nextLine(); // eneter word which I want to count in File.txt
BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before
int counter = 0;
String line;
System.out.println("Looking for information");
while (( line = bf.readLine()) != null){
int indexfound = line.indexOf(stilius);
if (indexfound > -1) {
counter++;
}
}
if (counter > 0) {
System.out.println("Word are repeated "+ counter + "times");}
else {
System.out.println("Error...");
}
bf.close();
}
catch (IOException e) {
System.out.println("Error:" + e.toString());
}
}
}
This program counting specific word (entered by keyboard) in file.txt.
I need to make this program: for ex.: if I enter word: One It must show:
Word One repeated 3 times by John, Elisa, Albert
All what I need to elect by who this word repeated. But I don't know really how to make It, maybe LinkedList or I dont know, someone could help me?
Thank you very much.

You can have a List<String> that holds where your word was repeated, and add elements to it on the fly.
To add elements to it you can use an extra variable lastName (of type String), and in your while loop, do something like:
if (line.trim().length() == 0) lastName = null;
else if (lastName == null) lastName = line;
The first line if for "resetting" the lastName variable, after you changes the name of the name (assuming there is an empty line after all the words of each name)
The second line is setting it back to the new name after an empty line.
And in addition when you increase counter do:
myList.add(lastName)
So the loop will be something like that:
List<String> resultList = new ArrayList<String>();
String lastName = null;
while (( line = bf.readLine()) != null){
if (line.trim().length() == 0) lastName = null;
else if (lastName == null) lastName = line;
int indexfound = line.indexOf(stilius);
if (indexfound > -1) {
counter++;
resultList.add(lastName);
}
//do something with resultList, which contains the names
}

I'd suggest you to use Map<String, Set<String>> where key is a word and value is the list of people names that "typed" this word.
So, here is how you can define your collection:
Map<String, Set<String>> word2people = HashMap>();`
Here is how you can add words there:
String name = ...
String word = ...
Set<String> people = word2people.get(word);
if (people == null) {
people = new HashSet<String>();
word2people.put(people);
}
people.add(name);
And here is how you can retrieve the value:
word2people.get(word)

Related

How do I separate input lines in a file in string value and integer keys for Hash tables?

I want to get parse (if possible) the partition that contains the string that is a number(i.e."95") but I could accept any strategy to do it. my code works for hashMaps
to not make this lengthy, this is how the lines in the input file look like:
Kostas_Antetokounmpo 37
public static void main (String[] args) throws IOException {
String path = "roster.txt";
String row;
Integer row1;
HashTable_NBA<Integer,String> roster = new HashTable_NBA<>();
BufferedReader read = new BufferedReader(new FileReader(path));
while ((row = read.readLine()) != null){
String[] partition = row.split(" ", 2);
if(partition.length >= 2){
Integer key = Integer.parseInt(partition[1]);
String value = partition[0];
roster.put(key, value);
}
}
System.out.println(roster);
}
}
//EDIT
//the errors are these
Exception in thread "main" java.lang.NumberFormatException: For input string: "37 "
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at HashTable_NBA.main(HashTable_NBA.java:161)
This is a guess but I am assuming there could be more than one space between the values. And I remove the 2 from the split command. That was causing problems.
while ((row = read.readLine()) != null){
String[] partition = row.split("\\s+");
if(partition.length >= 2){
Integer key = Integer.parseInt(partition[1]);
String value = partition[0];
roster.put(key, value);
}
}

Print out the total number of letters - Java

I want to print out the total number of letters (not including whitespace characters) of all the Latin names in the data file. Duplicate letters must be counted. This is what I have done so far:
List<Person> peopleFile = new ArrayList<>();
int numberOfLetters = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("people_data.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] tokenSize = fileRead.split(":");
String commonName = tokenSize[0];
String latinName = tokenSize[1];
Person personObj = new Person(commonName, latinName);
peopleFile.add(personObj);
fileRead = br.readLine();
// Iterating each word
for (String s: tokenSize) {
// Updating the numberOfLetters
numberOfLetters += s.length();
}
}
br.close();
}
catch (FileNotFoundException e) {
System.out.println("file not found");
}
catch (IOException ex) {
System.out.println("An error has occured: " + ex.getMessage());
}
System.out.print("Total number of letters in all Latin names = ");
System.out.println(numberOfLetters);
The problem is that it prints out all number of letters in the file, I just want it to print out the number of characters in the Latin names.
The text file:
David Lee:Cephaloscyllium ventriosum
Max Steel:Galeocerdo cuvier
Jimmy Park:Sphyrna mokarren
What you are doing wrong is you are counting all the names despite you tokenize them. You can use this method to count letters of any String or Sentence.
public static int countLetter(String name) {
int count = 0;
if(name != null && !name.isEmpty()) {
/* This regular expression is splitting String at the
* sequence of Non-alphabetic characters. Hence actually
* splitting the Name into group of words */
String[] tokens = name.split("[^a-zA-Z]+");
for(String token : tokens) {
count += token.length();
}
}
return count;
}
And replace these lines
/* Note: here you are iterating all your Names from each line */
for (String s: tokenSize) {
// Updating the numberOfLetters
numberOfLetters += s.length();
}
with this
numberOfLetters += countLetter(latinName);
Does it make sense ? I hope you found your problem.
NB: you can experiment with this regex here
Get rid of all the blank spaces before summing the length :
s=s.replaceAll("[ \n\t]+","");
numberOfLetters += s.length();

Split information in a CSV file using Java, count the strings and discard the duplicates

I have a CSV file with the following information:
2,Cars
5,Cars
5,Planes
5,Boats
10,Planes
10,Boats
28,Planes
I want to split the numbers from the type of transportation. How can I count the total of cars + planes + boats to be '3' and not '7'?
I am using the following Java code that someone else provided to split the CSV:
try {
BufferedReader br2 = new BufferedReader(new FileReader("transport.csv"));
System.out.println("\nTESTING");
String sCurrentLine2;
java.util.HashMap<String, String>();
while ((sCurrentLine2 = br2.readLine()) != null) {
String[] information2 = sCurrentLine2.split(",");
String transCode = information2[1];
System.out.println(transCode);
}
} catch (IOException e) {
e.printStackTrace();
}
In the array String transCode = information2[1]; when I change to 0 it will give the numbers, when I change to 1 gives the names.
while((sCurrentLine2 = br2.readLine()) != null{
String[] entries = sCurrentLine2.split(",");
Set<String> types = new Hashset<>();
for(int i = 0; i < entries.length; i++){
String[] entry = entries[i].split(" ");
types.add(entry[0]);
}
System.out.println(types.size());
}
I modified the code you provided. Maybe there is another way to do it better, but this is what I did. I forced it a little and gave '3' as result. But it should have done it counting the words not considering duplicated.
while ((line2 = br2.readLine()) != null) {
String[] entries = line2.split(",");
for (int i = 0; i < entries.length; i++) {
String[] entry = entries[i].split(" ");
termsDup.add(entry[0]);
}
}
System.out.println(termsDup.size()-4);

Calculating the frequency of strings as they get stored in a nested hashmap

i want to write a code that stores strings in a hashmap as they are read from text files.
i have written the code below and it works, no errors, the frequency of every occurrence of the string combination does not change, it is always 1.
i am asking for assistance on how i can ensure that if a string combination appears more than once in the text file then its frequency should also increase.
this is my code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class NgramBetaC {
static String[] hashmapWord = null;
public static Map<String,Map<String, Integer>> bigrams = new HashMap<>();
public static void main(String[] args) {
//prompt user input
Scanner input = new Scanner(System.in);
//read words from collected corpus; a number of .txt files
File directory = new File("Corpus4");
File[] listOfFiles = directory.listFiles();//To read from all listed iles in the "directory"
//String bWord[] = null;
int lineNumber = 0;
String line;
String files;
String delimiters = "[\\s+,?!:;.]";
int wordTracker = 0;
//reading from a list of text files
for (File file : listOfFiles) {
if (file.isFile()) {
files = file.getName();
try {
if (files.endsWith(".txt") || files.endsWith(".TXT")) { //ensures a file being read is a text file
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
line = line.toLowerCase();
hashmapWord = line.split(delimiters);
for(int s = 0; s < hashmapWord.length - 2; s++){
String read = hashmapWord[s];
String read1 = hashmapWord[s + 1];
final String read2 = hashmapWord[s + 2];
String readBigrams = read + " " + read1;
final Integer count = null;
//bigrams.put(readBigrams, new HashMap() {{ put (read2, (count == null)? 1 : count + 1);}});
bigrams.put(readBigrams, new HashMap<String, Integer>());
bigrams.get(readBigrams).put(read2, (count == null) ? 1 : count+1);
} br.close();
}
}
} catch (NullPointerException | IOException e) {
e.printStackTrace();
System.out.println("Unable to read files: " + e);
}
}
}
}
THE LINES CONTAINED IN THE TEXT FILES ARE::
1.i would like some ice cream.
2.i would like to be in dubai this december.
3.i love to eat pasta.
4.i love to prepare pasta myself.
5.who will be coming to see me today?
THE OUTPUT I GET WHEN PRINTING CONTENTS OF THE HASHMAP IS:
{coming to={see=1}, would like={to=1}, in dubai={this=1}, prepare pasta={myself=1}, to eat={pasta=1}, like to={be=1}, to prepare={pasta=1}, will be={coming=1}, love to={prepare=1}, some ice={cream=1}, be in={dubai=1}, be coming={to=1}, dubai this={december=1}, to be={in=1}, i love={to=1}, to see={me=1}, who will={be=1}, like some={ice=1}, i would={like=1}, see me={today=1}}
Please assist! some string combinations are not even appearing.
THE OUTPUT I EXPECT AS I READ FROM THE FILES IS:
{coming to={see=1}, would like={to=1}, in dubai={this=1}, prepare pasta={myself=1}, to eat={pasta=1}, like to={be=1}, to prepare={pasta=1}, will be={coming=1}, love to={prepare=1}, some ice={cream=1}, be in={dubai=1}, be coming={to=1}, dubai this={december=1}, to be={in=1}, i love={to=1}, to see={me=1}, who will={be=1}, like some={ice=1}, i would={like=2}, see me={today=1}, love to {eat=1}, would like {some=1}, i would {love=1}, would love {to=1}}
Tentatively update the current structure without overwriting the originl content
Replace
bigrams.put(readBigrams, new HashMap<String, Integer>());
bigrams.get(readBigrams).put(read2, (count == null) ? 1 : count+1);
With
HashMap<String, Integer> counter = bigrams.get(readBigrams);
if (null == counter) {
counter = new HashMap<String, Integer>();
bigrams.put(readBigrams, counter);
}
Integer count = counter.get(read2);
counter.put(read2, count == null ? 1 : count + 1);

How do I read each column of a txt file, and place them in separate arrays?

I'm doing a Programming Assignment and basically I need to read from a txt file and sort everything in there in different arrays allowing me to display everything in the cmd prompt neatly and be able to delete stuff.
h Vito 123
d Michael 234 Heart
s Vincent 345 Brain Y
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
n Vicky 911 7
First column needs to be the employeeRole (employee, doctor). The second column being the employeeName. Third column being the employeeNumber and some of them have have a fourth column (if it's a number it's number of patients. Y is for like sweeping, or answering calls)
So my thought process was put each column into it's own array and then writing it out that way. I was able to put each row into its own array with
public class ReadingFile {
// String test;
// char[] employeeRole = new char[9];
String[] employeeRole = new String[9];
String[] employeeName = new String[9], specialty;
String[] wholeLine = new String[9];
// String word;
int[] employeeNum = new int[9];
int r, n, l, num;
public void Reader()
{
Scanner inputStream = null;
Scanner inputStream2 = null;
Scanner inputStream4 = null;
try
{
BufferedReader inputStream3 =
new BufferedReader(new FileReader("data.txt"));
inputStream = new Scanner(new FileInputStream("data.txt"));
inputStream =
new Scanner(new FileInputStream("data.txt"));
inputStream2 =
new Scanner(new FileInputStream("data.txt"));
inputStream4 =
new Scanner(new FileInputStream("data.txt"));
System.out.println("Yeah");
}
catch(FileNotFoundException e){
System.out.println("File Not found");
System.exit(1);
}
for (l=0; l<9; l++)
{
wholeLine[l] = inputStream2.nextLine();
System.out.println(wholeLine[l]);
}
But I couldn't figure out what to do from there. Doing a split would then put an array into an array? Which means I would put each line into an array and then each word into an array?
So I tried something else, anything with the length not equal to 1 would be the employeeNum, but then they there were the N's and Y's and the number of pateints.
for(r=0; r<9; r++) //role
{
String next = inputStream4.next();
while( next.length() != 1)
{
next = inputStream4.next();
}
employeeRole[r] = next;
System.out.println(employeeRole[r]);
}
I also tried
for (r=0; r<9; r++)
{
employeeRole[r] = wholeLine[r].substring(wholeLine[r].indexOf(1));
//inputStream.nextLine();
System.out.println(employeeRole[r]);
}
I'm just not sure if I'm going the right way about it? If I'm making it more difficult than it really is? Or if there's an easier way to do this. But after everything is done, the output should be able to basically say
Doctors: 2
Name: Michael Employee Number: 234 Specialty: Heart
Name: Nicos Employee Number: 891 Specialty: Bone
Any help would be greatly appreciated, thanks!
You don't have to open 4 streams in order to read the file (I guess you wanted to open "one per column" but you shouldn't do it).
Second, you can split the string on spaces (" ") which will provide you the columns (for every line separately) exactly like you want.
Code example:
BufferedReader br = null;
String[] characters = new String[1024];//just an example - you have to initialize it to be big enough to hold all the lines!
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("data.txt"));
int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String[] arr = sCurrentLine.split(" ");
//for the first line it'll print
System.out.println("arr[0] = " + arr[0]); // h
System.out.println("arr[1] = " + arr[1]); // Vito
System.out.println("arr[2] = " + arr[2]); // 123
if(arr.length == 4){
System.out.println("arr[3] = " + arr[3]);
}
//Now if you want to enter them into separate arrays
characters[i] = arr[0];
// and you can do the same with
// names[1] = arr[1]
//etc
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

Categories