-2

today I come to this website with a question. I have searched around the website but cannot find anything remotely close to answering my question.

So I am writing this program which is supposed to have 4 options, 2 of which are to "Read" and "Write" to the file. I have made most of the code perfectly but am having a tough time making the program "Read" and "Write" to the file. I believe this maybe be a problem with a scanner for example but i have tried various little tricks, none have worked sadly. I am open to any help possible, this is driving me crazy, I'm sure that i have overlooked something, that is why i ask for your help today. Below is my entire code and then the exact part I am having the trouble with.

(Still new to the website, sorry if this isn't formatted correctly)

ERROR CODES I HAVE GOTTEN:

Lab9_5.java:67: error: ';' expected
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
                             ^
Lab9_5.java:67: error: <identifier> expected
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
                                 ^
Lab9_5.java:83: error: <identifier> expected
        inFile = new (new FileReader("E:/savings.txt"));
                    ^
3 errors

CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab9_5 {

    //Declare global variable and Scanner object to read from keyboard
    static double[] notGreenCost = new  double[6];
    static double[] goneGreenCost = new double[6];
    static double[] savings = new double[6];
    static String[] months = new String[6];
    static Scanner in = new Scanner(System.in);


    public static void main(String[] args) throws IOException {

        String endProgram = "no";
        int option = 0;
        initMonths();

        while(endProgram.equals("no")){
            System.out.println("What do you like to do? Type:");
            System.out.println("1 to enter data");
            System.out.println("2 to display data");
            System.out.println("3 to write data to a file");
            System.out.println("4 to read data from a file");
            option = in.nextInt();

            if(option == 1){
                getNotGreen(notGreenCost, months);
                getGoneGreen(goneGreenCost, months);
                energySaved(notGreenCost, goneGreenCost, savings);
            }
            else if(option == 2){
                displayInfo(notGreenCost, goneGreenCost, savings, months);
            }
            else if(option == 3){
                writeToFile(months, savings);
            }
            else if(option == 4){ 
                readFromFile(months, savings);
            }

            System.out.println("Do you want to end the program (enter yes or no): ");
            endProgram = in.next();
            while(!endProgram.equals("no") && !endProgram.equals("yes")){ 
                System.out.println("Please enter a valid value of yes or no");
                endProgram = in.next();
            }

        }


    }

// Write to file function
    public static void writeToFile(String[] months, double[] savings) throws IOException{
        PrintWriter savings = null;
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
        outFile.write("Savings");
        outFile.newLine();
        int counter = 0;

        while(counter<6){
            outFile.write(months[counter]);
            outFile.newLine();
            outFile.write(savings[counter]+"");
            outFile.newLine();
            counter++;
        }
        outFile.close();
    }
// Read from file function
    public static void readFromFile(String[] months, double[] savings) throws IOException{
        inFile = new (new FileReader("E:/savings.txt"));
        String str1 = "";

        while((str1=inFile.readLine())!=null){
            System.out.println(str1);
        }
        inFile.close();
    }
// InitMonth function
    public static void initMonths(){
        months = new String[]{"January", "February", "March", "April", "May", "June"};
    }
// Get not green function
    public static void getNotGreen(double[] notGreenCost, String[] months){
        int counter = 0;
        while(counter<6){
            System.out.print("Enter NOT GREEN energy cost for "+months[counter]+": ");
            notGreenCost[counter] = in.nextDouble();
            counter++;
        }
    }
// Get gone green function
    public static void getGoneGreen(double[] goneGreenCost, String[] months){
        int counter = 0;
        while(counter<6){
            System.out.print("Enter GONE GREEN energy cost for "+months[counter]+": ");
            goneGreenCost[counter] = in.nextDouble();
            counter++;
        }
    }
// Energy saved function
    public static void energySaved(double[] notGreenCost, double[] goneGreenCost, double[] savings){
        int counter = 0;
        while(counter<6){ 
            savings[counter] = notGreenCost[counter] - goneGreenCost[counter];
            counter++;
        }
    }
// Display info function
    public static void displayInfo(double[] notGreenCost, double[] goneGreenCost, double[] savings, String[] months){
        System.out.println("SAVINGS     NOT GREEN    GONE GREEN        MONTH");
        System.out.println("_________________________________________________");
        int counter = 0;
        while(counter<6){
            System.out.println("$" + savings[counter] + "      $" + notGreenCost[counter] + 
        "        $" + goneGreenCost[counter] + "        " +  months[counter]);
            counter++;
        }
    }
} 

PROBLEM AREA OF THE CODE:

// Write to file function
    public static void writeToFile(String[] months, double[] savings) throws IOException{
        PrintWriter savings = null;
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
        outFile.write("Savings");
        outFile.newLine();
        int counter = 0;

        while(counter<6){
            outFile.write(months[counter]);
            outFile.newLine();
            outFile.write(savings[counter]+"");
            outFile.newLine();
            counter++;
        }
        outFile.close();
    }
// Read from file function
    public static void readFromFile(String[] months, double[] savings) throws IOException{
        inFile = new (new FileReader("E:/savings.txt"));
        String str1 = "";

        while((str1=inFile.readLine())!=null){
            System.out.println(str1);
        }
        inFile.close();
    }
Joe Glow
  • 1
  • 3
  • Does your problem code throw an error of any sort? – achAmháin Nov 29 '17 at 15:15
  • 3
    Need more detail. You say, *...am having a tough time making the program "Read" and "Write" to the file*. This doesn't really say much. What kind of "tough time"? Are you getting an error message? What are the symptoms exactly? – lurker Nov 29 '17 at 15:17
  • I apologize, i will update the question with the error codes. – Joe Glow Nov 29 '17 at 15:18
  • Curious, why does your writing to file method have `double[] savings` as an argument, and then declare, but never use, `PrintWriter savings = null;` – phflack Nov 29 '17 at 15:19
  • Is there a particular reason you're using `PrintWriter`? Have you read any online information about how to do basic file I/O in Java? Why are you doing `new` twice in: `inFile = new (new FileReader("E:/savings.txt"));` That doesn't make sense. – lurker Nov 29 '17 at 15:20
  • `Lab9_5.java:67: error: ';' expected` means that you have a syntax error in the file "Lab9_5.java", at around line 67 – phflack Nov 29 '17 at 15:21
  • We don't actually need all of your code. Only problem code is what we need. See also [mcve] page in Help Center. – M. Prokhorov Nov 29 '17 at 15:21
  • Syntax errors and typos aside (pretty sure that line should be `inFile = new FileReader([...]);`), are there any issues with the file reading/writing itself? So far it looks like you're only running into compilation problems – phflack Nov 29 '17 at 15:22
  • 1
    *I have searched around the website but cannot find anything remotely close to answering my question.*. A quick search for "java printwriter example" yielded: [How to use PrintWriter and File Classes in Java](https://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java) – lurker Nov 29 '17 at 15:24
  • Ohh so the problem is beacuse i am using "new" to many times? I was trying to use old files i had as examples for this. – Joe Glow Nov 29 '17 at 15:24
  • That's just one problem. It looks like you really need to read the documentation and examples for Java classes before you try to use them. You're just kind of guessing, which is no way to write a program. – lurker Nov 29 '17 at 15:27
  • Appreciate all the comments and links, i am looking into all of the help you gave me. Thank you. – Joe Glow Nov 29 '17 at 15:29

1 Answers1

0

Code does not compile.

line 62: PrintWriter savings = null; //savings is already defined
line 63: outFile = PrintWriter new (new FileWriter("E:/savings.txt")); // incorrect PrintWriter instantiation, outFile has no type specified...
line 79: inFile = new (new FileReader("E:/savings.txt")); //new (new... ?

It will compile if you fix object types and instantiation where needed, there were no other compiler errors. Though I have no idea if the code works at all.

I would suggest reading some basics, like: Creating Objects

CrazySabbath
  • 1,274
  • 3
  • 11
  • 33