3 Replies Latest reply: Apr 17, 2007 11:13 PM by 796125 RSS

    Need help with adding to String members[];

    807599
      Alright I am really confused about this now. lol.

      Here is what I am wanting:

      I need to get information from a txt file online, by rows, would look like this:

      iy1175696721
      yx1176164740
      xm1176351702

      I know I can do

      String members[] = {"iy1175696721", "yx1176164740", "xm1176351702"};

      But I want it to get that information from a txt file...

      Here is what I found so far

                URL memberList = new URL("http://www.example.com/text.txt");
                URLConnection mc = memberList.openConnection();
                
                BufferedReader in = new BufferedReader(
                                      new InputStreamReader(
                                      mc.getInputStream()));
                                      
              String inputLine;
              
              while ((inputLine = in.readLine()) != null) 
               System.out.println(inputLine);
        • 1. Re: Need help with adding to String members[];
          807599
          OK. What part are you confused about?

          Do you really intend to get the data from a text file from a web server, or do you want to read the file from the local disk?
          • 2. Re: Need help with adding to String members[];
            807599
            If you don't know the length of the array until after parsing the entire file, you can try creating a list first, and then once you know the final size of the list, you will be able to create the array.

            If your file is not on the internet, your code will look a little different than the example code you have so far.

            I'm not exactly sure what the question is, so maybe one of those two pieces of advice helped?
            • 3. Re: Need help with adding to String members[];
              796125
              Alright I am really confused about this now. lol.

              Here is what I am wanting:

              I need to get information from a txt file online, by
              rows, would look like this:

              iy1175696721
              yx1176164740
              xm1176351702

              I know I can do

              String members[] = {"iy1175696721", "yx1176164740",
              "xm1176351702"};

              But I want it to get that information from a txt
              file...

              Here is what I found so far

              URL memberList = new
              ew URL("http://www.example.com/text.txt");
                        URLConnection mc = memberList.openConnection();
                        
                        BufferedReader in = new BufferedReader(
              new
              InputStreamReader(
              
              c.getInputStream()));
              
              
              while ((inputLine = in.readLine()) != null) 
              System.out.println(inputLine);
              de]
              Okay, so if I had to take a guess at this, your problem is that you can't create the array before reading the file, because you don't know how many lines are in the file?

              You're best bet (unless the file is rediculously large) is to use an ArrayList so you can add the lines to the list one at a time, and the array list will take care of worrying about the size of the internal array.

              - Adam