Results 1 to 11 of 11
  1. #1
    sheetalnri is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default Need a solution to read and store data from a file

    Hii everyone...I want to know how to store the input values which are read from a file. The input values are all double values in a line separated by spaces. I want to read these values one after the other and store in a double typed variable.

    I actually wrote,
    FileReader fr=new FileReader(filename);
    BufferedReader in=new BufferedReader(fr);
    while(true)
    {
    String line=in.readLine();//here i cud read the line

    Please help me.
    Thanks...

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Reading a file

    Hello

    Your code is correct, but it will result in an infinite loop. To stop this, use a condition to break out of the loop. The following code will create a vector of String objects of each line in the file.
    Java Code:
    public static Vector<String> loadFile(String filename){
        Vector<String> strings = new Vector<String>();
        try{
            FileReader file = new FileReader(filename);
            BufferedReader buffer = new BufferedReader(file);
            while (true)
            {
                String line = buffer.readLine();
                if (line == null)
                    break;
                else {
                    strings.add(line); 
                }
            }
            buffer.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        return strings;
    }
    Note that you must always close a file after using it and that the readLine() method will return null if there is no more to read. It is also important to handle any exceptions that may be thrown by the IO classes.
    ;)

    I do not have a IDE on this PC, so my code has been checked by hand.
    Last edited by tim; 10-04-2009 at 02:56 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    sheetalnri is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default

    thanks for ur post...
    but my question is how to store double values of a line of a text file one by one to variables...
    if the first line of input is: 1.0,1.0,90.0
    and second line: 1.0,1.0,0.0
    and third line: ........... and so on

    First I want to read the first line and then I want to store them like
    double a=1.0;
    double b=1.0;
    double c=90.0;
    and then go for the second line and read the data and store them
    and so on....

    thanks again...

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Wink Okay

    Hello

    I have attached a zip file for you. It contains an executable jar file, a "numbers.txt" file and the source code "Main.java". You can use a text editor to view the source code, like "Notepad" if you are using Windows. It can do everything that you asked and more. I like to use Vector data structures and iterators for fast coding. Just ask if you need more help. The program will increment all the numbers in the data file "numbers.txt". To see this:
    1. Open "numbers.txt"
    2. Close "numbers.txt"
    3. Run the jar executable
    4. Reopen "numbers.txt"


    Just ask if you cannot access the code then I will post it.

    Hope this helps.
    Last edited by tim; 10-04-2009 at 03:05 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    Anushaanni is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Question java

    Hello....
    Can any one please help me with this problem
    how to store double values of a line of a text file one by one to variables...
    if the first line of input is: 1.0,2.0
    1.90,2.45....
    and second line: 1.0,1.0
    and third line: ........... and so on

    First I want to read the first line and then I want to store them like
    double a=1.0;
    double b=2.0;
    double c=1.90;
    and then go for the second line and read the data and store them
    and so on....

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hello... hello... hello....

    There seems to be an echo in this posted thread ...

    Anushaanni... look in the above posts...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    peotoy is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default cannot open example.zip

    can some pls re-upload the example.zip as the winzip is not opening it .

    thanks

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, seeing as how it's all null bytes, no wonder. Hopefully it was meant to be a joke on you, if not, then "shame on you, tim". (For attempting to post it at all, that does not help.)

    See the API docs for Double (the parse/valueOf methods) and String (the split method), and Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O)

  9. #9
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi masijade
    Quote Originally Posted by masijade View Post
    Hopefully it was meant to be a joke on you, if not, then "shame on you, tim". (For attempting to post it at all, that does not help.)
    It seems that the file was corrupted while I uploaded it. And no, I don't make fun of others. I deleted the corrupted zip file. :(

    I'm sorry about that sheetalnri. :o
    Last edited by tim; 10-04-2009 at 03:13 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  10. #10
    yemyem is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi, I have the exact same problem but i cant see see how to get hold of your zip file tim.

    thanks

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by yemyem View Post
    Hi, I have the exact same problem but i cant see see how to get hold of your zip file tim.

    thanks
    Well good for you. Another cheating attempt thwarted.

    Exactly what are you having a problem with? Post your code, any and all compiler/exception messages, and a complete (but brief) description of expected results vs. actual results. Then we will help you correct your code.

    If you don't figure out how to do this assignment (and copying someone else's code is not solving it yourself) then you'll have an even harder time with your next assignment which will probably build of the principles learned in this assignment. Which, of course, you won't have learned if you simply copy someone else's code.

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  2. Replies: 3
    Last Post: 04-07-2008, 06:02 PM
  3. How to store property file into key value pair
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-05-2008, 10:16 AM
  4. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 09:45 PM
  5. how to use btree to store data
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:52 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
  •