Results 1 to 7 of 7
  1. #1
    FunkyProg is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Compare grocery list with leaflet

    Hi all!

    I used to do some java programing a few years ago. Now I'd like to write a small program that searches for a list of words (my grocery list) in a text file (the supermarket flyer available in text format). I hope someone can help me with the structure/syntax. Here's what I got so far...

    import ??
    public static etc.
    Create a table called "grocery" with a line for every word in the list of goods
    for(int i=0; i<n; i++) grocery[i] = ReadLine(); // Where do I specify that the file is grocery.txt?


    String flyerLine = Readline(); // Where do I specify that the file is flyer.txt?
    String[]flyerWords = flyerLine.split(" "); //split the string into words
    for(int i=0; i<n; i++) { //for each word in the grocery table
    for(int j=0; i<m; j++) //search every word in the leaflet table
    if(grocery[i] == flyerWords[j]) output[k] = flyerWords[j]; k++; //compare & copy to a table
    }
    output > output.txt

    Okay I know this is really basic, but it's been so long since I haven't written a program. Please help me!

    Thanks

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Where do I specify that the file is grocery.txt?
    You need some kind of reader. I prefer BufferedReaders myself, an example:
    Java Code:
    try {
                BufferedReader in = new BufferedReader(new FileReader(new File("myfile.txt")));
                String line;
                while((line = in.readLine()) != null){
                    //do something with the line
                }
            } catch (IOException ex) {
                Logger.getLogger(Misc.class.getName()).log(Level.SEVERE, null, ex);
            }
    This example reads until the end of the file, then the while loop breaks. Hope this helps!

  3. #3
    FunkyProg is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default errors

    Thanks Quad64bit, this was helpful but I got 7 errors.
    Below is an updated version of my code. The errors are of the type "cannot find symbol":
    1. symbol : variable Level
    2. symbol : class Misc
    3. symbol : variable Logger
    4. symbol : variable in
    5. symbol : variable Level
    6. symbol : class Misc
    7. symbol : variable Logger

    Any help will be appreciated! This is not a student homework btw, I'm a grad student in life sciences trying to make ends meet with grocery rebates...

    The program :

    Java Code:
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class epicerie 
    {
             public static void main(String[] args) 
             {
                    String[]listeTab;
                    listeTab = new String[320];  
    	
    		String[]resultTab;
    		resultTab = new String[320];
    
    		try {
               		 BufferedReader in = new BufferedReader(new FileReader(new File("liste.txt")));
                		String line;
                		while((line = in.readLine()) != null)
    			{
                    		int i = 0;
    				listeTab[i] = line;
    				i++;
                		}
           		 } catch (IOException ex) {Logger.getLogger(Misc.class.getName()).log(Level.SEVERE, null, ex);}
    
    		try {
               		 BufferedReader in2 = new BufferedReader(new FileReader(new File("iga.txt")));
                		String line2;
                		while((line2 = in.readLine()) != null)
    			{
                    		String[]lineWords = line2.split(" ");
    				for(int j = 0; j < lineWords.length; j++ )
    					{
    						for(int k=0; k<320; k++)
    							{	String str1 = listeTab[k];
    								String str2 = lineWords[j];
    								int l = 0;
    								if(str1.equals(str2))
    								{resultTab[l] = str1; l++;}
    							}
    					}
    
                		}
           		 } catch (IOException ex) {Logger.getLogger(Misc.class.getName()).log(Level.SEVERE, null, ex);}
    
    		try
    		{
        			FileWriter fstream = new FileWriter("out.txt");
          	 		 BufferedWriter out = new BufferedWriter(fstream);
       			 for(int m=0; m<resultTab.length; m++)
    				{
    					out.write(resultTab[m]+'\n');m++;
        				}
        			out.close();//Close the output stream
        		}
    		catch (Exception e)
    		{
          			System.err.println("Error: " + e.getMessage()); //Catch exception if any
    		}
    		
    
      	}
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-25-2011 at 04:26 AM. Reason: Moderator Edit: Code tags added

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

    Default

    One of your problems and the source of several of your error messages is that you're copying his code blindly and without thinking which you should never do. Don't use his variable names and all, but instead borrow the concepts and use your own variables and references.

    Also, I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by FunkyProg View Post
    1. symbol : variable Level
    3. symbol : variable Logger
    5. symbol : variable Level
    7. symbol : variable Logger
    You are missing the logging import. Figure out how can you import it.

    Quote Originally Posted by FunkyProg View Post
    2. symbol : class Misc
    6. symbol : class Misc
    Where is the Misc class? Import it if it's in some another package.

    Quote Originally Posted by FunkyProg View Post
    4. symbol : variable in
    See carefully, you are declaring some another variable name for reader and trying to read it using some another variable [in].

    Plus, from next time onwards, use Code Tags while posting your code.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Quote Originally Posted by goldest View Post
    You are missing the logging import. Figure out how can you import it.
    Based on his original post, I don't think that his original intent was to log errors but rather just to write information to a file. Instead I think he ran into trouble when he copy and pasted quad's code that had useful information on using a BufferedWriter but was also using logging of error messages. Having said this, he probably needs to look into and learn to use logging at some point.
    Last edited by Fubarable; 01-25-2011 at 04:49 AM.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    To the OP, my forum compatriots make good points. Also wanted to add, you can use a PrintWriter for writing a file out, its a bit easier to work with than the BufferedReader, requiring only a String representing the file name:
    Java Code:
    try {
                PrintWriter out = new PrintWriter("myOutFile.txt");
                out.println("I wrote this to a file with a PrintWriter!");
                out.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Sorts.class.getName()).log(Level.SEVERE, null, ex);
            }
    As others have said, don't blind copy this, play with it on its own, understand it, then implement it in your code. I also forgot to mention in my previous post, you should always close the reader or writer when you're done with it (like I did here). You should close when all of your reads are complete or all of your writes are complete. If you don't close a file you are writing, the file often ends up incomplete.

Similar Threads

  1. cannot compare string
    By hmcoder in forum New To Java
    Replies: 1
    Last Post: 01-19-2011, 03:57 PM
  2. how to compare two timestamp
    By baktha.thalapathy in forum New To Java
    Replies: 1
    Last Post: 06-11-2010, 10:05 AM
  3. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  4. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 AM

Tags for this Thread

Posting Permissions

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