Results 1 to 7 of 7
  1. #1
    Raisty is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Red face Using text files to get values

    Hi,

    I cant remember how to get specific values from an existing text file.

    I think I need to use an array but am unsure how to populate it and retrieve the correct positions. I want to use the values to update variables for use in algorithms.

    I am trying to manipulate some existing code from a previous project but this has only set data to use.

    public class BankModelED {
    private BankED accArray[] = {new BankED (1001, "abcd", 10000.00),
    new BankED (1002, "efgh", 25020.00),
    new BankED (1003, "mnop", 2300.99),
    new BankED (1004, "Vwxy", 23456.99)
    };

    public BankED getAccountED(long lngAccNo) {
    for (int i = 0; i < accArray.length; i++) {
    if (accArray[i].getAccountNoED() == lngAccNo) {
    return accArray[i];
    }
    }
    return null;
    }
    }

    If someone could provide an example on how to read and return values would be much appreciated.

    Thanks.

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    What is your exact requirement,

    Do you have some text file which is containing some text value
    And you need to search some "text" in text file if found then what do you want to return
    If not then what

    Please explain
    sanjeev,संजीव

  3. #3
    Raisty is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    I am writting a program to return specific user stats. I have a text file that is set by lines and columns.

    E.g. login value1 value2
    EA0 13.45 15.25

    I need to search for the login and use the corresponding value (always floats) with various formulas and display results.

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    OK

    just tell me the format of text file is fixed like you mention
    loginId value1 value2

    and now if the login ID matched then you need to get the value1 and value2 for that login id and exit from loop

    Is this your requirement?
    sanjeev,संजीव

  5. #5
    Raisty is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes this is correct and the text file format is always fixed. The login will be entered by the user to search.

    I need to locate loginId in column 1 and "value1" and "value2" from the line to assign to variables.

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Check the below code

    i tested this for the data

    abc 1.2 1.5
    xyz 765 67.75
    hello 765 87.67
    abc 65.76 87.87
    abc 87.87 87.87

    and got the result


    Value1..HERE..::765
    Value2..HERE..::87.67

    Is this correct, you may change the method return type, or any basic changes if require

    Java Code:
    public class TestFileReading
    {
    
    	public static void main(String[] args) 
    	{
    		checkFile("hello");
    	}
    	    public static boolean checkFile(String loginId) {
    
            String fileLoginId = "";
    		String value1 = "";
    		String value2 = "";
    
            File file = new File("C:\\loginDetails.txt");
            final String newLine = System.getProperty("line.separator");
            try {
                Scanner input = new Scanner(file);
                while (input.hasNext()) {
                    fileLoginId = input.next();
                    if (loginId.equals(fileLoginId)) {
                        if(input.hasNext())
    						value1 = input.next();
    					if(input.hasNext())
    						value2 = input.next();
    				
    				System.out.println("Value1..HERE..::" + value1);
    				System.out.println("Value2..HERE..::" + value2);
    				return true;
                    }
                }
    
            } catch (Exception error) {
                System.out.println("Error accessing file!" + newLine);
                JOptionPane.showMessageDialog(null, "Error accessing file!");
            }
            return false;
        }
    }
    sanjeev,संजीव

  7. #7
    Raisty is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok. I think this is quite different from the way I learned (I studied Java some years ago).
    But I think this is probably more efficient.

    I assume that input.next scans through blank spaces to find the next set of data and assigns them to the variables you have declared. Although I havent integrated this code yet I think this will suit what I am trying to do.

    I would like to have this program eventually read from a flat file that exists in a network directory. The file will always be the last file in the folder.

    E.g. I will run a report from a business system which will append the result (flat file) into a network directory. I would then like my program to locate this file (name changes but is always the last file) and scan for values.

    This would mean I wouldnt need to intervene at all. Atm I would need to change the file to text and rename before running the program. Any thoughts on this? is the scan method the same for flat files?

    Thanks very much for your guidance. I think Im slowly starting to remember all of the things I had forgotten.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  2. Replies: 7
    Last Post: 04-10-2010, 12:46 PM
  3. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 12:38 PM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 04:20 PM
  5. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 05:43 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
  •