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

    Default ';' expected error

    Hi,
    I am getting an ';' expected error, but I have all the required semicolons. I don't know what is wrong; can someone help me?

    Thanks

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class file_reader2
    {
    	public static void main(String[] args) throws IOException
    	{
    		String line;
    		BufferedReader in;
    		
    		in = new BufferedReader(newInputStreamReader(System.in));
    		
    		System.out.println(" ");
    		System.out.println("Welcome to My File Reader!");
    		System.out.println(" ");
    		System.out.println("Please Input File Location:");
    		location in.readLine("Desktop\\");
    		
    		in = new BufferedReader(new FileReader(location));
    		line = in.readLine();
    		
    	while(line != null)
    		{
    			System.out.println(line);
    			line= in.readLine();
    		}
    	}
    }
    Attached Thumbnails Attached Thumbnails ';' expected error-file_reader2-error.png  
    Last edited by name; 12-20-2011 at 03:59 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: ';' expected error

    What is 'location'? You confused the compiler in thinking that you wanted to define a new variable 'in' of type 'location'. Look where the care is pointing; it expected a semi colon at that position, so that line would become:

    Java Code:
    location in;
    That doesn't make sense either (location isn't defined and you already defined a variable 'in', but the compiler was just starting to complain).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: ';' expected error

    I tried to use "location" as a variable so the user could define where the file is located. Is "location" one of those predefined names that I can't use for a variable name? I am sorry if I am doing it wrong .

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: ';' expected error

    There's nothing special about the name 'location' and there's no need to say sorry; what did you have in mind with the line:

    Java Code:
    location in.readLine("Desktop\\");
    It doesn't make sense syntactically (that's why the compiler complained) and the readLine() method doesn't take arguments ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: ';' expected error

    The use of desktop in "location in. readLine("Desktop\\")" was to automatically fill in a shortcut to the desktop, because I didn't want to put all the files I want to read in my bin folder. I tried to compile the program without the Desktop shortcut, but it also didn't work.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: ';' expected error

    Java Code:
    location in.readLine("Desktop\\");
    What do you want the program to do in that statement?
    If you can explain what you want to do, perhaps we can suggest a valid syntax.

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

    Default Re: ';' expected error

    I would like that line to receive the input of the file name and location

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: ';' expected error

    Sorry, lines do not receive values. Variables receive values.
    What variable do you want to receive the contents/next line of the file. I assume that is what you mean when you said: "input of the file name and location".

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

    Default Re: ';' expected error

    Quote Originally Posted by Norm View Post
    "input of the file name and location".
    That is exactly what I said!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: ';' expected error

    Sorry, I do not understand what you mean by "input of the file and location".
    You can read the contents of a file by having a class open the file and calling a read method.
    But what is the "and location" part mean?

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

    Default Re: ';' expected error

    I would like to end this post right now, it is both confusing and stressful for me to talk to two moderators. It is also very difficult to enplane this error, because I am both not experienced at java, or forums.

    Thanks for trying help.

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: ';' expected error

    I hope you're still looking at this, I think I can help

    location in.readLine("Desktop\\");
    Is just incorrect syntax, but I see what you were trying to do. Try this line instead:
    Java Code:
    String location = in.readLine();

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ';' expected error

    And you can't easily fill in a default value for the user input like "Desktop\\". First master the syntax as quad64bit suggests, then think about ways of providing the user with a default.

    There is no reason to be worried about not understanding Java syntax. That's where we all started! And it is stressful, but take it a step at a time and consult books and examples making sure you understand each line and what it does.

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

    Default Re: ';' expected error

    Hi again,
    I tried what you said quad64bit, but it still didn't work; even without "Desktop\\" in the line, it still didn't work ! I will attach the updated code below. I think it is also a good time to explain what the program must do. I am designing a program that will read and display a txt. file for a school assignment. I managed to get it to work , but I had two problems with it... Firstly, the user can't define which file to read (without changing the program), so I decided to install the line
    Java Code:
    location = in.readLine();
    so the user can input which file to read. Secondly, it only works with files in my bin folder (C:\j2sdk1.4.2_16\bin), but I wanted it to read the files from the desktop, so I added "Desktop\\" to the line
    Java Code:
    location = in.readLine("Desktop\\");
    and added a shortcut to the desktop to my bin folder (which now that i think of it wasn't a very bright idea... ).

    So, to summarize, I need the program to ask the user which txt. file to read (preferably on the desktop) and display the file to the user. And I am still getting the ';' expected error. Could someone please help me.

    Here is the updated code.
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class file_reader2
    {
    	public static void main(String[] args) throws IOException
    	{
    		String line;
    		BufferedReader in;
    		
    		in = new BufferedReader(newInputStreamReader(System.in));
    		
    		System.out.println(" ");
    		System.out.println("Welcome to My File Reader!");
    		System.out.println(" ");
    		System.out.println("Please Input File Location:");
    		String location in.readLine();
    		
    		in = new BufferedReader(new FileReader(location));
    		line = in.readLine();
    		
    	while(line != null)
    		{
    			System.out.println(line);
    			line= in.readLine();
    		}
    	}
    }
    Thanks in advance,
    name
    Last edited by name; 12-22-2011 at 05:25 AM.

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ';' expected error

    Have you come across System.getProperty()? It is a very useful method. Try running the following:

    Java Code:
     
    
    public class HomeEg {
        public static void main(String[] args) {
            String name = System.getProperty("user.name");
            String home = System.getProperty("user.home");
            
            System.out.println("Hello " + name);
            System.out.println("I should save files in " + home);
            System.out.println("or maybe " + home + "\\desktop");
        }
    }
    One approach to the problem of a default is to print a message to the user "Please enter file location or press enter for home directory". Then when you've read the user's input you would have to check if it was empty and, if it is, replace it with their home directory or desktop or whereever.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ';' expected error

    Also:

    Java Code:
    String line;
    BufferedReader in;
             
    in = new BufferedReader(newInputStreamReader(System.in));
             
    System.out.println(" ");
    System.out.println("Welcome to My File Reader!");
    System.out.println(" ");
    System.out.println("Please Input File Location:");
    String location in.readLine(); // <-- change this to what quad64bit said
             
    in = new BufferedReader(new FileReader(location));
    line = in.readLine();
    // etc

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

    Default Re: ';' expected error

    I tried the System.getPropriety line, and it still didn't work. I don't know if I wrote it wrong, or because I am running an older version of java. Also, that last line of code you wrote about was exactly the same as what I wrote.

  18. #18
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: ';' expected error

    Lets try this:
    - What version of java are you running? (I don't think this matters, but just for argument's sake)
    - What are you using to write your code, and how are you compiling and running it?

    If you do something like:

    Java Code:
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in))
    String fromUser = input.readLine();
    System.out.println(fromUser);
    and then type something in, hit return (enter), it should echo back whatever you typed correct?

    If that works, I'm guess you're just typing in the wrong path. Do you understand the difference between relative and absolute paths?

    For example, on *nix systems, you could type a path two different ways:
    /Users/myName/Desktop/someFile.txt
    or
    someFolder/someFile.txt

    The first is absolute. It is the complete path starting from the system root '/' all the way to the file 'someFile.txt'
    The second is relative, and requires that the someFolder directory is in the same place as the java program you are running. If it isn't, then you get a file not found IO error.

    Also, java doesn't understand common unix shortcuts like ~ (the home directory) so ~/Desktop/someFile.txt will not work.

    If you make a text file, and put it in the same folder as your java application, you can reference it just like this:
    Java Code:
    // ...
    in = new BufferedReader(new FileReader("myFile.txt"));
    System.out.println(in.readLine());
    This should print the first line of the file. If that works, then go back to the way you were trying to do it before, where you request the location from the user and pass that into the FileReader in place of "myFile.txt".

  19. #19
    rahul4mates is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: ';' expected error

    Try the below code:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class file_reader2
    {
    public static void main(String[] args) throws IOException
    {
    String line;
    BufferedReader in;

    in = new BufferedReader(new InputStreamReader(System.in));

    System.out.println(" ");
    System.out.println("Welcome to My File Reader!");
    System.out.println(" ");
    System.out.println("Please Input File Location:");
    String location = in.readLine();

    in = new BufferedReader(new FileReader(location));
    line = in.readLine();

    while (line != null) {
    System.out.println(line);
    line = in.readLine();
    }

    }
    }

    After u compile the code and run the program, it waits for your file name (need to give the complete path of the file name), for example u have a file in c:/foldername/filename.txt.
    then it will read the contents of the filename.txt line by line and prints in console.
    Last edited by rahul4mates; 12-23-2011 at 08:08 AM.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: ';' expected error

    @rahul: please don't spoonfeed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ';' expected error
    By collwill in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 07:51 AM
  2. Error: ')' expected
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:32 AM
  3. Error: '{' expected
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 09:34 AM
  4. My error is: ')' expected
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:49 PM
  5. MSG ERROR: : expected
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 02:21 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
  •