Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2009, 11:21 PM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default 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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-27-2009, 11:28 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-28-2009, 12:07 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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();
}
       
                           
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2009, 12:16 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2009, 12:45 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-28-2009, 12:47 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Please show your text file.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-28-2009, 12:51 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-28-2009, 12:59 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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);
      }
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-28-2009, 01:13 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-28-2009, 01:54 AM
Senior Member
 
Join Date: Feb 2009
Posts: 589
Rep Power: 1
pbrockway2 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-28-2009, 02:15 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
I did and the println for the pirate produced

Code:
 pirate: Phrases@92dcdb
Confused
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-28-2009, 02:40 AM
Senior Member
 
Join Date: Feb 2009
Posts: 589
Rep Power: 1
pbrockway2 is on a distinguished road
Default
In that case the contents of your text file actually contains

Code:
1
Phrases@92dcdb
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-28-2009, 02:52 AM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-28-2009, 07:56 PM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-28-2009, 08:37 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-28-2009, 09:25 PM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
Ah didn't notice that it's fixed now.

I'm not also quite sure what you mean by a main method Fubarable sorry.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 11-28-2009, 09:59 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Originally Posted by FlashNinja View Post
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
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 11-28-2009, 10:05 PM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 11-28-2009, 10:15 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Originally Posted by FlashNinja View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 11-28-2009, 10:31 PM
Member
 
Join Date: Nov 2009
Posts: 11
Rep Power: 0
FlashNinja is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PDF Box issue jazz2k8 Advanced Java 0 03-20-2009 12:04 PM
Performance Issue hiranya Threads and Synchronization 2 11-25-2008 12:32 AM
NullPointerException issue fritz1474 AWT / Swing 2 09-03-2008 06:21 PM
Performance issue mathes_n Web Frameworks 8 09-02-2008 06:11 AM
Issue chaitu444 New To Java 2 11-06-2007 08:49 PM


All times are GMT +2. The time now is 02:19 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org