Results 1 to 20 of 20
Thread: ';' expected error
- 12-20-2011, 03:56 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
';' 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(); } } }Last edited by name; 12-20-2011 at 03:59 PM.
- 12-20-2011, 04:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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:
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).Java Code:location in;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 04:25 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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
.
- 12-20-2011, 04:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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:
It doesn't make sense syntactically (that's why the compiler complained) and the readLine() method doesn't take arguments ...Java Code:location in.readLine("Desktop\\");
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 09:54 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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.
- 12-20-2011, 10:14 PM #6
Re: ';' expected error
What do you want the program to do in that statement?Java Code:location in.readLine("Desktop\\");
If you can explain what you want to do, perhaps we can suggest a valid syntax.
- 12-20-2011, 10:44 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: ';' expected error
I would like that line to receive the input of the file name and location
- 12-20-2011, 10:53 PM #8
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".
- 12-20-2011, 11:01 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
- 12-20-2011, 11:05 PM #10
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?
- 12-20-2011, 11:14 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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-21-2011, 03:44 AM #12
Re: ';' expected error
I hope you're still looking at this, I think I can help
Is just incorrect syntax, but I see what you were trying to do. Try this line instead:location in.readLine("Desktop\\");
Java Code:String location = in.readLine();
- 12-22-2011, 05:22 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-22-2011, 05:22 AM #14
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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 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 lineJava Code:location = in.readLine();
and added a shortcut to the desktop to my bin folder (which now that i think of it wasn't a very bright idea...Java Code:location = in.readLine("Desktop\\");
).
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.
Thanks in advance,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(); } } }
nameLast edited by name; 12-22-2011 at 05:25 AM.
- 12-22-2011, 05:35 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: ';' expected error
Have you come across System.getProperty()? It is a very useful method. Try running the following:
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.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"); } }
- 12-22-2011, 05:43 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
- 12-22-2011, 02:39 PM #17
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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.
- 12-22-2011, 02:55 PM #18
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:
and then type something in, hit return (enter), it should echo back whatever you typed correct?Java Code:BufferedReader input = new BufferedReader(new InputStreamReader(System.in)) String fromUser = input.readLine(); System.out.println(fromUser);
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:
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".Java Code:// ... in = new BufferedReader(new FileReader("myFile.txt")); System.out.println(in.readLine());
- 12-23-2011, 08:04 AM #19
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
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.
- 12-23-2011, 08:09 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
';' expected error
By collwill in forum New To JavaReplies: 3Last Post: 05-03-2011, 07:51 AM -
Error: ')' expected
By baltimore in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:32 AM -
Error: '{' expected
By romina in forum New To JavaReplies: 1Last Post: 07-26-2007, 09:34 AM -
My error is: ')' expected
By silvia in forum New To JavaReplies: 1Last Post: 07-18-2007, 04:49 PM -
MSG ERROR: : expected
By Marty in forum New To JavaReplies: 1Last Post: 05-31-2007, 02:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks