This discussion is archived
5 Replies Latest reply: May 30, 2009 9:08 PM by 800308 RSS

Loading into String array?

807588 Newbie
Currently Being Moderated
I made a method that loads 2 strings from a text file, and adds them into an array, but it won't work, heres the code.
public static void loadFile() {
     
     for(int i = 0; i < 2; i++) {
          try {
     final Properties testCheck = new Properties();
 
final FileInputStream fis = new FileInputStream("myfile.txt");
testCheck.load(fis);
fis.close();

toTest[i] = testCheck.getProperty("test","Nothing Found");


} catch (IOException ioexception) {
           System.err.println ("Error reading");
        }
     }
} 
Thats the method, I make it print out the array, and it shows up Nothing Found, which means it didn't find what the value in the text file? Any help is greatly appreaciated.
  • 1. Re: Loading into String array?
    camickr Expert
    Currently Being Moderated
    The problem is with the code indenting. If the code isn't properly aligned you can't read it. And if you can't read the code then you can't see what the problem might be.
  • 2. Re: Loading into String array?
    807588 Newbie
    Currently Being Moderated
    I don't see what the problem is yet, but I don't have all the information I would want, either.

    I don't know what's in the text file.

    I don't know how you go about printing what's in the array.

    Incidentally, you might consider moving the instantiation of the FileInputStream and Properties objects outside the loop, along with the loading of the properties object from the file. There's no need to do that multiple times, as near as I can tell, and moving it out makes it easier to figure out what you DO want to loop.

    rc
  • 3. Re: Loading into String array?
    807588 Newbie
    Currently Being Moderated
    OK, whats in the text file is
    test TESTING
  • 4. Re: Loading into String array?
    807588 Newbie
    Currently Being Moderated
    Whatever your problem is, you haven't described it completely.

    Please excuse any typos in the following; my development environment and my internet are on different machines tonight,and I cannot copy-and-paste this over.
    package rc.test;
    import java.io.*;
    import java.util.*;
    
    public class TestLP
    {
      static String[] toTest = new String[2];
      public static void main (String[] arguments)
      { loadFile();
        for (int i=0; i<toTest.length; i++)
        { System.out.println(toTest);
    }
    }

    public static void loadFile()
    {
    for (int i=0; i<2; i++)
    {
    try
    {
    final Properties testCheck = new Properties();
    final FileInputStream fis = new FileInputStream("myfile.txt");
    testCheck.load(fis);
    fis.close();

    toTest[i] = testCheck.getProperty("test", "Nothing Found");
    }
    catch (IOException ioexception)
    { System.err.println("Error reading");
    }
    }
    }
    }
    With the file "myfile.txt" in the directory in which the program is run, and containing the single line "test TESTING", this program prints:
    
    TESTING
    TESTING
    
    All of your code is here, reformatted but otherwise intact; I've added what I surmised to be reasonable drivers to show us whether this code works as expected, and it does.
    
    Possibilities: 
    
    The array you think you're loading the data into is in a different scope than the one loaded.  You do not give us the declaration of that array, so we cannot tell.
    
    The printout of the information is somehow flawed.  You don't show us how you output it, so we cannot tell.
    
    myfile.txt doesn't contain what you expect, or there is more than one copy.  Perhaps "test" is capitalized, which would give the result you indicate.  So would a file that doesn't actually contain "test" as a key.
    
    Part of the point here is that complete information would cost very little compared to what you've already done, and would allow us to help you better.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 5. Re: Loading into String array?
    800308 Newbie
    Currently Being Moderated
    RCook wrote:
    Whatever your problem is, you haven't described it completely.
    Totally Agree.

    Here's my 2-bobs worth:
    package forums;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
     
    public class AutoLoadingProperties extends  java.util.Properties
    {
      private static final long serialVersionUID = 1L;
    
      public AutoLoadingProperties(String filename) throws IOException {
        super.load( new FileInputStream(filename) );
      }
    
      public static void main (String[] args) { 
        try {
          Properties props = new AutoLoadingProperties("AutoLoadingProperties.properties");
          props.list(System.out);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
    }
    ... Of course one has to ask "What's the point?" ... and if I saw this in a production codebase I be awfully tempted to refactor to eliminate it... but there you go... It is what it is, and it ain't what it ain't.

    Cheers. Keith.