Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: FileReader "not a statement"

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileReader "not a statement"

    Quote Originally Posted by Norm View Post
    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.

    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

     ----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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: FileReader "not a statement"

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 12:09 PM
  5. first itteration of "for" statement acts differntly to all the rest, why?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 4
    Last Post: March 1st, 2011, 11:06 AM