Results 1 to 17 of 17
  1. #1
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default Reading from a text file into an array

    I am trying to read a text file, enter it into an array, and they can do a search in the array.

    My text file contains the following:

    Java Code:
    Interview with Vampire;Anne Rice;available;/n
    Working with Java;Unknown Author;not available;/n
    Book3;author3;available;/n
    book4;author4;not available;/n
    book5;author5;available;/n
    the code I wrote so far is able to read each line and output it onto the screen but I want to put into an array (title; author;availability).

    Java Code:
    ublic void readFromFile(){
    		FileReader text;
    		try {
    			FileReader file = new FileReader("book.txt");
    			BufferedReader buff = new BufferedReader(file);
    			boolean eof = false;
    			while (!eof) {
    				String line = buff.readLine();
    				if (line== null)
    					eof = true;
    				else
    					
    					System.out.println(line);
    			}
    			buff.close();
    		}
    		
    		catch (IOException ex){
    		    ex.printStackTrace();
    		}
    	}

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    hi there, instead of the system.out.println in the else statement you have to read the words in the line such that when it gets to a semicolon ; it stops and saves the word in an array. You should have three array (title, author and availability) to you as well need to you an int for example such that when int = 1 you save the read word in the title array and then make the int = 2, but be careful to keep counting the level of the array you are in, so that the next line is saved in the the second array space
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    and another solution can be by creating a 2D array or book class, such that you can form a Book array that saves the title, author and availability respectively. but this depends on your assignment and what you know
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    Thank you. How do I make it stop at the semicolon?

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    your can state a condition statement, for example while(!buff.read().equals(';')). this is the simplest way to do it.

    I am not that well practiced in the Buffered but i solved this exercise before by creating a Book class and used an array of Book, but I know that you can read a semicolon by BufferedReader as well, so that would help I believe.
    Last edited by baf06; 04-10-2011 at 05:22 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  6. #6
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    would the code look like this?

    Java Code:
    try {
    			FileReader file = new FileReader("book.txt");
    			BufferedReader buff = new BufferedReader(file);
    			boolean eof = false;
    			while (!eof) {
    				String line = buff.readLine();
    				if (line== null)
    					eof = true;
    				else
    					while (!equals(';')){
    					btitle.add(line);	
    					}
    				while (!equals(';')){
    					bauthor.add(line);
    				}
    				while (!equals(';')){
    					bavailable.add(line);
    				}
    			}
    			buff.close();
    		}
    		
    		catch (IOException ex){
    		    ex.printStackTrace();
    		}
    	}

  7. #7
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    String line gives you the content of the whole line, instead you need to read each letter of this line separately until you reach a semicolon, and when a ; is reached then you move to the next field you ought to fill (title, author, or availability) for example :

    change this :
    else
    while (!equals(';')){
    btitle.add(line);
    }
    while (!equals(';')){
    bauthor.add(line);
    }
    while (!equals(';')){
    bavailable.add(line);
    }



    Java Code:
        else {
        int i = 0;
        while (line.hasNext()){ //this will reads the next letter
            String lett = line.Next();  //reads the next letter in the line
            if(! lett.equals(";")){   //if the letter is not a semicolon
                if(i==0){
                    btitle += lett;     //add it to the title 
                } else if (i==1){
                    bauthor += lett;    //or to the author
                } else if (i==2){
                    bavai += lett;      //or to the availability
                }                       //depenfing on the i
            } else i++; //when you get a smicolon add the i so it moves to the second String to add it to
        }
    }
    what you wrote will check if the whole line is equal to a semicolon, and that is always false in your case.
    Hope I helped and explained what should be done and not just solved it for you.
    Last edited by baf06; 04-10-2011 at 06:03 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Instead of doing all this, there is a nice and easy method in String called split().

    Java Code:
    //...
    String line = buffer.readLine();
    String parts[] = line.split(";");
    //...

  9. #9
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    interesting...

    that is the first time i am seeing the split() method. if i use that method, how would I add the parts to the title, author, and available arrays?

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    part[0] is the title, part[1] is the author, part[2] is the availability.

  11. #11
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    ahh..I see that now...but new problem is that it is only reading the first line and not going onto lines 2-5

    Java Code:
    try {
    			FileReader file = new FileReader("book.txt");
    			BufferedReader buff = new BufferedReader(file);
    			boolean eof = false;
    			int i=0;
    			while (!eof) {
    				String line = buff.readLine();
    
    				if (line == null)
    					eof = true;
    				else {
    					
    						String parts[] = line.split(";");
    						btitle.add(parts[0]);
    						bauthor.add(parts[1]);
    						bavailable.add(parts[2]);
    						System.out.println(btitle);
    						System.out.println(bauthor);
    						System.out.println(bavailable);
    					
    					
    				}
    			}
    		}
    
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The code looks fine. If it runs for the first line only, then the End Of File is reached early?

  13. #13
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    yes, so my output is:

    [book1]
    [a1]
    [not available]

  14. #14
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ok then double-check your file and see if it is correct.

  15. #15
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    I think I know why...the problem is with my source file.

    I want the source file to look like:

    title1;author1;available;/n
    title2;author2;available;/n
    ...

    but instead it is:
    title1;author1;available;/ntitle2;author2;available;/n

  16. #16
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    this is how i am writing to the source file:

    Java Code:
    try { // all the I/O stuff must be a in try/catch
    				File file = new File("book.txt");
    				if (!file.exists()) {
    					file.createNewFile();
    					System.out.println("new file:" + file);
    				}
    				String bookRecord = title + ";" + author +";" + available + ";" + "/n";
    				
    				System.out.println(bookRecord);
    				
    				FileWriter writer = new FileWriter(file,true);
    
    				BufferedWriter buwriter = new BufferedWriter(writer);
    				buwriter.flush();
    				buwriter.write(bookRecord);
    				buwriter.close();
    
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			}
    I figured out what was wrong...I had to add an additional line to my initial code writing the file..it now outputs correctly
    Last edited by xyknight; 04-10-2011 at 11:10 PM.

  17. #17
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  3. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 PM
  4. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 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
  •