Results 1 to 4 of 4
  1. #1
    DaFlake is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default Arrays and File Reading

    It is me again and I can once again use a little help. My little learning project is going along very well but I am once again stuck....

    What I want to do now is create an array and use items from a txt file to fill the array.

    If I remember correctly, I need to create a FileInputStream linking to the file and then use BufferedReader to read it in... This is kind of what I did.

    Java Code:
    try {
         FileInputStream interestIn = new FileInputStream("interest.rtf);
         BufferedReader interestOut = new BufferedReader(new InputStreamReader(interestIn));
    
    for (int j; j < interestRateArray.length; j++) {
    
         interestRateArray[j] = interestOut.readLine();
         }
    
    interestOut.close();
    I can't cut and paste right now as I am coding on one computer (not connected to the network) and posting from one that is. (Long Story)....

    Basically what I am trying to do is take an array that I had created manually in the code and make it go out and pull the values from a file into the array.

    Should I create new method for this?

    Like before, I also don't want to post the code up as this is an active universites project (UoP) that I am playing with for the fun of it. Any help is appreciated and if you could point me in the right direction please let me know.

  2. #2
    DaFlake is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default

    OK, to add to the above, I managed to get my code cleaned up with no errors.

    :)

    What I am getting now is that the file can't be found when I run that application. I have tried the total path and the local path with no luck. I am on a Mac, so I wonder if there is something that I am missing.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    catch the "FileNotFoundException

    1.Either give the above exception in try-catch or use throws

    2.Check whether file exist on the same directory where ur java class is located because u have not specified the path and directly giving the file.

    3.If u give path put "/" for location not "\"
    Ramya:cool:

  4. #4
    DaFlake is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks!

    I think that I got that figured out but I am now getting another error instead of the file not found one.

    Basically I think that it is trying to read the file but something is happeneing. I created an RTF file in my directory and I think that it is now finding it as I am getting a different error.

    Can't open input server /Library/InputManagers/MultiClutchInputManager.bundle

    It looks like something about Floating Decimal....


    I did get rid of the try and catch and declared the exception and the beginning of the method.

    Here is what I did...

    Java Code:
    String [] interestRateArray = new String [3];
    
    FileInputStream interestIn = new FileInputStream("/Users/Will/.../interest.rtf");
    BufferedReader interestOut = new BufferedReader(new InputStreamReader(interestIn));
    
    for (int j; j < interestRateArray.length; j++) {
    
         interestRateArray[j] = interestOut.readLine();
         }
    
    interestOut.close();
    My "interest" file contains three decimal numbers seperated by a return.

    My question is, will this work or am I off base here? I don't get any compiling errors but it does crash.

    Just so you know, after I load up the array, I am pulling the number from the array position and then using parse to get it to a double so that I can use it.

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  2. Input data from file to arrays problem
    By PVL268 in forum New To Java
    Replies: 18
    Last Post: 03-16-2009, 05:05 AM
  3. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM
  4. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM
  5. Problem with Sequential File and Arrays
    By rhivka in forum New To Java
    Replies: 8
    Last Post: 07-30-2007, 09:03 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
  •