how to coverage write file func in java - java

I made a simple function? to which I give a list of objects and then get their headers (just some strings), then sort and write to a file
when i made a test for this func, coverage was 0 lines and i dont know why
public static void writeToFile(List<Appliances> appliances) throws IOException {
try (FileWriter writer = new FileWriter("\\result.csv")) {
appliances.stream().sorted(Comparator.comparing(Appliances::getName)).collect(Collectors.toList());
String previousClassName = "";
for (var app : appliances) {
if (!app.getClass().getSimpleName().equals(previousClassName)) {
writer.write(app.getHeaders());
writer.write("\r\n");
writer.write(app.toCSV());
writer.write("\r\n");
previousClassName = app.getClass().getSimpleName();
}
}
}
}
so here is my unit test code, also I'm not native english speaker, dont write clever words pls, just a code and a little text would be a perfect solution, thanks a lot
#Test
void writeToFile() throws Exception {
try (FileReader expected = new FileReader(String.valueOf(path));
BufferedReader expectedBR = new BufferedReader(expected);
FileReader result = new FileReader(String.valueOf(path2));
BufferedReader resultBR = new BufferedReader(result);
) {
String line1;
String line2;
while ((line1 = expectedBR.readLine()) != null & (line2 = resultBR.readLine()) != null) {
Assertions.assertEquals(line1, line2);
}
}
}

Related

Storing values in different variables by reading a file

i'm a beginner at java and still learning so please excuse my question if it sounds stupid.
i've been stuck on a straight forward problem i was given:
i'm supposed to read a text file and store the values of the text file in different variables. my text file looks like:
foo.txt
Directory_path=C:\University
school_name=SyracuseUni
i want to store the directory path and school_name in a new variable say
var_one = C:\University
and var_two = SyracuseUni
I was able to split it but in a single string.
public static void main(String[] args) throws IOException {
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\foo.txt"));
String strLine = null;
String var_one = null;
String var_two = null;
while ((strLine = br.readLine()) != null) {
String[] parts = strLine.split("=");
String parameter = parts[1];
System.out.println(parameter);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
this gives me an output like this which isn't how i want it:
C:\University
SyracuseUni
i will appreciate if anyone can guide me towards the right approach. thanks all.
There is already a simple way to deal with such files using java.util.Properties class. This could be an overkill if you are simply trying to learn how to read a file.
public static void main(String[] args) {
String myVar1 = null;
String myVar2 = null;
Properties prop = new Properties();
InputStream input = null;
try (FileInputStream input = new FileInputStream("pathToYourFile")) {
prop.load(input);
myVar1 = prop.getProperty("Directory_path");
myVar2 = prop.getProperty("school_name");
} catch (IOException ex) {
//Handle exception
}
}
Something simple would be using Java Properties. You could also store values in a map. If you really insisted on filling two separate varibles, you could always count how many lines you've went across in your while loop and use switch/case to determine which variable to fill.
public static void main(String[] args) throws IOException {
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\foo.txt"));
String strLine = null;
HashMap<String, String> map = new HashMap<String, String>();
while ((strLine = br.readLine()) != null) {
String[] parts = strLine.split("=");
map.put(parts[0], parts[1]);
}
for (Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " = " + value);
}
}
catch (IOException e) {
e.printStackTrace();
}
}

Counting values in a column of a .csv file Java

I'm tasked, by my professor, to write a program to read a .csv file (778 rows (including header row), 8 columns).
I need to:
1. Write a method to receive the name of the file as its argument and then print the number of lines in the file.
public void printNumberOfLinesInFile(String fileName)
Write a method to receive the name of the file as its argument and then print the number of private and non-private colleges.
public void printNumberOfPrivateNonPrivateCollegesInFile(String fileName)
Write a method to receive the name of the file as its argument and then print the private college name with largest out of state tuition.
public void printMostExpensivePrivateCollegeInFile(String fileName)
Write a method to receive the name of the file as its argument and then print the non-private college with largest out of state tuition.
public void printMostExpensiveNonPrivateCollegeInFile(String fileName)
Write a method to receive the name of the file as its argument and then print the number of applications and the number of applicants that are accepted for private and non-private colleges.
public void printNumberOfApplications(String fileName)
Write a method to receive the name of the file as its argument and then print following information for private and non-private colleges.
Average of expenses for books.
Average of expenses for room.
Average of personal expenses.
public void printAverageOfExpenses(String fileName)
Disclaimer: I do not want anyone to do my homework for me. I need to learn so I can apply my knowledge when I graduate and enter industry. I'm simply asking for a hint or a better way at writing the code.
My code thus far:
public class Week14
{
public String data;
public void printNumberOfLinesInFile(String inFile) throws IOException
{
int collegeCount = 0;
FileReader fileRead = new FileReader(inFile);
BufferedReader bufferRead = new BufferedReader(fileRead);
while(true)
{
String line = bufferRead.readLine();
if(line == null)
{
break;
}
collegeCount++;
//System.out.println(line);
}
System.out.println(collegeCount-1 + " Colleges total.");
}
public void printNumberOfPrivateNonPrivateCollegesInFile(String inFile) throws IOException
{
int privateCount = 0;
int nonprivateCount = 0;
int count = 0;
FileReader fileRead = new FileReader(inFile);
BufferedReader bufferRead = new BufferedReader(fileRead);
while((data = bufferRead.readLine())!= null)
{
String line = bufferRead.readLine();
String [] lineItems = line.split(",");
for(int i = 0; i < line.length(); i++)
{
if(lineItems[i].equals("Yes"))
{
privateCount++;
}
}
break;
}
System.out.println(privateCount+" private Colleges.");
System.out.println(nonprivateCount+ " non-private Colleges.");
}
public void printMostExpensivePrivateCollegeInFile(String inFile) throws FileNotFoundException, IOException
{
int mostExpensive = 0;
int currentExpensive = 0;
FileReader fileRead = new FileReader(inFile);
BufferedReader bufferRead = new BufferedReader(fileRead);
while((data = bufferRead.readLine())!= null)
{
String line = bufferRead.readLine();
if(line.equals("OutstateTuition"))
{
System.out.println(line);
}
else
{
System.out.println(line);
}
}
}
public void printMostExpensiveNonPrivateCollegeInFile(String fileName)
{
}
public void printNumberOfApplications(String fileName)
{
}
public void printAverageOfExpenses(String fileName)
{
}
public static void main(String[] args) throws FileNotFoundException, IOException
{
File inFile = new File("College.csv");
FileReader fileReader = new FileReader(inFile);
BufferedReader buffReader = new BufferedReader(fileReader);
Week14 w1 = new Week14();
//w1.printNumberOfLinesInFile("College.csv");
w1.printNumberOfPrivateNonPrivateCollegesInFile("College.csv");
//^^^The above line goes into an infinite loop^^^
//w1.printMostExpensivePrivateCollegeInFile("College.csv");
}
}
The problem is, I'm stuck on trying to count the amount of private and nonprivate colleges. In my method, printNumberOfPrivateNonPrivateCollegesInFile (line 39), I'm running into an exception: java.lang.ArrayIndexOutOfBoundsException: 8
I've asked my professor how I can avoid this, I've looked online and the problem seems to lie with the iterator int 'i'. I'm trying to traverse the array, and 'i' is out of bounds. When I put a '1' in
if(lineItems[i].equals("Yes"))
for my privateCount, there is an output of 67 from privateCount, (I think it is counting the individual characters for some reason).
My question, what would be the most effective way to traverse the entire .csv file, and to access individual columns so I can count them and output them?
Any help would be greatly appreciated.
edit:
I have changed the while loop:
while(true)
{
String line = bufferRead.readLine();
String [] lineItems = line.split(",");
if(line == null)
{
break;
}
for (String lineItem : lineItems) {
privateCount++;
}
}
Now I can traverse the entire .csv file, but I'm receiving a java.lang.NullPointerException when I try and count.
edit 2:
I've redone my while loop again,
while(true)
{
String line = bufferRead.readLine();
String [] lineItems = line.split(",");
for (String lineItem : lineItems) {
if (lineItem.equals("Yes")) {
privateCount++;
}
}
System.out.println(privateCount);
}
I'm now counting the right value for privateCount, but there's a NullPointerException at :
String [] lineItems = line.split(",");
and the loop will not let me put my 'echo' outside of the while-loop without a 'break' statement.
With respect to actual industry-level code, and assuming that assignment did not specifically focus on actual CSV decoding, I would recommend finding and using a library to handle low-level decoding, such as OpenCSV, SuperCSV or CSV-module for Jackson.
This way your code can focus on more interesting part of finding specific values, and not on intricate details like possible escaping and/or quoting of contents.
If the focus is on CSV edge cases this is not the approach to use; but for real production code one hopefully rarely if ever writes the actual low-level decoding part, given the existence of multiple good libraries for the task.
if(lineItems != null && lineItems.length>0){
// do your loop
}
if (lineItem!= null && !lineItem.trim().equals("") && lineItem.equals("Yes")) {
privateCount++;
}
most likely will prevent your null issue
public void printNumberOfPrivateNonPrivateCollegesInFile(String inFile) throws IOException
{
int privateCount = 0;
int nonprivateCount = 0;
FileReader fileRead = new FileReader(inFile);
BufferedReader bufferRead = new BufferedReader(fileRead);
try
{
while(true)
{
String line = bufferRead.readLine();
String [] lineItems = line.split(",");
//System.out.println(Arrays.toString(lineItems));
for (String lineItem : lineItems)
{
if (lineItem!= null && !lineItem.trim().isEmpty() && lineItem.equals("No"))
{
nonprivateCount++;
}
if (lineItem!= null && !lineItem.trim().isEmpty() && lineItem.equals("Yes"))
{
privateCount++;
}
}
//System.out.println(privateCount);
}
}
catch(NullPointerException npe)
{
}
System.out.println(privateCount);
System.out.println(nonprivateCount);
}
Fixed it. I'm now just catching the exception so it isn't as annoying. Thanks all for the help.

ArrayIndexOutOfBounds when trying to split a line from reader

I am trying to write a java program to parse relevant strings from a .txt file with a certain format.
I want to use the contents of the .txt file to initiate data for my classes. A sample file would look like this:
Movies
Lord of the Rings: 180
Fight Club: 120
...
Theaters
A:100
B:50
C:200
...
Shows
1,1,960
1,1,1080
1,1,1200
1,3,1020
1,3,1140
2,2,990
2,2,1210
...
Prices
Adult:10
Child:7
Senior:8
...
End
This is what I have so far (and it is returning an error when trying to read the above file to initialize my class.
public static void inititializeFromFile(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while((line = reader.readLine()) != null) {
if(line.equals("Movies")) {
while (!(line.equals("Theaters"))) {
String currentline = line;
String[] parts = currentline.split(":");
String part1 = parts[0];
String part2 = parts[1];
movies.add(new Movie(part1, part2));
}
}
// do basic string comparisons here
if(line.equals("...")) {
// do something
}
else if(line.contains(":")) {
// most likely of type A:100, B:50
}
else if(line.equals("End")) {
// do something
}
else {
// anything else
}
}
reader.close();
}
}
Here is a sample program that will read in the file for you, line by line, and has some scenarios to determine what type of line we are looking at. I was lazy and threw the IOExceptions that might be thrown at me in the code - you should never do this, instead modify the program to use a try catch.
import java.io.*;
public class tmp {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line;
while((line = br.readLine()) != null) {
// do basic string comparisons here
if(line.equals("...")) {
// do something
}
else if(line.contains(":")) {
// most likely of type A:100, B:50
}
else if(line.equals("End")) {
// do something
}
else {
// anything else
}
}
br.close();
}
}

text File Dictionary Java

Im coding a dictionary in java that the user inputs the words and they are stored in a .txt file in alphabetical order. When I use the SortWords method on its own it works fine, but when I combine it with the Option1Method which adds words to the dictionary it dosent work. Its like it changes the file type of the text file or something?? Any help would be greatly appreciated
public static void Option1Method() throws IOException {
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Ok Because I couldn't let this question go unanswered here is a simple example of how you might do this using some built in classes:
import java.io.*;
import java.util.*;
public class q15664563 {
public static void main(String[] args) throws IOException {
String wordlist = "wordlist.txt";
if(args.length == 1 && args[0].toLowerCase().charAt(0) == 'l') {
//List Words
SortedSet<String> dictionary = load(wordlist);
for(String word : dictionary) {
System.out.println(word);
}
} else if(args.length == 2 && args[0].toLowerCase().charAt(0) == 'a') {
//Add Word
SortedSet<String> dictionary = load(wordlist);
dictionary.add(args[1].toLowerCase());
save(dictionary, wordlist);
} else {
System.err.println("Correct usage:");
System.err.println(" q15664563 LIST --lists all words");
System.err.println(" q15664563 ADD [word] --adds word to list");
}
}
private static void save(SortedSet<String> dictionary, String wordlist) throws IOException {
FileWriter file = new FileWriter(wordlist);
BufferedWriter writer = new BufferedWriter(file);
for(String word : dictionary) {
writer.append(word);
writer.newLine();
}
writer.close();
file.close();
}
private static SortedSet<String> load(String wordlist) throws IOException {
SortedSet<String> dictionary = new TreeSet<String>();
FileReader file = new FileReader(wordlist);
BufferedReader reader = new BufferedReader(file);
String word;
while((word = reader.readLine()) != null) {
dictionary.add(word);
}
reader.close();
file.close();
return dictionary;
}
}
Some things to point out with this code, reading and writing are two separate operations so to keep things clean it helps to put them in their own functions (this is a matter of opinion of course). Also, as is so often the case there are data types that will help you, and TreeSet is a great example of one. It implements the SortedSet interface and is alphabetical every time you iterate through ti (actually it's alphabetical every time you insert into it, something to keep in mind as the set grows in length). Given this property if you wanted to optimize you may only need to use TreeSet when the list might change it's alphabetic order (on insert). When listing you would read the file line by line knowing that it was in the correct order. If you did this then you would no longer have a generic load() method that you could use in both instances (something to consider when trading simplicity of design for performance).

how to edit my compare method

I want to compare contens of my two txt files and write the different words in other file3.txt file
I want to do compare method in this way to write another txt file. Also I dont
have an error for coding
I don't have a result. here is my code
I just ran your program with the following files and could not reproduce your problem.
deneme1
abc
def
ghi
deneme2
abc
ghi
klm
And deneme3 was created with the following content:
abc
ghi
EDIT
It seems you want the opposite behaviour. Some of your methods are unnecessarily complicated and could be made much shorter by using the right tools of the standard JDK. See below an example of a simplified implementation (that only keeps the words that are not in common between the 2 files) - this example is case sensitive:
public class TextAreaSample {
public static void main(String[] args) throws IOException {
//readAllLines does what you do in readFileAsList
List<String> strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
List<String> strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
Set<String> notInCommon = getNotInCommon(strings1, strings2);
write(notInCommon, "C:\\temp\\deneme3.txt");
}
private static void write(Collection<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
for (String s : out) {
writer.write(s + "\n");
}
writer.close();
}
private static Set<String> getNotInCommon(List<String> strings1, List<String> strings2) {
//Sets are great to get unique lists and check commonality
Set<String> onlyInFile1 = new HashSet<String>(strings1);
onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
Set<String> onlyInFile2 = new HashSet<String>(strings2);
onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
Set<String> notInCommon = new HashSet<>();
notInCommon.addAll(onlyInFile1);
notInCommon.addAll(onlyInFile2);
return notInCommon;
}
}
I have simplified and corrected your code into this:
public class TextAreaSample
{
public static void main(String [] args) throws IOException {
compare(readFileAsList("deneme1.txt"),
readFileAsList("deneme2.txt"));
}
private static void compare(List<String> strings1, List<String> strings2)
throws IOException
{
final Collator c = Collator.getInstance();
c.setStrength(Collator.PRIMARY);
final SortedSet<String>
union = new TreeSet<String>(c),
intersection = new TreeSet<String>(c);
union.addAll(strings1);
union.addAll(strings2);
intersection.addAll(strings1);
intersection.retainAll(strings2);
union.removeAll(intersection);
write(union, "deneme3.txt");
}
private static void write(Collection<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File(fname));
try { for (String s : out) writer.write(s + "\n"); }
finally { writer.close(); }
}
private static List<String> readFileAsList(String name) throws IOException {
final List<String> ret = new ArrayList<String>();
final BufferedReader br = new BufferedReader(new FileReader(name));
try {
String strLine;
while ((strLine = br.readLine()) != null) ret.add(strLine);
return ret;
} finally { br.close(); }
}
}
I have deneme1.txt:
plane
horoscope
microscope
deneme2.txt:
phone
mobile
plane
Output in deneme3.txt:
horoscope
microscope
mobile
phone
My suggestion is don't try to solve everything in one shot.
You can simplify your compare method by using one liner
strings1.retainAll(strings2)
See this for more info
http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#retainAll(java.util.Collection)
And print the contents of the strings1 and see if it is okay then solve that file writing part.
You are opening the third file deneme3.txt twice without closing it in between. I guess the second time (in write()) an exception will be thrown, so there will be no write. Remove the first occurence of FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt")); (the one in compare()) and you should be fine.
I think you have to flush() your writer before closing it.
private static void write(ArrayList<String> out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));
for (int i = 0; i < out.size(); i++) {
writer.write(out.get(i) + "\n");
}
// Flush the writer before closing it.
writer.flush();
writer.close();
}

Categories

Resources