Hello all,
I have been working on a program and it is almost done, however I have 2 small issues:
Number 1: I need to write my information to an output.txt file, however, I can't seem to get the information to the text file and can't figure out why.
Number 2: I need to have my output information separated line by line, but right now it is all listed on the same line.
Here is my program so far
package tastwob; import java.util.Scanner; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.FileReader; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.Collections; public class TasktwoB{ public static void main(String[] args) throws FileNotFoundException, IOException { // Prompt for the input and output file names Scanner console = new Scanner(System.in); System.out.print("Please enter the Filename here: "); String inputFile = console.next(); // Create variables ArrayList propids = new ArrayList(); String line = null ; int count = 0; double sum = 0; BufferedWriter pwfo = null; try{ File input = new File(inputFile); BufferedReader in = new BufferedReader(new FileReader(input)); pwfo = new BufferedWriter(new FileWriter("C:\\Users\\Bosco\\Documents\\NetBeansProjects\\Tasktwo\\src\\tasktwo\\overview.txt", true)); PrintWriter pwo = new PrintWriter(pwfo); while ((line = in.readLine())!= null) { count++; String[] plist = line.split("[\\s}]"); Double pvalue = Double.parseDouble(plist[2]); //for (int i=0; i < plist.length; i++) { sum+= pvalue; } } //int i = 0; System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " +(sum)+ "0" +"\n"); in.close(); Scanner idfile = new Scanner((input)); //print property ids while (idfile.hasNextLine()) { line = idfile.nextLine(); String[] fields = line.split("[\\s}]"); String pids = (fields [0]); { propids.add(pids); //for(int i =0; i<propids.size();i++) in.close(); } } Collections.sort(propids); System.out.println(propids); //close Print Writer pwo.write("C:\\Users\\Bosco\\Documents\\NetBeansProjects\\Tasktwo\\src\\tasktwo\\overview.txt"); pwo.close(); }catch(Exception e){ } } }
I am reading information from a text file as follows:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
My output should look like this:
Total properties listed: 7
Total value of properties listed: 2120000.00
110001
110020
110223
110333
110421
110442
112352
but it looks like this:
Total properties listed: 7
Total value of properties listed: 2120000.00
[110001, 110020, 110223, 110333, 110421, 110442, 112352]
and it is not writing to the overview.txt file as it should.
I thank you in advance for any assistance you can provide.