Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default Need help debugging this file parsing program I am writing

    Well I wrote this code

    Java Code:
    import java.io.*;
    
    
    public class verncommandshell
    {
        public static void main(String [] args)throws IOException
        {
    	int i = 0;
    	BufferedReader br = new BufferedReader
    	    (new InputStreamReader(System.in));
    	File commandfile;
    	try//reads in the command txt file and stores it in an array	    
    	    {
    	      commandfile = new File(commands.txt);
    	      commandreader = new FileReader(commandfile);
           	      String[] commandbuffer = new Strimg[commandfile.length];
    	      while(commandreader != -1)
    	          {
    		      commandbuffer[i] = commandreader.read();
    		      i++;
    	          }
    	      commandreader.close();		       
                  commandfile.close();
    	    }
    
    	 catch(IOException e)
    	   {
    	     e.printStackTrace();
    	   }
    	for(i = 0; i < commandbuffer.length; i++)
            {
    	    System.out.print(commandbuffer[i]);
       
    	}
       }
    }
    and this is the compiler output

    Java Code:
    Script started on Fri 02 Jul 2010 08:14:27 PM EDT
    Welcome Commander system initilizing
    Command line interface intilized java    javac verncommandshell.java
    verncommandshell.java:23: cannot find symbol
    symbol  : variable commands
    location: class verncommandshell
    	      commandfile = new File(commands.txt);
    	                             ^
    verncommandshell.java:24: cannot find symbol
    symbol  : variable commandreader
    location: class verncommandshell
    	      commandreader = new FileReader(commandfile);
    	      ^
    verncommandshell.java:25: cannot find symbol
    symbol  : class Strimg
    location: class verncommandshell
           	      String[] commandbuffer = new Strimg[commandfile.length];
           	                                   ^
    verncommandshell.java:25: cannot find symbol
    symbol  : variable length
    location: class java.io.File
           	      String[] commandbuffer = new Strimg[commandfile.length];
           	                                                     ^
    verncommandshell.java:26: cannot find symbol
    symbol  : variable commandreader
    location: class verncommandshell
    	      while(commandreader != -1)
    	            ^
    verncommandshell.java:28: cannot find symbol
    symbol  : variable commandreader
    location: class verncommandshell
    		      commandbuffer[i] = commandreader.read();
    		                         ^
    verncommandshell.java:31: cannot find symbol
    symbol  : variable commandreader
    location: class verncommandshell
    	      commandreader.close();		       
    	      ^
    verncommandshell.java:32: cannot find symbol
    symbol  : method close()
    location: class java.io.File
                  commandfile.close();
                             ^
    verncommandshell.java:39: cannot find symbol
    symbol  : variable commandbuffer
    location: class verncommandshell
    	for(i = 0; i < commandbuffer.length; i++)
    	               ^
    verncommandshell.java:41: cannot find symbol
    symbol  : variable commandbuffer
    location: class verncommandshell
    	    System.out.print(commandbuffer[i]);
    	                     ^
    10 errors
    Command line interface intilized exit
    
    Script done on Fri 02 Jul 2010 08:14:52 PM EDT
    I am confused on what I am doing wrong so guys help?:o
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You've not initialize commands variable in your code before use. Is this the complete code you've written?

  3. #3
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    I thought I can't initialize it that as it is the name of a .txt file that I am parsing and loading into an array. Can you show me an example of or a link to some better source than the lecture notes I am using then? Thanks a lot guys
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    new File(commands.txt);
    Is it supposed to be a String?
    new File("commands.txt");

  5. #5
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    No at least I don't think so, here is what I was working off of:

    Java Code:
    
    package Files13;
    
    
    import java.io.*;
    public class Ex132b 
    
    
    {
    	/**
    	 Kyle Sponable
    	 Ex 13-2b
    	 This program will output to a file using characted streams
    	 
    	 */
    	
    	public static void main(String[] args)throws IOException
    	{
    		InputStream is;
    		OutputStream os;
    		int value;
    		String fout, fin;
    			
    		BufferedReader br = new
    		       BufferedReader(new InputStreamReader(System.in));
    		File f, ff;
    		System.out.print("Enter input filename: ");
    		fin = br.readLine();
    		System.out.print("Enter output filename: ");
    		fout = br.readLine();
    		try
    		{
    			ff = new File(fin);
    			f = new File(fout);
    			is = new FileInputStream(ff);
    			os = new FileOutputStream(f);
    			//System.out.println("Enter names numbers (<ctrl>z to end: ");
    			value = is.read();
    			while (value != -1)
    			{
    				os.write(value);
    				value = is.read();
    			}
    			is.close();
    			os.close();
    			System.out.println("/n" + fout + " created ");
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    			
    		}
    	}
    }
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I'm confused. You've posted two different programs.
    Which one are you working on?

  7. #7
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    The first one is the one I am working on, the second one is some source from some of my old notes that I took last semester in programming class and what I am basing the file parsing off of. I was not sure if seeing that might help you guys out
    Last edited by The_Sponzy_Paradox; 07-03-2010 at 04:47 AM.
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    OK. How are the list of error coming?

  9. #9
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    Here is the code as I have it now it has gotten some what better though I am still unsure what is wrong. Thanks guys Norm in particular
    Java Code:
    import java.io.*;
    
    
    public class verncommandshell
    {
        public static void main(String [] args)throws IOException
        {
    	int i = 0;
    	BufferedReader br = new BufferedReader
    	    (new InputStreamReader(System.in));
    	File 'commands.txt' ;
    	try//reads in the command txt file and stores it in an array	    
    	    {
    	      commandfile = new File(commands.txt);
    	      commandreader = new FileReader(commandfile);
           	      String[] commandbuffer = new Strimg[commandfile.length];
    	      while(commandreader != -1)
    	          {
    		      commandbuffer[i] = commandreader.read();
    		      i++;
    	          }
    	      commandreader.close();		       
                  commandfile.close();
    	    }
    
    	 catch(IOException e)
    	   {
    	     e.printStackTrace();
    	   }
    	for(i = 0; i < commandbuffer.length; i++)
            {
    	    System.out.print(commandbuffer[i]);
       
    	}
       }
    }

    Here is the output:
    Java Code:
    Command line interface intilized javac verncommandshell.java
    verncommandshell.java:20: unclosed character literal
    	File 'commands.txt' ;
    	     ^
    verncommandshell.java:20: not a statement
    	File 'commands.txt' ;
    	^
    verncommandshell.java:20: unclosed character literal
    	File 'commands.txt' ;
    	                  ^
    verncommandshell.java:20: not a statement
    	File 'commands.txt' ;
    	              ^
    4 errors
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

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

    Default

    Where have you seen code like this as it's not Java?
    Java Code:
    File 'commands.txt' ;
    To declare any variable you need a type name, such as File, followed by a variable name, but this can't be enclosed in single quotes. If you want to declare and initialize a variable, then you need the above and set it equal to something that returns a File object such as a constructor, for e.g.,

    Java Code:
    File myFile = new File("myDirectory/mySubdirectory/FileName.txt");

    What are you trying to do here, create a new File that is associated with a File on the disk called "commands.txt"?

  11. #11
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    Well I fixed everything but one last error. And I am not sure, I declared a String array but I cannot insert the tokens into the string array

    here is the code

    Java Code:
    
    import java.io.*;
    
    
    public class verncommandshell
    {
        public static void main(String[] args)throws IOException
        {
    	int i = 0;
    	BufferedReader br = new BufferedReader
    	    (new InputStreamReader(System.in));
    	File commandfile = new File(commands.txt");//change later
    	FileReader fr = new FileReader(commandfile);
    	StreamTokenizer st = new StreamTokenizer(fr);
    	String[] commandbuffer = new  String[(int)commandfile.length()];//length of the file
    	
       try//reads in the command txt file and stores it in an array
    	    {
    		while(!st.TT_EOF)//while token is not the EOF
    	          {
    		      commandbuffer[i] = (st.nextToken()); //error
    		      i++;
    	          }
    	    }
    
    	 catch(IOException e)
    	   {
    	     e.printStackTrace();
    	   }
    	for(i = 0; i < commandbuffer.length; i++)//going to go away here for testing only
            {
    	    System.out.print(commandbuffer[i] +  "\n" );
    
          	}
       }
    }
    error is:
    Java Code:
    verncommandshell.java:28: incompatible types
    found   : int
    required: java.lang.String
    		      commandbuffer[i] = (st.nextToken());
    		                                      ^
    Last edited by The_Sponzy_Paradox; 07-07-2010 at 04:38 AM.
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What your error message really talking about? Did you read it? commandbuffer is a String array and you are trying to store an int value in it (nextToken())? Can you do that?

  13. #13
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    Yeah, that is why I am confused it should just be a string and should accept a string no problem. I never created an Int array. So I do not understand why it is looking for an Integer. It is a string cause I can convert it to an integer and it will compile.
    You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    why it is looking for an integer
    Read the API doc for the nextToken() method! What type of data does it return? Is it at String or something else?

Similar Threads

  1. writing to a excel file from java program
    By priyankabhar in forum New To Java
    Replies: 15
    Last Post: 01-17-2014, 12:39 PM
  2. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  3. help with BNF Grammar program using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 10-21-2009, 08:00 PM
  4. Due Tommorow Writing a Program Please Help
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-23-2008, 03:15 AM
  5. Replies: 1
    Last Post: 07-31-2007, 12:39 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
  •