Page 1 of 2 12 LastLast
Results 1 to 16 of 17

Thread: Java file reading and Stroing in array

  1. #1
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts

    Java file reading and Stroing in array

    OK, I currently have the follwoing:

    The code should read from the file stock.txt and put the info into the array in the prog (arrays already declared etc) however i just get nullpointer errors and non displaying of the data in the file.

    Could anyone offer any help i.e. have I made any really obvious mistakes?


    File stockFile = new File("stock.txt");

    if (!stockFile.exists())
    {
    return;
    }

    BufferedReader input=
    new BufferedReader(
    new FileReader(stockFile));

    int count=1;

    stockCode[count] = input.readLine();
    stockDesc[count] = input.readLine();
    String strCurrentLevel = input.readLine();
    currentLevel[count] = Integer.parseInt(strCurrentLevel);
    String strReorderLevel = input.readLine();
    reorderLevel[count] = Integer.parseInt(strReorderLevel);
    String strPrice = input.readLine();
    price[count] = Float.parseFloat(strPrice);

    while (stockCode!=null)
    {
    System.out.print("\t"+stockCode[count]);
    System.out.print("\t\t"+stockDesc[count]);
    System.out.print("\t\t"+currentLevel[count]);
    System.out.print("\t\t"+reorderLevel[count]);
    System.out.print("\t\t"+price[count]);
    count++;
    stockCode[count] = input.readLine();
    stockDesc[count] = input.readLine();
    strCurrentLevel = input.readLine();
    currentLevel[count] = Integer.parseInt(strCurrentLevel);
    strReorderLevel = input.readLine();
    reorderLevel[count] = Integer.parseInt(strReorderLevel);
    strPrice = input.readLine();
    price[count] = Float.parseFloat(strPrice);


    }
    input.close();

    Thanks in advance

  2. #2
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Can you post the textfile as well?
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  3. #3
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Just looking at some code for one of our products, this is how we do it:

    Code:
    FileInputStream fis = new FileInputStream("file.txt");
    BufferedReader myInput = new BufferedReader(new InputStreamReader(fis));
    String thisLine = "";
    
    while ((thisLine = myInput.readLine()) != null) {
        System.out.println(thisLine);
    }
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  4. #4
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Stock Code
    Stock Description
    500
    540
    29

    That's what's in the text file, I'll give your suggestion above a go and I'll let you know how I get on.

    Thanks muchly

  5. #5
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Quote Originally Posted by DaBeeeenster
    Just looking at some code for one of our products, this is how we do it:

    Code:
    FileInputStream fis = new FileInputStream("file.txt");
    BufferedReader myInput = new BufferedReader(new InputStreamReader(fis));
    String thisLine = "";
    
    while ((thisLine = myInput.readLine()) != null) {
        System.out.println(thisLine);
    }
    hmm yeah that works for one variable, but I need to cycle through all of my variables putting them into their array each time, how would i modify this code to do that?

  6. #6
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    Hi,

    Firstly, what code are you using to initialise the array?

    Secondly, remember that array indexes start at 0 (not 1).

    Thirdly, the expression

    while (stockCode!=null)

    will always be true if you have initialised the array properly and this loop will just run and run.

    Not sure exactly what you are trying to achieve here, but if its just read the contents of a file, store the values and print them out then I would read the entire file in first then iterate over your data structure and print things out seperately - keep it simple.

    Reading the contents of a file should be done using something like

    while ((c = in.read()) != -1)

    This will terminate the loop once it encounters end of file.

    Array iteration is best done using an expression such as

    for (int i=0; i<array.length; i++)

    Best practice code for file I/O this can be found here -
    http://java.sun.com/docs/books/tutor...lestreams.html

    hope this helps,
    dave

  7. #7
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Quote Originally Posted by wilkied

    while ((c = in.read()) != -1)
    Yeah I tried that , but only got it to work with one variable, I need to store all of my variables into their arrays how would I go about this, could i just simply use && in the above line specifiing each new line from teh file to go to the relevant array space?

    Thanks for your help

  8. #8
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    k.. do you want a solution ? Assuming that you want it implemented the same way you tried it. Without my petty wanting each record to be an object, and simply having an array of these objects. This is how you would do it...

    This reads in a text file filling the arrays. You call the program like this...
    java HexusProb <filename>. If you do not specify a file. it will crash.
    Then it prints out the arrays to the terminal console.
    --------------------------------------------------------------------
    import java.io.* ;

    public class HexusProb {
    /*Declare arrays, ten is arbitrary value */
    static String[] StockCode = new String[10] ;
    static String[] StockDesc = new String[10] ;
    static int[] CurrentLevel = new int[10] ;
    static int[] ReorderLevel = new int[10] ;
    static float[] Price = new float[10] ;

    public static void main(String[] args) {

    try {

    BufferedReader fileIn =
    new BufferedReader(new FileReader(args[0]));

    int count = 0 ;

    String in = fileIn.readLine() ;
    while(in!=null) {

    StockCode[count] = in ;
    StockDesc[count] = fileIn.readLine() ;
    CurrentLevel[count] = Integer.parseInt(fileIn.readLine()) ;
    ReorderLevel[count] = Integer.parseInt(fileIn.readLine()) ;
    Price[count] = Float.parseFloat(fileIn.readLine()) ;
    in = fileIn.readLine() ;
    count++ ; /* increment counter */
    } /* End of while */

    } catch(IOException ioe) {
    ioe.printStackTrace() ;
    }

    displayRecords() ;
    System.exit(0) ;

    } /* End of main */

    /* This prints out the arrays to the console */
    public static void displayRecords() {

    for (int i = 0 ; i <= (StockCode.length-1) ; i++) {

    if(StockCode[i]==null)
    return ; /* end of useful data */

    System.out.print("\t"+StockCode[i]);
    System.out.print("\t\t"+StockDesc[i]);
    System.out.print("\t\t"+CurrentLevel[i]);
    System.out.print("\t\t"+ReorderLevel[i]);
    System.out.print("\t\t"+Price[i]);

    }

    }
    }

    ----------------------------------------------
    I may have got a little excited by the fact there was finally a Java problem !
    Last edited by killgORE; 05-04-2004 at 02:05 AM.

  9. #9
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    thanks muchly for your help

  10. #10
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    hmm just tried that, it reads the first line fine but not the second, third etc etc.

    here's the two necessary files, it compiles fine just has trouble when in the prog itself, i.e. it can't do certain things like find the second item succesfully or extend/increase stock.

    Take a looksy at these ....

    http://www.jmbradley.net/StockControl.java
    http://www.jmbradley.net/stock.txt

  11. #11
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Ahh finally all sorted

  12. #12
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    What was the prob mate ? It worked fine for me ! using a textfile of the data you posted earlier.

    Glad to hear its sorted though.

  13. #13
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    it was a combination of things really, a bit of tidying up later and it works fine

    Thanks for all your help

  14. #14
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    Incidently. I have been looking for some posts to let me help with java problems. This helps me... as I have to think a bit while I fix problems, and helps the poster. So if there are anymore problems then just make a post in this programming section. Not that I am some sort of uber Java guru. But I am trying real hard to be !

  15. #15
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by killgORE
    Incidently. I have been looking for some posts to let me help with java problems. This helps me... as I have to think a bit while I fix problems, and helps the poster. So if there are anymore problems then just make a post in this programming section. Not that I am some sort of uber Java guru. But I am trying real hard to be !
    I think the best approach for helping people to learn is to point them in the right direction rather than giving them a complete solution.

    This way people have to actually think about what they are doing wrong and work towards finding and understanding the right way to do it.

    cheers,
    dave

  16. #16
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    Hmmmm. I take this onboard, and this is the way I teach people when I am sitting in the same room as them. I figure out what is wrong... then ask them little questions till they find the problem themself - as this is best. But this is a little time consuming through a web forum don't you think ? Suppose there is always good old ICQ...

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •