Results 1 to 12 of 12
  1. #1
    konayuki1510 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Reading data into array from file and perform the searching

    Hello, I am a new java programmer.

    I have a question about how to reading data into array from file and perform the searching

    in the text file, I have:

    Peter
    Mary
    John
    Tommy
    Katherine
    Betty

    I need to put this text file into array first

    and then when I input the keyword, e.g. "Tommy"

    it can be print out "Correct"

    or if I input "Daisy"

    it can be print out "Wrong"

    can anyone help me to solve the problem?

    Java Code:
        public static void searchdata()
        {
            //create file object
            File file = new File("db.txt");
     
            try
            {
                //create FileInputStream object
                FileInputStream fin = new FileInputStream(file);
     
                byte fileContent[] = new byte[(int)file.length()];
     
                fin.read(fileContent);
     
                String strFileContent = new String(fileContent);
     
                //I'll Stop it here...
            }
            catch(FileNotFoundException e)
            {
                System.out.println("File not found" + e);
            }
            catch(IOException ioe)
            {
                System.out.println("Exception while reading the file " + ioe);
            }
        }

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    In this case you should use a while loop and iterate through the file

    Java Code:
    int ch;
    while( (ch = fin.read()) != -1) {
    //some code here
    //ch will contain the character being read
    }
    Inside the while loop you have to continue appending to an empty string till you encounter \n character. Now add this string to an ArrayList. Clear the string and continue reading till the next \n character. Again add this to ArrayList and so on

    Here is how you have to define and add items to ArrayList
    Java Code:
    ArrayList<String> a = new ArrayList<String>();
    a.add("abc");
    a.add("def");
    Now to check whether the given String is present in the ArrayList, do this
    Java Code:
    a.indexOf("abc")
    If it contains it'll give a positive number, else -1.

    I'm don't think we can do this check on String[] array.

  3. #3
    konayuki1510 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    I try to edit the method like this...
    Java Code:
        public String[] readLines(String filename) throws IOException {
            FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            List<String> lines = new ArrayList<String>();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
            bufferedReader.close();
            return lines.toArray(new String[lines.size()]);
        }
    but I still don't know after I put the data into array to perform the search..

  4. #4
    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

    Loop through the array again and compare each with the value you want to search.

    If your search is one time them while reading the file you can search and add them to the array. Actually it depends on your requirement.

  5. #5
    konayuki1510 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Loop through the array again and compare each with the value you want to search.

    If your search is one time them while reading the file you can search and add them to the array. Actually it depends on your requirement.
    I need to show the inputdialog to enter the keyword, and then if the keyword match the content in array, it shows "Correct", if it cannot, shows "Wrong".

    I had been modified the code, but it still not work, can everyone help me to modify it?

    Java Code:
    public static void search() throws IOException
    	{
    		//create file object
    		File file = new File("db.txt");
    		FileReader fileReader = new FileReader(file);
    		BufferedReader bufferedReader = new BufferedReader(fileReader);
    		List<String> lines = new ArrayList<String>();
    		String line = null;
    		String s;
    		while ((line = bufferedReader.readLine()) != null)
    		{
    			lines.add(line);
    		}
    		for (int i=0; i<lines.length; i++)
    		{
    			s = JOptionPane.showInputDialog("KEYWORD");
    			// Stop it here...
    			}
    		bufferedReader.close();
    		}

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Why do you read the file in the search method? This means each time it is called you load the data again. Do it once in another method.

    Why do you ask for user to enter the search word inside the loop? You would only get it once. Then inside the loop compare each String in your List with the user input. If you find a match output success message and exit the method. If you get to the end of the loop (ie exit without finding a match) display failed message.

  7. #7
    konayuki1510 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Why do you read the file in the search method? This means each time it is called you load the data again. Do it once in another method.

    Why do you ask for user to enter the search word inside the loop? You would only get it once. Then inside the loop compare each String in your List with the user input. If you find a match output success message and exit the method. If you get to the end of the loop (ie exit without finding a match) display failed message.
    that means after I put the text file to the array
    then I close the method, then I write another method about searching using for loop?

    may be like this?
    Java Code:
    public static void read() throws IOException
    	{
    		//create file object
    		File file = new File("db.txt");
    		FileReader fileReader = new FileReader(file);
    		BufferedReader bufferedReader = new BufferedReader(fileReader);
    		List<String> lines = new ArrayList<String>();
    		String line = null;
    		String s;
    		while ((line = bufferedReader.readLine()) != null)
    		{
    			lines.add(line);
    		}
    
    		bufferedReader.close();
    		}

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Yes but you cannot declare your List in that method as it will be out of scope for other methods. I also see that you have made your methods static. Is this a requirement? I try and avoid static methods (except main) as much as possible.
    Java Code:
    class Whatever {
        // declare instance variables here. The List for example.
    
        // constructor if needed
    
        // methods such as search and read
    
        public void doStuff() {
            // call other methods
        }
    
        public static void main(String[] args) {
            Whatever w = new Whatever();
            w.doStuff();
        }
    }

  9. #9
    konayuki1510 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Yes but you cannot declare your List in that method as it will be out of scope for other methods. I also see that you have made your methods static. Is this a requirement? I try and avoid static methods (except main) as much as possible.
    Java Code:
    class Whatever {
        // declare instance variables here. The List for example.
    
        // constructor if needed
    
        // methods such as search and read
    
        public void doStuff() {
            // call other methods
        }
    
        public static void main(String[] args) {
            Whatever w = new Whatever();
            w.doStuff();
        }
    }
    Maybe I had not give the detail of the question.

    I need to write a project about the CD collection.

    The requirement is
    • Create a menu of options controlled with an integer variable for the programme including
    o input a new entry from a keyboard
    o search for an entry
    o print all the entries so far
    o Quit
    • Declare a series of arrays to hold details of CDs. A CD consists of (for example) the title, number of tracks, artist, (“Superstition”, 20, “Stevie Wonder”). The program should be able to store CD details in the declared arrays.
    • A single entry can be entered by the user typing the details in and the entry can be printed out.
    • Your program should provide a search method that allows the title of a given CD to be looked up in the array and the details printed to the screen.

    also, some addition requirement need to do in this project.

    • Expand menu with additional options and write code for the following:
    • An entry can be updated
    • Data can be sorted by artist and entries displayed in that order to the screen
    • Data can be stored in a text file
    • Data can be retrieved into the array from a text file.

    I had been finished the "add entry", "print entry". But I don't know how to do the sorting and searching after putting data into the array.

    Can everyone give some suggestion for me to do this project?


    This is the detail of this project. Can everyone help me?

    Sorry about that the question is too long, but it is urgent. I hope somebody can help me. Thanks alot!!

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by konayuki1510 View Post
    This is the detail of this project. Can everyone help me?
    Yes. All the advice you have been given so far still applies. You just have to understand the concepts and apply them to your assignment.

    but it is urgent.
    I don't give a rat's backside. Your time constraints are your problem not ours. You are not entitled to more help than others on this forum. You are not entitled to help sooner than others on this forum.

  11. #11
    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

    Quote Originally Posted by Junky View Post
    I don't give a rat's backside. Your time constraints are your problem not ours. You are not entitled to more help than others on this forum. You are not entitled to help sooner than others on this forum.
    And that's a type request I really hate.

  12. #12
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by konayuki1510 View Post
    I try to edit the method like this...
    but I still don't know after I put the data into array to perform the search..
    This is exactly what I have mentioned in my first post.
    Use indexOf(String) method provided by ArrayList.

    Or as one previous post suggested. Search for the string inside the loop itself. Have some boolean flag initially set to false. When you encounter the string in the read file, set it to true and break from the loop.

Similar Threads

  1. Help with reading / searching a .txt file
    By Absolute in forum New To Java
    Replies: 0
    Last Post: 03-11-2010, 05:39 AM
  2. Replies: 1
    Last Post: 12-03-2009, 07:03 AM
  3. Reading data to file
    By puk284 in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2009, 03:19 PM
  4. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 07:29 PM
  5. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 AM

Posting Permissions

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