Java - substring issues - java

I'm going to show all of my code here so you guys get a gist of what I'm doing.
import java.io.*;
import java.util.*;
public class Plagiarism {
public static void main(String[] args) {
Plagiarism myPlag = new Plagiarism();
if (args.length == 0) {
System.out.println("Error: No files input");
}
else if (args.length > 0) {
try {
List<String> foo = new ArrayList<String>();
for (int i = 0; i < 2; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
}
int blockSize = Integer.valueOf(args[2]);
System.out.println(args[2]);
// String line = foo.toString();
List<String> list = new ArrayList<String>();
for (int k = 0; k < foo.size() - blockSize; k++) {
list.add(foo.toString().substring(k, k+blockSize));
}
System.out.println(list);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();
while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z]","").toLowerCase());
}
return myList;
}
}
This is the code that is using substring.
int blockSize = Integer.valueOf(args[2]);
//"foo" is an ArrayList<String> which I have to convert toString() to use substring().
String line = foo.toString();
List<String> list = new ArrayList<String>();
for (int k = 0; k < line.length() - blockSize; k++) {
list.add(line.substring(k, k+blockSize));
}
System.out.println(list);
When I specify blockSize as 4 in cmd this is the result:
[[, a, , ab, abc ]
the text file (standardised using my other code) is this:
abcdzaabcdd
so the result should be this:
[abcd, bcdz, cdza, ] etc.
Any help?
Thanks in advance.

Here is code showing how to improve a little your code. Main change is returning simplified string from simplify method instead of List<String> of simplified lines, which after converting it to string returned String in form
[value0, value1, value2, ...]
Now code returns String in form value0value1value2.
Another change is lowering indentation lever by removing unnecessary else if statement and braking control flow with System.exit(0); (you can also use return; here).
class Plagiarism {
public static void main(String[] args) throws Exception {
//you are not using 'myPlag' anywhere, you can safely remove it
// Plagiarism myPlag = new Plagiarism();
if (args.length == 0) {
System.out.println("Error: No files input");
System.exit(0);
}
String foo = null;
for (int i = 0; i < 2; i++) {
BufferedReader reader = new BufferedReader(new FileReader(args[i]));
foo = simplify(reader);
System.out.println(foo);
}
int blockSize = Integer.valueOf(args[2]);
System.out.println(args[2]);
List<String> list = new ArrayList<String>();
for (int k = 0; k < foo.length() - blockSize; k++) {
list.add(foo.toString().substring(k, k + blockSize));
}
System.out.println(list);
}
public static String simplify(BufferedReader input)
throws IOException {
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = input.readLine()) != null) {
sb.append(line.replaceAll("[^a-zA-Z]", "").toLowerCase());
}
return sb.toString();
}
}

Related

How to find the longest sequence of strings in order, but not necessarily contiguous

I have a program that reads input from two text files:
answersA.txt
10
US Independence
French Revolution
WW I
Great Depression
WW II
Korean War
British Invasion
Vietnam War
Gulf War
Dot Com Era
myanswers1.txt
7
Korean War
British Invasion
WW I
Vietnam War
Great Depression
US Independence
French Revolution
The first line is the how many values there will be. I'm trying to find the longest pattern between the two files. For these cases, the longest pattern is "Korean War,British Invasion,Vietnam War" and the score from the grade() method would be 5, since there were 5 answers right. I'm trying to use the dynamic programming approach, and although I can figure out how to do this for a singular string, I am struggling to figure out how to do it for an array of strings (if that's even the best way to handle it).
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Patterns {
public int grade;
public String LHP;
String[] answers = null;
String[] myAnswers = null;
public Patterns(String filename) {
List<String> readAnswers = new ArrayList<String>();
try {
FileInputStream newFile = new FileInputStream(filename);
DataInputStream data_input = new DataInputStream(newFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
if ((line.length() != 0)) {
readAnswers.add(line);
}
}
buffer.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(readAnswers);
answers = new String[readAnswers.size()];
answers = readAnswers.toArray(answers);
}
public int grade(String filename) {
List<String> readMyAnswers = new ArrayList<String>();
try {
FileInputStream newFile = new FileInputStream(filename);
DataInputStream data_input = new DataInputStream(newFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String line;
while ((line = buffer.readLine()) != null) {
if ((line.length() != 0)) {
readMyAnswers.add(line);
}
}
buffer.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(readMyAnswers);
myAnswers = new String[readMyAnswers.size()];
myAnswers = readMyAnswers.toArray(myAnswers);
grade = LCS(answers, myAnswers);
return grade;
}
public String pattern(String filename) {
return LHP;
}
public static int LCS(String[] A, String[] B) {
int[][] LCS = new int[A.length + 1][B.length + 1];
String[][] solution = new String[A.length + 1][B.length + 1];
for (int i = 0; i <= B.length; i++) {
LCS[0][i] = 0;
solution[0][i] = "0";
}
for (int i = 0; i <= A.length; i++) {
LCS[i][0] = 0;
solution[i][0] = "0";
}
for (int i = 1; i <= A.length; i++) {
for (int j = 1; j <= B.length; j++) {
if (A[i - 1] == B[j - 1]) {
LCS[i][j] = LCS[i - 1][j - 1] + 1;
} else {
LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
}
return LCS[A.length][B.length];
}
public static void main(String[] args) {
Patterns test1 = new Patterns("answersA.txt");
System.out.println("The score is: " + test1.grade("myanswers1.txt"));
}
}

Manipulating strings and integers via two dimensional array from an external file java

I am trying to design a program that takes data from an external file, stores the variable to arrays and then allows for manipulation.sample input:
String1 intA1 intA2
String2 intB1 intB2
String3 intC1 intC2
String4 intD1 intD2
String5 intE1 intE2
I want to be able to take these values from the array and manipulate them as follows;
For each string I want to be able to take StringX and computing((intX1+
intX2)/)
And for each int column I want to be able to do for example (intA1 + intB1 + intC1 + intD1 + intE1)
This is what I have so far, any tips?
**please note java naming conventions have not been taught in my course yet.
public class 2D_Array {
public static void inputstream(){
File file = new File("data.txt");
try (FileInputStream fis = new FileInputStream(file)) {
int content;
while ((content = fis.read()) != -1) {
readLines("data.txt");
FivebyThree();
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static int FivebyThree() throws IOException {
Scanner sc = new Scanner(new File("data.txt"));
int[] arr = new int[10];
while(sc.hasNextLine()) {
String line[] = sc.nextLine().split("\\s");
int ele = Integer.parseInt(line[1]);
int index = Integer.parseInt(line[0]);
arr[index] = ele;
}
int sum = 0;
for(int i = 0; i<arr.length; i++) {
sum += arr[i];
System.out.print(arr[i] + "\t");
}
System.out.println("\nSum : " + sum);
return sum;
}
public static String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
}
return lines.toArray(new String[lines.size()]);
}
/* int[][] FivebyThree = new int[5][3];
int row, col;
for (row =0; row < 5; row++) {
for(col = 0; col < 3; col++) {
System.out.printf( "%7d", FivebyThree[row][col]);
}
System.out.println();*/
public static void main(String[] args)throws IOException {
inputstream();
}
}
I see that you read data.txt twice and do not use first read result at all. I do not understand, what you want to do with String, but having two-dimension array and calculate sum of columns of int is very easy:
public class Array_2D {
static final class Item {
final String str;
final int val1;
final int val2;
Item(String str, int val1, int val2) {
this.str = str;
this.val1 = val1;
this.val2 = val2;
}
}
private static List<Item> readFile(Reader reader) throws IOException {
try (BufferedReader in = new BufferedReader(reader)) {
List<Item> content = new ArrayList<>();
String str;
while ((str = in.readLine()) != null) {
String[] parts = str.split(" ");
content.add(new Item(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
}
return content;
}
}
private static void FivebyThree(List<Item> content) {
StringBuilder buf = new StringBuilder();
int sum1 = 0;
int sum2 = 0;
for (Item item : content) {
// TODO do what you want with item.str
sum1 += item.val1;
sum2 += item.val2;
}
System.out.println("str: " + buf);
System.out.println("sum1: " + sum1);
System.out.println("sum2: " + sum2);
}
public static void main(String[] args) throws IOException {
List<Item> content = readFile(new InputStreamReader(Array_2D.class.getResourceAsStream("data.txt")));
FivebyThree(content);
}
}

How to eliminate empty columns to be inserted to DB if CSV files contains empty columns?

I have a table in database with 5 columns. In CSV file I have data like below. How to process it into Oracle database by elimating empty columns (because CSV file contains 6 columns, it will mismatch in database columns having 5 columns).
111, ,John,2000, ,US
222, ,Alle,3000, ,China
333, ,Kite,4000,LCD,IND
444, ,King,5000,LED,Aust
package com.java;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.Vector;
public class Test3
{
public static void main(String[] args)
{
try
{
int commas = 0;
List data = new Vector();
List columnCount = new Vector();
String[] cols = null;
String[] strArray = null;
String file = "D:/temp/CSV/data.csv";
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
do
{
commas = 0;
cols = line.split(",");
strArray = new String[cols.length];
int i=0;
for (String element : cols)
{
if (!isBlank(element))
{
strArray[i] = element;
i++;
}
}
int newFile = line.length();
for(int k = 0; k < newFile; k ++ )
{
char eachChar = line.charAt(k);
if(eachChar == ',')
{
commas ++;
}
}
data.add(strArray);
line = br.readLine();
}
while (line != null);
Vector columns = new Vector(commas + 1);
for(int i = 0; i < commas +1 ; i ++ )
{
columns.add("" + i);
}
} catch (Exception e) {
}
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
{
return true;
}
for (int i = 0; i < strLen; i++)
{
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
}
You can use a util method to check if it is empty string or not.
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
and use it in your reader like below
Scanner s = null;
try {
s = new Scanner(new File("D:/sri/Test.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNextLine()) {
String[] line = s.nextLine().split(",");
for (String element : line) {
if (!isBlank(element))
System.out.println(element);
}
}

Java - Comparing two huge text files

I am trying to develop a basic java program to compare two huge text files and print non matching records .i.e. similar to minus function in SQL. but I am not getting the expected results because all the records are getting printed even though both files are same. Also suggest me whether this approach is performance efficient for comparing two huge text files.
import java.io.*;
public class CompareTwoFiles {
static int count1 = 0 ;
static int count2 = 0 ;
static String arrayLines1[] = new String[countLines("\\Files_Comparison\\File1.txt")];
static String arrayLines2[] = new String[countLines("\\Files_Comparison\\File2.txt")];
public static void main(String args[]){
findDifference("\\Files_Comparison\\File1.txt","\\Files_Comparison\\File2.txt");
displayRecords();
}
public static int countLines(String File){
int lineCount = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(File));
while ((br.readLine()) != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
public static void findDifference(String File1, String File2){
String contents1 = null;
String contents2 = null;
try
{
FileReader file1 = new FileReader(File1);
FileReader file2 = new FileReader(File2);
BufferedReader buf1 = new BufferedReader(file1);
BufferedReader buf2 = new BufferedReader(file2);
while ((contents1 = buf1.readLine()) != null)
{
arrayLines1[count1] = contents1 ;
count1++;
}
while ((contents2 = buf2.readLine()) != null)
{
arrayLines2[count2] = contents2 ;
count2++;
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void displayRecords() {
for (int i = 0 ; i < arrayLines1.length ; i++) {
String a = arrayLines1[i];
for (int j = 0; j < arrayLines2.length; j++){
String b = arrayLines2[j];
boolean result = a.contains(b);
if(result == false){
System.out.println(a);
}
}
}
}
}
Based upon your explanation you do not need embedded loops
consider
public static void displayRecords() {
for (int i = 0 ; i < arrayLines1.length && i < arrayLines2.length; i++)
{
String a = arrayLines1[i];
String b = arrayLines2[i];
if(!a.contains(b){
System.out.println(a);
}
}
For the performance wise, you should try to match the size of the files. If the sizes(in bytes) are exactly the same, you might not need to compare them.

Java two different documents, done stuff but now they are one.. want them separate

Here is all of my code for you guys to see, I will explain the problem below:
import java.io.*;
import java.util.*;
public class Plagiarism {
public static void main(String[] args) {
Plagiarism myPlag = new Plagiarism();
if (args.length == 0) {
System.out.println("Error: No files input");
}
else if (args.length > 0) {
try {
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}
public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();
while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
}
return myList;
}
}
Right, what this does is print out this:
abcdefabcd123abcdz456aabcdd
This result is actually two separate documents, text1.txt and text2.txt combined into one line on the cmd. What I need is these two documents to be standardised and then printing out as seperate elements for each value they each hold, rather than all together as one, because I need to then use them as 2 separate things (or objects) for the next part of my code.
Hopefully this is not too confusing and you clever people can help me out! Thanks in advance!
You are just printing out the result, so as it runs through the loop, it will print it all out on one line.
You could store the results in a separate array, or you could just do this to have them each print on a different line:
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
System.out.println();
}
The new println will separate each entry.
check this one will print data line by line
abcdefabcd123
abcdz456aabcdd
public static void main(String[] args) {
Plagiarism myPlag = new Plagiarism();
if (args.length == 0) {
System.out.println("Error: No files input");
}
else if (args.length > 0) {
try {
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
System.out.println();
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}
public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();
while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
}
return myList;
}
}
It gets printed into one line because you use System.out.print(), you could just use System.out.println() if you want to start a new line with each output.
But that is just a problem of the output. Internally you already have the data separated in your list, so what is your actual problem?
If you want to hold the file contents in two different objects do the following..
List<ArrayList<String>> files = new ArrayList<ArrayList<String>>();
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
files.add(simplify(reader));
for(ArrayList<String> al : files)
{
for (int j = 0; j < al.size(); j++) {
System.out.print(al.get(j));
}
System.out.println();
}
}
and change your second method to
public static ArrayList<String> simplify(BufferedReader input) throws IOException

Categories

Resources