Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Issue

  1. #1
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    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.

    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 11:25 PM. Reason: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can't declare a mthod like so:
    Java Code:
    public void load(String "pirate.txt")
    Instead do something like:
    Java 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.

  3. #3
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    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.

    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();
    }
           
                               
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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:
    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:
    Java 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.

  5. #5
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    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(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 :p

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please show your text file.

  7. #7
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    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
    Here, see what light you can make of this please :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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?:

    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);
          }

  9. #9
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    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

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

    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.

  11. #11
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    I did and the println for the pirate produced

    Java Code:
     pirate: Phrases@92dcdb
    Confused :confused:

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

    Default

    In that case the contents of your text file actually contains

    Java 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:

    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 02:45 AM.

  13. #13
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    I have just checked the text file and it definately still contains
    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
    hmm.

  14. #14
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Forget what I said above the file had been overidden :p.

    Now when I do read from the file that does contain:

    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
    The printout is instead:

    Java 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:

    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();
    }
    }
    And my Phrases code:

    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;
        }
        
    }
    Could someone take a quick look over this please and give me feedback about why it is reading the text file as shown?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Where's your main method?

    Also, we have recommended that you not hard-code your file String as you are doing here:
    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")));
    Why have you not fixed this yet?
    Last edited by Fubarable; 11-28-2009 at 08:42 PM.

  16. #16
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    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.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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:
    Java Code:
      public static void main(String[] args) {
    
        // bunch of code goes in here
    
      }

  18. #18
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    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 :p

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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 :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 10:43 PM.

  20. #20
    FlashNinja is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. PDF Box issue
    By jazz2k8 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2009, 12:04 PM
  2. Performance Issue
    By hiranya in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-25-2008, 12:32 AM
  3. NullPointerException issue
    By fritz1474 in forum AWT / Swing
    Replies: 2
    Last Post: 09-03-2008, 06:21 PM
  4. Performance issue
    By mathes_n in forum Web Frameworks
    Replies: 8
    Last Post: 09-02-2008, 06:11 AM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •