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();
}