Results 1 to 14 of 14
Like Tree3Likes
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By DevDeckardCain

Thread: How to read text file in array and show it to textfield :(

  1. #1
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default How to read text file in array and show it to textfield :(

    Hello everybody. I'm a newbie;

    I have a problem about read text file in array and show it to textfield :( ;

    Content the file:



    I want read line 1 to textfield 1, line 2 to textfield 2,....

    Can You help me? :( I just learnt java language since 2 weeks.

    If i write wrong grammar, i'm sorry :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: How to read text file in array and show it to textfield :(

    What have you tried?
    Will each line from the file go in a separate textfield?
    How many lines are in the file?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    yes, each line from the file go in a separate textfield. i want read 1 record (6 line) to array. From array, show it to textfield.

    Sorry, My English is very bad :(

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

    Default Re: How to read text file in array and show it to textfield :(

    What have you tried?
    Do you have any specific java programming questions?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    I want read line by line (6 line to 6 text field) to textfield. And next 6 line if I click Next button


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

    Default Re: How to read text file in array and show it to textfield :(

    What have you tried? Post your code.
    Do you have any specific java programming questions?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    I want read a file into an array ? @@

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

    Default Re: How to read text file in array and show it to textfield :(

    What have you tried?
    Post your code.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    I want read a file into an array. :(

    Java Code:
    try {
                int count = 1;
            
    
                FileReader fr= new FileReader("abc.txt");
                BufferedReader rd= new BufferedReader(fr);
                rd.readLine();
                while (rd.readLine() != null) {
                    count++;
                }
    
                rd.close();
                fr.close();
            } catch (FileNotFoundException fe) {
                JOptionPane.showMessageDialog(this, "ERROR " + fe, "!!!", JOptionPane.ERROR_MESSAGE);
            }
    Last edited by HungNQ; 05-30-2014 at 07:50 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: How to read text file in array and show it to textfield :(

    The readLine() method returns a String that should be saved in a variable. The posted code does not save what the method returns:
    String aLine = rd.readLine(); // read next line into aLine
    HungNQ likes this.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    Thanks, but, It isn't that I need. I will retry :) Thank you very much!

    I want delete this topic. Thanks :)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: How to read text file in array and show it to textfield :(

    The program must read lines from the file into variables before the lines can be stored into an array.
    HungNQ likes this.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    May 2014
    Location
    The Internet
    Posts
    5
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    Hello, HungNQ. I've written a small program to help you out. Hopefully you understand it, it's been a while since I've had to explain programming to someone (I'm not a pro either so the code may be a little messy), but it does work. If you need more help you can message me :)

    *I've removed the ======== lines from the screenshot that you've given me, it makes it easier to parse that way. An example document is included*

    abc.txt on my Desktop
    Java Code:
    123
    123123
    123
    12312
    3123
    123123
    3232
    232
    32
    232
    3232
    323
    232
    32
    232
    2323
    232
    23
    123123
    3232
    232
    32
    232
    3232
    Main.java
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    public class Main {
    	//Hello, HungNQ. All you would have to do to get this to work, is grab the data from the whereTheTextFieldsWillReadFrom array list and assign it to the
    	//corresponding TextField, like so:
    	//TextField0 = whereTheTextFieldsWillReadFrom.get(0);
    	//TextField1 = whereTheTextFieldsWillReadFrom.get(1);
    	//and so on.
    	
    	static ArrayList<String> myFileContents = readFile("/home/deckardcain/Desktop/abc.txt");
    	static int counter = 0, numberOfFields = 6, numberOfEntries = myFileContents.size() / numberOfFields;
    	static ArrayList<String> whereTheTextFieldsWillReadFrom = new ArrayList<String>();
    	
    	public static void main(String args[]) {
    		//Setup the array with enough space to store out variables
    		for(int i = 0; i != numberOfFields; i++) {
    			whereTheTextFieldsWillReadFrom.add("");
    		}
    		
    		//This iterates through the whole file
    		for(int c = 0; c != numberOfEntries; c++) {
    			//This emulates the entry into the field and the next button trigger, you'll have to change this to make it actually enter it into the field
    			System.out.println("TextField: " + counter);
    			System.out.println("------------------------------------");
    			//i is equal to how many times we've pressed next * number of TextFields we have
    			//while i does not equal the number of TextFields we have + (the number of TextFields we have * the number of times we have pressed next)
    			//increment i
    			for(int i = counter * numberOfFields; i != numberOfFields + (numberOfFields * counter); i++) {
    				//input the values into the array so that the TextFields can get at them
    				whereTheTextFieldsWillReadFrom.set(i - (counter * numberOfFields), myFileContents.get(i));
    			}
    			//output the array to show you that it does work, this can be removed
    			for(int i = 0; i != whereTheTextFieldsWillReadFrom.size(); i++) {
    				System.out.println(whereTheTextFieldsWillReadFrom.get(i));
    			}
    			//I've pressed the next button, which increments counter by one
    			counter++;
    		}
    	}
    	
    	public static ArrayList<String> readFile(String location) {
    		ArrayList<String> data = new ArrayList<String>();
    		try{
    			BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(location))));
    			String strLine;
    
    			while ((strLine = br.readLine()) != null)   {
    				data.add(strLine);
    			}
    			br.close();
    		}catch (Exception e){
    			System.err.println("Error: " + e.getMessage());
    		}
    		return data;
    	}
    }
    HungNQ likes this.

  14. #14
    HungNQ is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: How to read text file in array and show it to textfield :(

    Quote Originally Posted by DevDeckardCain View Post
    Hello, HungNQ. I've written a small program to help you out. Hopefully you understand it, it's been a while since I've had to explain programming to someone (I'm not a pro either so the code may be a little messy), but it does work. If you need more help you can message me :)

    *I've removed the ======== lines from the screenshot that you've given me, it makes it easier to parse that way. An example document is included*

    abc.txt on my Desktop
    Java Code:
    123
    123123
    123
    12312
    3123
    123123
    3232
    232
    32
    232
    3232
    323
    232
    32
    232
    2323
    232
    23
    123123
    3232
    232
    32
    232
    3232
    Main.java
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    public class Main {
    	//Hello, HungNQ. All you would have to do to get this to work, is grab the data from the whereTheTextFieldsWillReadFrom array list and assign it to the
    	//corresponding TextField, like so:
    	//TextField0 = whereTheTextFieldsWillReadFrom.get(0);
    	//TextField1 = whereTheTextFieldsWillReadFrom.get(1);
    	//and so on.
    	
    	static ArrayList<String> myFileContents = readFile("/home/deckardcain/Desktop/abc.txt");
    	static int counter = 0, numberOfFields = 6, numberOfEntries = myFileContents.size() / numberOfFields;
    	static ArrayList<String> whereTheTextFieldsWillReadFrom = new ArrayList<String>();
    	
    	public static void main(String args[]) {
    		//Setup the array with enough space to store out variables
    		for(int i = 0; i != numberOfFields; i++) {
    			whereTheTextFieldsWillReadFrom.add("");
    		}
    		
    		//This iterates through the whole file
    		for(int c = 0; c != numberOfEntries; c++) {
    			//This emulates the entry into the field and the next button trigger, you'll have to change this to make it actually enter it into the field
    			System.out.println("TextField: " + counter);
    			System.out.println("------------------------------------");
    			//i is equal to how many times we've pressed next * number of TextFields we have
    			//while i does not equal the number of TextFields we have + (the number of TextFields we have * the number of times we have pressed next)
    			//increment i
    			for(int i = counter * numberOfFields; i != numberOfFields + (numberOfFields * counter); i++) {
    				//input the values into the array so that the TextFields can get at them
    				whereTheTextFieldsWillReadFrom.set(i - (counter * numberOfFields), myFileContents.get(i));
    			}
    			//output the array to show you that it does work, this can be removed
    			for(int i = 0; i != whereTheTextFieldsWillReadFrom.size(); i++) {
    				System.out.println(whereTheTextFieldsWillReadFrom.get(i));
    			}
    			//I've pressed the next button, which increments counter by one
    			counter++;
    		}
    	}
    	
    	public static ArrayList<String> readFile(String location) {
    		ArrayList<String> data = new ArrayList<String>();
    		try{
    			BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(location))));
    			String strLine;
    
    			while ((strLine = br.readLine()) != null)   {
    				data.add(strLine);
    			}
    			br.close();
    		}catch (Exception e){
    			System.err.println("Error: " + e.getMessage());
    		}
    		return data;
    	}
    }

    Thank bro so much. It is very useful for me :)
    Last edited by HungNQ; 05-30-2014 at 11:18 PM.

Similar Threads

  1. Read text file into array and display it
    By Waflix in forum New To Java
    Replies: 1
    Last Post: 07-10-2012, 09:58 PM
  2. Save text in Textfield to an array
    By Norah in forum New To Java
    Replies: 1
    Last Post: 06-12-2012, 05:00 PM
  3. read from text file into array of objects
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 12:40 PM
  4. Replies: 2
    Last Post: 04-01-2011, 03:04 PM
  5. Replies: 7
    Last Post: 11-01-2010, 02: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
  •