Results 1 to 4 of 4
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Trouble with Java File Reader

    Hi,
    I am creating a program that would read a text file and display its contents for an assignment. I have made a code, but I have two issues with it. Firstly, I am getting two errors that say I am missing 2 '}' (I shall post an image of the errors). Secondly, the topic of reading data from another file in Java is new to me. It would be nice if someone would explain to me how to read a file in Java.

    Thanks in advance,
    name

    Here is the code:
    Java Code:
    import java.io.*;
    
    public class file-reader
    {
    	public static void main(String[] args) throws IOException
    	{
    		String line;
    		BufferedReader in;
    		
    		in = new BufferedReader(new FileReader("temp.txt"));
    		in = new BufferedReader(newInputStreamReader(System.in)); 
    		
    		int loopcont == 1;
    		
    		System.out.println ("Welcome to My File Reader!");
    		
    		while(loopcont == 1)
    		{
    			System.out.println ("Input file location:");
    			line in.readline ();
    			System.out.println (" ");
    			
    			System.out.println (line);
    			System.out.println (" ");
    			
    			System.out.println ("Would you like to read another file? (y/n)");
    			loopcont2 in.readln ();
    			
    			if loopcont2 == ("Yes" || "Y" || "yes" || "y")
    			{
    				loopcont == 1;
    			}
    			if loopcont2 == ("No" || "N" || "no" || "n")
    			{
    				loopcont == 0;
    			}
    			if loopcont2 != ("Yes" || "Y" || "yes" || "y" || "No" || "N" || "no" || "n")
    			{
    				System.err.println ("Unrecognized option");
    			}
    		}
    		System.out.println ("Goodbye");
    	}
    }
    Attached Thumbnails Attached Thumbnails Trouble with Java File Reader-file-reader-error.png  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Trouble with Java File Reader

    The subtraction operator (-) can not be used in a name.

    The Scanner class and the FileReader class can be used to read a text file. Do a Search here on the forum for code samples.

  3. #3
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: Trouble with Java File Reader

    Thanks

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Trouble with Java File Reader

    Have a look at the link in my sig.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 02:06 AM
  2. problem with file reader
    By Stormrage in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 11:48 PM
  3. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 08:19 AM
  4. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 07:43 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •