Results 1 to 20 of 21
Thread: Issue
- 11-27-2009, 10:21 PM #1
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.
Java 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 10:25 PM. Reason: Code tags added
-
You can't declare a mthod like so:
Instead do something like:Java Code:public void load(String "pirate.txt")
and use pirateTxt as a parameter variable.Java Code:public void load(String pirateTxt)
Also, I added code tags to your original post. Please see my signature below.
- 11-27-2009, 11:07 PM #3
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.
Java 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(); } }
-
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:
Java Code:public void load(String pirateTxt) throws IOException {
Then you don't want to hard-code the file name within the method like so:
Java 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:
Otherwise if you still have problems, you should do some debugging to find out more.Java Code:Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(pirateTxt)));
- 11-27-2009, 11:45 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
One more error it seems *sigh* could someone explain to me this annoying niggle of an error (shown in code). Last one I promise :pJava 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(); }
-
Please show your text file.
- 11-27-2009, 11:51 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
Here, see what light you can make of this please :)Java 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
-
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?:
Java 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, 12:13 AM #9
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
Java Code:pirate: Phrases@c5e9c
- 11-28-2009, 12:54 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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, 01:15 AM #11
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
I did and the println for the pirate produced
Confused :confused:Java Code:pirate: Phrases@92dcdb
- 11-28-2009, 01:40 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.Java Code:1 Phrases@92dcdb
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:
Java 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 01:45 AM.
- 11-28-2009, 01:52 AM #13
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
I have just checked the text file and it definately still contains
hmm.Java 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
- 11-28-2009, 06:56 PM #14
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
Forget what I said above the file had been overidden :p.
Now when I do read from the file that does contain:
The printout is instead:Java 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
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.Java Code:Phrases@1672bbb Phrases@6833f2 Phrases@12a73d9 Phrases@1bdb58 Phrases@dd75a4
Is there anyway from both my dictionary code:
And my Phrases code:Java 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(); } }
Could someone take a quick look over this please and give me feedback about why it is reading the text file as shown?Java 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; } }
-
Where's your main method?
Also, we have recommended that you not hard-code your file String as you are doing here:
Why have you not fixed this yet?Java 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")));Last edited by Fubarable; 11-28-2009 at 07:42 PM.
- 11-28-2009, 08:25 PM #16
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:05 PM #18
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 :p
-
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().Last edited by Fubarable; 11-28-2009 at 09:43 PM.
- 11-28-2009, 09:31 PM #20
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
PDF Box issue
By jazz2k8 in forum Advanced JavaReplies: 0Last Post: 03-20-2009, 11:04 AM -
Performance Issue
By hiranya in forum Threads and SynchronizationReplies: 2Last Post: 11-24-2008, 11:32 PM -
NullPointerException issue
By fritz1474 in forum AWT / SwingReplies: 2Last Post: 09-03-2008, 05:21 PM -
Performance issue
By mathes_n in forum Web FrameworksReplies: 8Last Post: 09-02-2008, 05:11 AM -
Issue
By chaitu444 in forum New To JavaReplies: 2Last Post: 11-06-2007, 07:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks