How to Ignore the white Spaces while compare two files? [duplicate] - java

This question already has answers here:
How to compare two Strings in java without considering spaces?
(9 answers)
Closed 7 years ago.
I want to create a Unit test method. Version of Java - 1.6
#Test
public void TestCreateHtml() throws IOException{
final File output = parser.createHtml();
final File expected = new File("src/main/resources/head.jsp");
assertEquals("The files differ!", FileUtils.readLines(expected), FileUtils.readLines(output));
}
This test method doesn't work.
The contents of both files are equals, but they have different number of white spaces.
How can I ignore the white spaces?

If the problem is in leading/trailing white space:
assertEquals(actual.trim(), expected.trim());
If problem is in file contents, only solution I can think of is to remove all white space from both inputs:
assertEquals(removeWhiteSpaces(actual), removeWhiteSpaces(expected));
where removeWhiteSpaces() method looks like this:
String removeWhiteSpaces(String input) {
return input.replaceAll("\\s+", "");
}

If the problem is only leading/trailing white spaces, you can compare line by line after trimming both. This does not work if there can also be extra newlines in one file compared to the other.
#Test
public void TestCreateHtml() throws IOException{
final File output = parser.createHtml();
final File expected = new File("src/main/resources/head.jsp");
List<String> a = FileUtils.readLines(expected);
List<String> b = FileUtils.readLines(output);
assertEquals("The files differ!", a.size(), b.size());
for(int i = 0; i < a.size(); ++i)
assertEquals("The files differ!", a.get(i).trim(), b.get(i).trim());
}

Iterate over list and trim each line
List<String> result = new ArrayList<String>();
for(String s: FileUtils.readLines(expected)) {
result.add(s.trim());
}
Same with other file.
And then compare new lists.

Just remove the leading and trailing whitespace before comparing:
#Test
public void TestCreateHtml() throws IOException{
final File output = parser.createHtml();
final File expected = new File("src/main/resources/head.jsp");
assertEquals("The files differ!", FileUtils.readLines(expected).replaceAll("^\\s+|\\s+$", ""), FileUtils.readLines(output).replaceAll("^\\s+|\\s+$", ""));
}

Related

Java does not recognize \n when read from file

If I print directly as
System.out.println("a\nb");
the result will be as expected, with a new line between characters,
but if I read the same line from a text file using
public static void main(String[] args) throws IOException {
List<String> lines;
lines = Files.readAllLines(Paths.get("filename.txt"));
String[] array = lines.toArray(new String[0]);
System.out.println(array[0]);
the text displayed will be exactly as written in a file "a\nb" without a new line between the characters. toString and other methods do not help.
How should I update the code?
With the help of the colleagues and commenters got an explanation and an answer (not the most elegant approach though).
"\n" in the code is interpreted as a new line character, but when read from a file it becomes a set of two characters: "\" and "n" and is no longer properly recognized. The shortest way to eliminate the problem would be to replace these characters with the new line in the code of the program, adding "replace("\n", "\n")". So, the code becomes as follows:
public static void main(String[] args) throws IOException {
List<String> lines;
lines = Files.readAllLines(Paths.get("filename.txt"));
String[] array = lines.toArray(new String[0]);
System.out.println(array[0].replace("\\n", "\n"));

Parse lines with different number of string words [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I have a text file with the following format(the words at each line are seperated with tab):
stri1 stri2 stri3
stri4 stri5 stri6
stri7 stri8
stri9 stri0 stri5
As you can see i have some lines with only two words. I have a class to save the words of each line:
public class Entity{
private word1,word2,word3;
//constructor and getter/setter methods
}
I want to save the text values using the following code:
for(String i : filelines){
String[] line = i.split("\t");
if(line[2] == null){
listOfEntities.add(new Entity(line[0], line[1], null));
}
else{
listOfEntities.add(new Entity(line[0], line[1], line[2]));
}
When i try to execute this code i get an ArrayIndexOutOfBoundsException because some line have only 2 words. How can i handle this situation because i want also the null values in order to make some sql queries later.
Mod with length of array will return 0 if array exceed legth it will store zeroth index other than that you have to do multiple ifs as you have done for second index.
Another way I would suggest pass the whole array in Entity parameter and in Entity class run a for loop till array length which will run till available lines in array only.
for(String i : filelines){
String[] line = i.split("\t");
if(line[2 % line.length] == null){
listOfEntities.add(new Entity(line[0], line[1 % line.length], null));
}
else{
listOfEntities.add(new Entity(line[0], line[1 % line.length], line[2 % line.length]));
}
I have another option for you. You have to separate the options in the file with an element. This looks like this:
stri1-stri2-stri3
stri4-stri5-stri6
stri7-stri8
stri9-stri0-stri5
For this you have to change your entity class a little bit:
public static class Entity {
private String[] words;
public Entity(String[] words) {
this.words = words;
}
public String[] getWords() {
return words;
}
}
To read out the whole thing now, I wrote you a code.
List<Entity> array = new ArrayList<>();
Scanner scanner = new Scanner(new File("MyFile.txt"));
while (scanner.hasNext()) {
String line = scanner.next();
String[] options = line.split("-");
array.add(new Entity(options));
}
array.forEach(entity -> {
System.out.println("Entity Words > " + Arrays.toString(entity.getWords()));
});
I hope I could help you with this.

How to eliminate the trailing white space after an ArrayList? [duplicate]

This question already has answers here:
Simple way to compare 2 ArrayLists
(10 answers)
Closed 3 years ago.
I am trying to create a test that asserts that two ArrayLists are equal. When running the test, I am getting an error that there is one difference between the expected and actual: there is a whitespace at the end of the arraylist.
The problem isn't caused by anything inside the arraylist, the problem is after the close of the arraylist. As far as I know, there is no trim function for this in java or a junit test which ignores whitespace.
ArrayList<Quote> expected = new ArrayList<>();
Quote q1 = new Quote("Test Test", "This is a quote.");
Quote q2 = new Quote("Author Two", "Quote 2.");
Quote q3 = new Quote("Unknown", "This quote does not have an author.");
Quote q4 = new Quote("Author Three-three", "Quote 3.");
expected.add(q1); expected.add(q2); expected.add(q3); expected.add(q4);
What this gives me is: java.util.ArrayList<[This is a quote. - Test Test, Quote 2. - Author Two, This quote does not have an author. - Unknown, Quote 3. - Author Three-three]>_
(I put a underscore where the whitespace is)
What I expect is: java.util.ArrayList<[This is a quote. - Test Test, Quote 2. - Author Two, This quote does not have an author. - Unknown, Quote 3. - Author Three-three]>
This does not include a whitespace.
I don't know why I am getting the whitespace nor do I know how to get rid of it. Any help would be appreciated.
For more context, the entire tester file
public class ImportQuotesTest {
BufferedReader reader;
#Before
public void setUp() throws Exception {
File file = new File("C:\\Users\\homba\\Documents\\QuotesProject\\src\\ImportQuotesTest.txt");
reader = new BufferedReader(new FileReader(file));
}
#Test
public void fillList() throws IOException {
ArrayList<Quote> actual = ImportQuotes.fillList(reader);
ArrayList<Quote> expected = new ArrayList<>();
Quote q1 = new Quote("Test Test", "This is a quote.");
Quote q2 = new Quote("Author Two", "Quote 2.");
Quote q3 = new Quote("Unknown", "This quote does not have an author.");
Quote q4 = new Quote("Author Three-three", "Quote 3.");
expected.add(q1); expected.add(q2); expected.add(q3); expected.add(q4);
Assert.assertEquals(expected,actual);
}
}```
Ah yeah, I stumbled into that once! First of all, if you're doing a unit test, just iterate through the whole list and assert each element or build a String yourself. I think I saw this in eclipse. It's running its own version of junit somehow and I think the tests pass when running from mvn test in command line.
To answer your question short:
You can use Hamcrest if you can: Assert about a List in Junit
Iterate:
for (int i = 0; i< list1.size; i++) { assertEquals(list1.get(i), list2.get(i)); }
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
list1.forEach(sb1::append);
list2.forEach(sb1::append);
assertEquals(sb1.toString(), sb2.toString()
Prefered way is number 1 or 3.

Regex to match a String with asterisk

I'm coding in Java and I want to split my string. I want to split it at.
/* sort */
Yes I plan to split a .java file that I have read as a string so I need it to include "/* sort */". I'm creating a code that sorts Arrays that are predefined in java class file.
Exactly that and do another split at
}
and then I wanted help how to go about splitting up the array since I'll be left with
an example would be this
final static String[] ANIMALS = new String[] /* sort */ { "eland", "antelope", "hippopotamus"};
My goal would be to sort that Array inside a .java file and replace it. This is my current code
private void editFile() throws IOException {
//Loads the whole Text or java file into a String
try (BufferedReader br = new BufferedReader(new FileReader(fileChoice()))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
everything = sb.toString();
}
arrayCutOff = everything.split("////* sort *////");
for(int i = 0; i < arrayCutOff.length; i++){
System.out.println(arrayCutOff[i]);
}
}
This basically reads the whole .txt or .java file completely with the exact same formatting into one string. I planned to split it at /* sort */ and sort the array inside but I realized if I did that I probably can't replace it.
Considered your're using java 8 you might go this direction:
private void editFile() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fileChoice()));
String content = lines.stream().collect(Collectors.joining(System.lineSeparator()));
Stream.of(content.split(Pattern.quote("/* sort */"))).forEach(System.out::println);
}
However, the trick you're asking for is Pattern.quote, which dates back Java 5. It'll qoute a literal so it can be used as a literal in regExs and is a bit more convenient (and reliable I think) than wrestling around with backslashes...

Splitting and saving data in Java

I'm trying to read a data file and save the different variables into an array list.
The format of the data file looks a little like this like this
5003639MATH131410591
5003639CHEM434111644
5003639PSYC230110701
Working around the bad formatting of the data file, I added commas to the different sections to make a split work. The new text file created looks something like this
5,003639,MATH,1314,10591
5,003639,CHEM,4341,11644
5,003639,PSYC,2301,10701
After creating said file, I tried to save the information into an array list.
The following is the snippet of trying to do this.
FileReader reader3 = new FileReader("example.txt");
BufferedReader br3 = new BufferedReader(reader3);
while ((strLine = br3.readLine())!=null){
String[] splitOut = strLine.split(", ");
if (splitOut.length == 5)
list.add(new Class(splitOut[0], splitOut[1], splitOut[2], splitOut[3], splitOut[4]));
}
br3.close();
System.out.println(list.get(0));
The following is the structure it is trying to save into
public static class Class{
public final String recordCode;
public final String institutionCode;
public final String subject;
public final String courseNum;
public final String sectionNum;
public Class(String rc, String ic, String sub, String cn, String sn){
recordCode = rc;
institutionCode = ic;
subject = sub;
courseNum = cn;
sectionNum = sn;
}
}
At the end I wanted to print out one of the variables to see that it's working but it gives me an IndexOutOfBoundsException. I wanted to know if I'm maybe saving the info incorrectly, or am I perhaps trying to get it to print out incorrectly?
You have a space in your split delimiter specification, but no spaces in your data.
String[] splitOut = strLine.split(", "); // <-- notice the space?
This will result in a splitOut array of only length 1, not 5 like you expect.
Since you only add to the list when you see a length of 5, checking the list for the 0th element at the end will result in checking for the first element of an empty list, hence your exception.
If you expect your data to have a comma or a space separating the characters then you would alter the split line to be:
String[] splitOut = strLine.split("[, ]");
The split takes a regular expression as an argument.
Rather than artificially adding commas I would look at String.substring in order to cut the line you have read into pieces. For example:
while ((strLine = br3.readLine())!=null) {
if (strLine.length() != 20)
throw new BadLineException("line length is not valid");
list.add(new Class(strLine.substring(0,1), strLine.substring(1,7), strLine.substring(7,11), strLine.substring(11,15), strLine.substring(15,19)));
}
[ Untested: my numbers might be out because I a bit knacked, but you get the idea ]

Categories

Resources