i want to counting matching and counting words.
i have two text file , and to compare words eachother.
for example,
a text file : a b c d e.
b text file : a a a a a.
and i want to see this output.
output : a 5.
but when i wrote code, it didn't works.
please help me.
i wrote code for java adk 1.8 using eclipses, windows 8.1 64bit.
this is code following this.
package test1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ex01 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileReader fr = new FileReader("C:/Users/Hong/Desktop/승현연구/152-300/301.txt");
FileReader key_item = new FileReader("C:/Users/Hong/Desktop/승현연구/no-yes2500.txt");
BufferedReader br = new BufferedReader(fr);
BufferedReader br2 = new BufferedReader(key_item);
FileOutputStream file = new FileOutputStream("C:/Users/Hong/Desktop/승현연구/답변빈도/a301.txt");
List<String> key = new ArrayList<String>();
List<String> str = new ArrayList<String>();
String in = "";
String s = "";
String ss[];
while ((in = br2.readLine()) != null) {
key.add(in);
}
while ((s = br.readLine()) != null) {
str.add(s);
}
***int cnt = 0;
int count = 0;
int cont = 0;
String txt = "";
for (int i = 0; i < key.size(); i++) {
for (int j = 0; j < str.size(); j++) {
System.out.println(j + " " + str.get(j));
if (str.get(j).lastindexOf(key.get(i))) {
cnt++;
//System.out.println(key.get(i) + " " + cnt);
}
if (cnt == 1){
//cont ++ 1;
//System.out.printf("%d",cont);
}
}
System.out.println(key.get(i) + " " + cnt);
txt = txt + key.get(i) + " " + cnt + "\n";
cnt = 0;
}
file.write(txt.getBytes());
}***
//System.out.println("Hello Java");
}
in my coding, error causeing this line
[ if (str.get(j).lastindexOf(key.get(i)))]
i don't know why
this is summary for explain text file and what i want to do
First, the code i'd like to see is to compare 301 text file and no-yes2500 text file and output the word counts belonging to no-yes2500
(ex : apple 3
banana 2 )
301.txt is a text file that consists of sentences about Q&A community answers.
no-yes2500.txt is a keyword list
str.get(j).toString().lastIndexOf(key.get(i).toString())>=0
Try this
You have a typo in your code, replace lastindexOf with lastIndexOf and make the expression in the if-clause to evaluate to a boolean, currently its an int (i.e. an index)
Related
After importing a CSV file and sorting it in to a 2-Dimensional array I get a couple of weird characters in only the first and possibly the last cell.
Expected output: S1358_R1
Actual output: S1358_R1
Does anyone know why these extra characters show up? The code used to do this is included below:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class open2 {
public static void main(String[] args) {
String line = "";
String splitBy = ",";
try {
//parsing a CSV file into BufferedReader class constructor
int i = 0;
String[][] ss = new String[10000][10000];
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\micha\\Documents\\spreadsheet.csv"));
while ((line = br.readLine()) != null) //returns a Boolean value
{
String[] cells = line.split(splitBy);
for (int j = 0; j < cells.length; j++) {
ss[i][j] = cells[j];
} // use comma as separator
i = i + 1;
}
System.out.println(ss[0][0]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Below java code is to split a big .csv file into multiple .csv files. But how to store Header in all splitted files?
import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class split {
public static void main(String args[]) {
try {
String inputfile = "E:/Sumit/csv-splitting-2/Proposal_Details__c.csv";
System.out.println("Input Path is :- " + inputfile);
double nol = 100000.0;
File file = new File(inputfile);
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
System.out.println("Lines in the file: " + count);
double temp = (count / nol);
int temp1 = (int) temp;
int nof = 0;
if (temp1 == temp) {
nof = temp1;
} else {
nof = temp1 + 1;
}
System.out.println("No. of files to be generated :" + nof);
FileInputStream fstream = new FileInputStream(inputfile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
for (int j = 1; j <= nof; j++) {
String outputpath = "E:/Sumit/csv-splitting-2/";
String outputfile = "File-2-Proposal_Details__c" + j + ".csv";
System.out.println(outputpath + outputfile);
FileWriter fstream1 = new FileWriter(outputpath + outputfile);
BufferedWriter out = new BufferedWriter(fstream1);
for (int i = 1; i <= nol; i++) {
strLine = br.readLine();
if (strLine != null) {
out.write(strLine);
if (i != nol) {
out.newLine();
}
}
}
out.close();
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Assuming your first line is the header, you can have a String header; that will get the read of the first line, eg: header = br.readLine();.
On your for loop for nof (which I assume means number_of_files), you always add the header as the first line when you create a new file.
It would be something like this:
before your for-loop, you just save the header on a variable
String header = br.readLine();
you have 2 for-loops, one that creates a file, the other one that write each line to the newly created file
Inside the first for loop, right after you create the file, you just write the header to it: our.write(header);
General tips:
use variable names that makes sense. nol, nof, j... none of them make sense, you can pretty much call them numOfLines, numOfFiles and currentFile for example.
I have a task to read a text file with several lines, after that I need to count every character's UNICODE value, so the sum of "hello" is 532 and for "how are you" is 1059 and so on, every string begins on new line in the .txt document and so far so good.
But for every line I need to print only its own value, and the way my code works, it adds every line's value and I cant get my head around a way to stop it when the end of the lxtine comes so it looks something like:
*read line
*count char values
*add up
*print them
*start over for the next line, and so
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.String;
import java.util.Arrays;
public class SumLines {
public static void main(String[] args) {
String filePath = "/home/lines.txt";
String readLine;
int sum = 0;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
while ((readLine = bufferedReader.readLine()) != null) {
char[] array = new char[readLine.length()];
System.out.println(readLine);
for (int i = 0; i < readLine.length(); i++) {
Arrays.fill(array, readLine.trim().charAt(i));
sum += (int) array[i];
System.out.print(sum + " ");
}
}
} catch (IOException e) {
System.out.println("Error.\n Invalid or missing file.");
e.printStackTrace();
}
System.out.println("\n*** final " + sum);
}
}
If I understood correctly, for the input:
hello
how are you
You would like to get something like this as output:
hello 532
how are you 1059
*** final 1591
For this, you need to make some modifications to your code:
In addition to calculating the sum of characters values per line, keep another sum of the total of all lines
For each input line, print the line followed by the sum of character values
You don't need an array at all
It's better to trim the input line once, instead of for every character
Like this:
int total = 0;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String readLine;
while ((readLine = bufferedReader.readLine()) != null) {
String trimmed = readLine.trim();
int sum = 0;
for (int i = 0; i < trimmed.length(); i++) {
sum += (int) trimmed.charAt(i);
}
System.out.println(readLine + " " + sum);
total += sum;
}
} catch (IOException e) {
System.out.println("Error.\n Invalid or missing file.");
e.printStackTrace();
}
System.out.println("\n*** final " + total);
After your for loop, set sum to 0. If you want to print the total sum, then you need another variable, say t.
Like this:
for (int i = 0; i < readLine.length(); i++) {
Arrays.fill(array, readLine.trim().charAt(i));
sum += (int) array[i];
System.out.print(sum + " ");
}
t=t+sum;
sum=0;
Then print t at the end.
A simple solution would be to limit the scope of the sum variable. That way, values will not persist between runs:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.String;
import java.util.Arrays;
public class SumLines {
public static void main(String[] args) {
String filePath = "/home/lines.txt";
String readLine;
int totalSum = 0;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String readLine;
while ((readLine = bufferedReader.readLine()) != null) {
int sum = 0;
for (int i = 0; i < readLine.length(); i++) {
sum += (int) readLine.charAt(i);
}
System.out.println(readLine + ": " + sum);
totalSum += sum;
}
} catch (IOException e) {
System.out.println("Error.\n Invalid or missing file.");
e.printStackTrace();
}
System.out.println("\n*** final " + totalSum);
}
}
Also, you don't have to use such complicated stuff just to get the Unicode value of a char. I made some improvements.
Have two variables, one for final sum and one for line sum.
public class SumLines {
public static void main(String[] args) {
String filePath = "/home/lines.txt";
String readLine;
int totalSum = 0;
int lineSum = 0
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
while ((readLine = bufferedReader.readLine()) != null) {
char[] array = new char[readLine.length()];
System.out.println(readLine);
for (int i = 0; i < readLine.length(); i++) {
Arrays.fill(array, readLine.trim().charAt(i));
lineSum += (int) array[i];
System.out.print(lineSum + " ");
}
totalSum += lineSum + totalSum;
lineSum = 0;
}
} catch (IOException e) {
System.out.println("Error.\n Invalid or missing file.");
e.printStackTrace();
}
System.out.println("\n*** final " + totalSum);
}
}
I have a java file, FileJava.java like this:
public class FileJava {
public static void main(String args[]) {
for (int i = 0; i < 5; i++) {
}
}
}
Then, i read above code line by line using this code:
import java.util.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
public class FileReplace {
List<String> lines = new ArrayList<String>();
String line = null;
public void doIt() {
try {
File f1 = new File("FileJava.java");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains("for"))
{
lines.add("long A=0;");
if(line.contains("(") && line.contains(")")){
String get = line;
String[] split = get.split(";");
String s1 = split[0];
String s2 = split[1];
String s3 = split[2];
}
}
lines.add(line);
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for(String s : lines)
out.write(s);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
FileReplace fr = new FileReplace();
fr.doIt();
}
}
The question is, how to read character between '(' and ')' inside (for) in the FileJava.java, the character i mean "int i = 0; i < 5; i++" that will be stored in a variable, i have split based on ";", but when i print, the value :
s1 = for (int i = 0
s2 = i < 5
s3 = i++) {
While i expect:
s1 = int i = 0
s2 = i < 5
s3 = i++
Thanks
To answer your question how to restrict the splitting to the parenthesized section:
String[] split =
get.substring( get.indexOf('(')+1, get.indexOf(')').split("\\s*;\\s*");
Edit to address another prob.
Printing of the file will all happen in one line, because BufferedReader.readLine strips the line ends (LF, CRLF) from the line it returns. Thus, add a line break when writing:
for(String s : lines){
out.write(s);
out.newLine();
}
int index1 = line.indexOf("(");
int index2 = line.indexOf(")");
line = line.subString(index1 + 1, index2);
Its because you are splitting on ';' for the input
for (int i = 0; i < 5; i++) {
which will return all the characters between ';'
You can write a new method, called getBracketContent for example, that will be something like
String getBracketContent(String str)
{
int startIdx = str.indexOf('(')
int endIdx = str.indexOf(')')
String content = str.subString(startIdx + 1, endIdx);
}
then your code would be
if(line.contains("(") && line.contains(")")){
String get = getBracketContent(line);
String[] split = get.split(";");
Ideally I would use regular expressions to parse the information you need, but that is probably something you may want to look into later.
If you want to read the contents of a java file, you would be much better off using an AST (Abstract Syntax Tree) parser which will read in the contents of the file and then callback when it encounters certain expressions. In your case, you can listen just for the 'for' loop.
here is my code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class csvimport5 {
public static void main(String[] args) throws IOException {
double [][] data = new double [87][2];
File file = new File("buydata.txt");
int row = 0;
int col = 0;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null && row < data.length)
{
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens())
{
//get next token and store it in the array
data[row][col] = Double.parseDouble(st.nextToken());
col++;
}
col = 0;
row++;
}
System.out.println(" "+data[87][2]);
}
}
it shows error:-numberformatException :empty string
pls help me
At some point in your file, the st.nextToken() is returning an empty string. Because you're trying to parse that into a Double, you're getting an error (there are no numbers in an empty string to get a double from).
The most common reason for this is bad input data. Are you able to provide a subset of your buydata.txt file, which causes the bug to occur?
As suggested by Erica, the cause of the error is some badly formatted string in your input file, which cause parseDouble() to throw a NumberFormatException. You should sorround it into a try ... catch. You could something like this:
// set val to a constant value that you know is not acceptable
dobule val = UNACCEPTABLE_VALUE;
String token = st.nextToken();
try {
val = Double.parseDouble(st.nextToken());
} catch (NumberFormatException e) {
System.err.println("bad value: " + token + " at line: " + line);
}
data[row][col] = val;
col++