FileReader "not a statement"
I am new to java and having issues compiling my code. I continue to receive two errors that FileReader is not a statement and that a ";" is expected after FileReader. I have tried to import several tools but I am still having trouble. I am running java version 1.6.0_35-b10, could this be the problem?
Here are the errors:
----jGRASP exec: javac -g lab12.java
lab12.java:61: not a statement
FileReader ifile = new FileReader("data.txt");
^
lab12.java:61: ';' expected // the arrow points after FileReader
FileReader ifile = new FileReader("data.txt");
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
This is my code:
Code java:
import java.util.Scanner;
import java.text.*;
import java.io.*;
import java.io.FileReader;
import java.io.File;
public class lab12
{
public static void main(String[] args){
int c;
String line;
Foreign calculator;
Scanner Keyboard = new Scanner(System.in);
FileWriter ofile = new FileWriter("data.txt");
BufferedWriter bw = new BufferedWriter(ofile);
PrintWriter outfile = new PrintWriter(ofile);
do
{
Foreign.displayTitle();
Foreign.displayMenu();
c = Foreign.getChoice(); //<----- Call getChoice() here and assign value to ‘c’
if ( c != 0)
{
calculator = new Foreign();
calculator.getAmount();
calculator.display();
outfile.println(calculator); //<---- calls toString() implicitly
}
}
while (c != 0);
outfile.close();
System.out.print("Display summary? (y/n)");
s = keyboard.nextLine();
displaySummary = s.charAt(0);
if (displaySummary == 'Y' || displaySummary == 'y')
FileReader ifile = new FileReader("data.txt");
BufferedReader infile = new BufferedReader(ifile);
while ((line = infile.readLine()) != null)
{
System.out.println(line);
}
infile.close();
Foreign.displayCount();
}
}
Re: FileReader "not a statement"
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Quote:
continue to receive two errors that FileReader is not a statement and that a ";" is expected after FileReader.
Please post the full text of the error messages. They have important information about the errors that you have left off.
Re: FileReader "not a statement"
I updated my post, sorry about that.
I pasted the error, but I want to mention that the second error points to right after FileReader while the first error is pointing at the beginning of FileReader. Unfortunately when I paste these messages here the formatting is changed to show both arrows pointing at the beginning of FileReader.
----jGRASP exec: javac -g lab12.java
lab12.java:61: not a statement
FileReader ifile = new FileReader("data.txt");
^
lab12.java:61: ';' expected
FileReader ifile = new FileReader("data.txt");
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: FileReader "not a statement"
One problem I see is the code does not use {}s with all the if statements.
You should ALWAYS use {}s to enclose the code used with if and while statements.
Re: FileReader "not a statement"
Quote:
Originally Posted by
Norm
One problem I see is the code does not use {}s with all the if statements.
You should ALWAYS use {}s to enclose the code used with if and while statements.
I originally had {} around the last if statement but it caused more errors. When I add them back as seen below I get 9 errors.
Code java:
import java.util.Scanner;
import java.text.*;
import java.io.*;
import java.io.FileReader;
import java.io.File;
public class lab12
{
public static void main(String[] args){
int c;
String line;
Foreign calculator;
Scanner Keyboard = new Scanner(System.in);
FileWriter ofile = new FileWriter("data.txt");
BufferedWriter bw = new BufferedWriter(ofile);
PrintWriter outfile = new PrintWriter(ofile);
do
{
Foreign.displayTitle();
Foreign.displayMenu();
c = Foreign.getChoice();
if ( c != 0)
{
calculator = new Foreign();
calculator.getAmount();
calculator.display();
outfile.println(calculator);
}
}
while (c != 0);
outfile.close();
System.out.print("Display summary? (y/n)");
s = keyboard.nextLine();
displaySummary = s.charAt(0);
if (displaySummary == 'Y' || displaySummary == 'y')
{
FileReader ifile = new FileReader("data.txt");
BufferedReader infile = new BufferedReader(ifile);
}
while ((line = infile.readLine()) != null)
{
System.out.println(line);
}
infile.close();
Foreign.displayCount();
}
}
These are the errors
Code java:
----jGRASP exec: javac -g lab12.java
lab12.java:57: cannot find symbol
symbol : variable s
location: class lab12
s = keyboard.nextLine();
^
lab12.java:57: cannot find symbol
symbol : variable keyboard
location: class lab12
s = keyboard.nextLine();
^
lab12.java:58: cannot find symbol
symbol : variable displaySummary
location: class lab12
displaySummary = s.charAt(0);
^
lab12.java:58: cannot find symbol
symbol : variable s
location: class lab12
displaySummary = s.charAt(0);
^
lab12.java:59: cannot find symbol
symbol : variable displaySummary
location: class lab12
if (displaySummary == 'Y' || displaySummary == 'y')
^
lab12.java:59: cannot find symbol
symbol : variable displaySummary
location: class lab12
if (displaySummary == 'Y' || displaySummary == 'y')
^
lab12.java:65: cannot find symbol
symbol : variable infile
location: class lab12
while ((line = infile.readLine()) != null)
^
lab12.java:69: cannot find symbol
symbol : variable infile
location: class lab12
infile.close();
^
lab12.java:71: cannot find symbol
symbol : method displayCount()
location: class Foreign
Foreign.displayCount();
^
9 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: FileReader "not a statement"
Quote:
it caused more errors
Adding the {}s did not cause the errors. The errors were just hidden.
The cannot find symbol error says that the compiler can not find where the variable mentioned in the error message is defined. Make sure the variable has a definition that is in scope (within the same pair of {}s) where it is used.
Looks like you have used a lot of variables without defining them first.
You need to define a variable BEFORE you can use it in the code.