
11-27-2009, 11:21 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
Issue
I am currently constructing code to read a file and for some reason an 'identifier expected' error keeps occuring at one particular line of code.
|
Code:
|
import java.util.*;
import java.io.*;
public class Dictionary
{
private String name;
private ArrayList<Phrases> phrase;
public Dictionary() {
name="pirate";
phrase=new ArrayList<Phrases>();
}
public void add(Phrases p){
phrase.add(p);
}
public void load(String "pirate.txt") throws IOException{ <identifier> expected
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream("pirate.txt")));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String p=infile.next();
String e=infile.next();
}
infile.close();
}
} |
Can someone explain to me from looking at this code why this error is occuring and how to fix it.
Last edited by Fubarable; 11-27-2009 at 11:25 PM.
Reason: Code tags added
|
|

11-27-2009, 11:28 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
You can't declare a mthod like so:
|
Code:
|
public void load(String "pirate.txt") |
Instead do something like:
|
Code:
|
public void load(String pirateTxt) |
and use pirateTxt as a parameter variable.
Also, I added code tags to your original post. Please see my signature below.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-28-2009, 12:07 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
Thanks for the help and now the error is gone, now I am able to run the whole method error free and the class is compiling but nothing significant seems to be happening when the method is ran. Could somebody just take a quick glance over the code to confirm wether or not it would be working as intended.
|
Code:
|
import java.util.*;
import java.io.*;
public class Dictionary
{
private String name;
private ArrayList<Phrases> phrase;
public Dictionary() {
name="pirate";
phrase=new ArrayList<Phrases>();
}
public void add(Phrases p){
phrase.add(p);
}
public void load(String pirateTxt) throws IOException{
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream("pirate.txt")));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String p=infile.next();
String e=infile.next();
}
infile.close();
}
public void save(String pirateTxt) throws IOException{
PrintWriter outfile = new PrintWriter
(new OutputStreamWriter
(new FileOutputStream("pirate.txt")));
outfile.println(phrase.size());
for (Phrases p:phrase) {
outfile.println(p.toString());
}
outfile.close();
}
} |
|
|

11-28-2009, 12:16 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
On quick glance at your code, I see that you have methods with parameters that are never used.
For instance if you declare a method like so:
|
Code:
|
public void load(String pirateTxt) throws IOException { |
Then you don't want to hard-code the file name within the method like so:
|
Code:
|
Scanner infile = new Scanner(new InputStreamReader(new FileInputStream("pirate.txt"))); |
If you do this, why even bother giving the method a parameter. Instead, use the parameter:
|
Code:
|
Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(pirateTxt))); |
Otherwise if you still have problems, you should do some debugging to find out more.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-28-2009, 12:45 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
|
Code:
|
import java.util.*;
import java.io.*;
public class Dictionary
{
private String name;
private ArrayList<Phrases> phrase;
public Dictionary() {
name="pirate";
phrase=new ArrayList<Phrases>();
}
public void add(Phrases p){
phrase.add(p);
}
public void load(String pirateTxt) throws IOException{
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream(pirateTxt)));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String pirate=infile.next();
String english=infile.next(); NoSuchElementException: null (in java.util.Scaner)
}
infile.close();
}
public void save(String pirateTxt) throws IOException{
PrintWriter outfile = new PrintWriter
(new OutputStreamWriter
(new FileOutputStream(pirateTxt)));
outfile.println(phrase.size());
for (Phrases p:phrase) {
outfile.println(p.toString());
}
outfile.close();
} |
One more error it seems *sigh* could someone explain to me this annoying niggle of an error (shown in code). Last one I promise
|
|

11-28-2009, 12:47 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
Please show your text file.
|
|

11-28-2009, 12:51 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
|
Code:
|
5
ahoy there
hello
me hearties
dear friends
well, shiver me timbers
gosh - how exciting
arrrrrrgh!
goodness!
you're a bilge rat
you are a horrid person |
Here, see what light you can make of this please
|
|

11-28-2009, 12:59 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
You need a lesson in debugging. For quick and dirty, I often sprinkle println statements in my code. For instance, if you do this, what is output?:
|
Code:
|
int num = inFile.nextInt();
inFile.nextLine();
for (int i = 0; i < num; i++) {
String pirate = inFile.next();
String english = inFile.next();
// *** add these lines to see what is going on ***
System.out.println("pirate: " + pirate);
System.out.println("english: " + english);
} |
|
|

11-28-2009, 01:13 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
Well since the english variable is still producing an error I just tried the println with the pirate variable and it returned
|
Code:
|
pirate: Phrases@c5e9c |
|
|

11-28-2009, 01:54 AM
|
|
Senior Member
|
|
Join Date: Feb 2009
Posts: 589
Rep Power: 1
|
|
|
Use the code that Fubarable posted. It goes in the load() method (not the save() method) because it is trying to show what is actually being read from the file.
|
|

11-28-2009, 02:15 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
I did and the println for the pirate produced
|
Code:
|
pirate: Phrases@92dcdb |
Confused
|
|

11-28-2009, 02:40 AM
|
|
Senior Member
|
|
Join Date: Feb 2009
Posts: 589
Rep Power: 1
|
|
In that case the contents of your text file actually contains
and not what you posted in reply #7. This could have happened because your code called save() previously and overwrote the data file.
To deal with the NoSuchElementException restore the data file and use the debugging code as posted. It is designed to give quite a few lines of output, not one.
[Edit]
It would be a good idea to separate files for input and output in order to avoid this sort of confusion.
And to add a line of debugging code to the save method:
|
Code:
|
public void save(String pirateTxt) throws IOException{
// etc
outfile.println(phrase.size());
for (Phrases p:phrase) {
System.out.println("About to write the line " + p.toString() + " into the data file");
outfile.println(p.toString());
}
outfile.close();
} |
Last edited by pbrockway2; 11-28-2009 at 02:45 AM.
|
|

11-28-2009, 02:52 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
I have just checked the text file and it definately still contains
|
Code:
|
5
ahoy there
hello
me hearties
dear friends
well, shiver me timbers
gosh - how exciting
arrrrrrgh!
goodness!
you're a bilge rat
you are a horrid person |
hmm.
|
|

11-28-2009, 07:56 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
Forget what I said above the file had been overidden  .
Now when I do read from the file that does contain:
|
Code:
|
5
ahoy there
hello
me hearties
dear friends
well, shiver me timbers
gosh - how exciting
arrrrrrgh!
goodness!
you're a bilge rat
you are a horrid person |
The printout is instead:
|
Code:
|
Phrases@1672bbb
Phrases@6833f2
Phrases@12a73d9
Phrases@1bdb58
Phrases@dd75a4 |
From the format of the printout I can see that I am succesfully able to load from and save to the file but it is not in the english format I have expected it to be.
Is there anyway from both my dictionary code:
|
Code:
|
import java.util.*;
import java.io.*;
public class Dictionary
{
private ArrayList<Phrases> phrase;
public Dictionary() {
phrase=new ArrayList<Phrases>(30);
}
public void add(Phrases p){
phrase.add(p);
}
public void printout(){
System.out.println("The phrases in the dictionary are: ");
for (Phrases p:phrase) {
System.out.println(p.toString());
}
}
public void load(String pirateTxt) throws IOException{
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream("pirate.txt")));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String pi=infile.next();
String e=infile.next();
infile.nextLine();
Phrases p=new Phrases(pi,e);
phrase.add(p);
}
infile.close();
}
public void save(String pirateTxt) throws IOException{
PrintWriter outfile = new PrintWriter
(new OutputStreamWriter
(new FileOutputStream(pirateTxt)));
outfile.println(phrase.size());
for (Phrases p:phrase) {
outfile.println(p.toString());
}
outfile.close();
}
} |
And my Phrases code:
|
Code:
|
import java.util.*;
public class Phrases
{
private String pirate;
private String english;
public Phrases() {
pirate="unknown";
english="unknown";
}
public Phrases(String pi,String e) {
pirate=pi;
english=e;
}
public String getEnglish()
{
return english;
}
} |
Could someone take a quick look over this please and give me feedback about why it is reading the text file as shown?
|
|

11-28-2009, 08:37 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
Where's your main method?
Also, we have recommended that you not hard-code your file String as you are doing here:
|
Code:
|
public void load(String pirateTxt) throws IOException {
// below, you use hard-coded String "pirate.txt" instead of using pirateTxt parameter??
Scanner infile = new Scanner(new InputStreamReader(new FileInputStream("pirate.txt"))); |
Why have you not fixed this yet?
Last edited by Fubarable; 11-28-2009 at 08:42 PM.
|
|

11-28-2009, 09:25 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
|
Ah didn't notice that it's fixed now.
I'm not also quite sure what you mean by a main method Fubarable sorry.
|
|

11-28-2009, 09:59 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
Originally Posted by FlashNinja
|
|
I'm not also quite sure what you mean by a main method Fubarable sorry.
|
All programs need a main method in order to run. It looks something like this:
|
Code:
|
public static void main(String[] args) {
// bunch of code goes in here
} |
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-28-2009, 10:05 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
Oh yes I plan to implement that after I have the little problem with how the text in the file is being read don't worry. I just need to figure out to get the program to read the text in plain english
|
|

11-28-2009, 10:15 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
Originally Posted by FlashNinja
|
Oh yes I plan to implement that after I have the little problem with how the text in the file is being read don't worry. I just need to figure out to get the program to read the text in plain english
|
Allow me to worry. How are you testing it now? How should we run this program?
Some problems stick out at me:
1) Phrases has no getPirate() method.
2) More importantly, Phrases has no toString() method, so if you call println for some Phrases object, you'll get the junk that you currently see. If you want to fix this, give this class a decent toString() method, and you'll see improvement.
3) Dictionary is still getting individual tokens via Scanner#next() when you really want to get whole lines via Scanner#hasNextLine() and Scanner#nextLine().
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Last edited by Fubarable; 11-28-2009 at 10:43 PM.
|
|

11-28-2009, 10:31 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 11
Rep Power: 0
|
|
|
Thank you Fubarable! Your recommendation for the toString method fixed it. Worked like a charm. I shoudl be able to get the rest of this done now that is out of the way.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 02:19 PM.
|
|