Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 List Doesnt Fully Write
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Omnipotence




PostPosted: Mon Nov 27, 2006 7:01 pm   Post subject: List Doesnt Fully Write

I coded this thing to build a list of every file in a folder (Which would be inputed) and it would number all the files and write it to a .txt file. The only trouble is, the list keeps cutting somewhere 5/6ths of the way (guessing estimate) and I can't seem to figure out why. I checked the number of times the for loop went through and even if the program picked out the last item, but I still cant get it to write. Any ideas?

code:

package javaidetest;

import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileWriter;

import javax.swing.*;
import java.awt.*;

public class Main
{
   
    public Main() {}
   
    public static void main(String[] args) throws IOException
    {
        //String Reader
        BufferedReader InStream = new BufferedReader (new InputStreamReader (System.in));
        //File Writer
        BufferedWriter OuStream = new BufferedWriter (new FileWriter ("Full List.txt"));

        //[Input] Destined Directory
        String DirName;
        DirName = InStream.readLine ();
        File DIR = new File (DirName);

        //[List] Full Folder List
        String[] SortingList = DIR.list ();
        int MArrSize = -1;
        int Check = 0;

        for (int indx = 1 ; indx < SortingList.length ; indx++)
        {
            if (indx >= 1000)
            {
                MArrSize = 0;
            }
            else
            {
                if (indx >= 100)
                {
                    MArrSize = 1;
                }
                else
                {
                    if (indx >= 10)
                    {
                    MArrSize = 2;
                    }
                    else
                    {
                    MArrSize = 3;
                    }
                }
            }

            for (int indy = 1 ; indy <= MArrSize ; indy++)
            {
                OuStream.write ("0");
            }

            String ListNo = "" + indx;

            OuStream.write (ListNo);
            OuStream.write (".");
            OuStream.write (" " + SortingList [indx - 1]);
            OuStream.newLine ();
        }
        System.out.println (SortingList.length);
    }   
}


Mod's note: please use code tags.[/i]
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Nov 27, 2006 7:21 pm   Post subject: (No subject)

Let's try formatting this so it looks like Java code. Smile

code:
package javaidetest;

import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileWriter;

import javax.swing.*;
import java.awt.*;

public class Main {
   public static void main(String[] args) throws IOException {
      //String Reader
      BufferedReader inStream = new BufferedReader(
         new InputStreamReader(System.in));
                  
      //File Writer
      BufferedWriter outStream = new BufferedWriter(
         new FileWriter("Full List.txt"));

      //[Input] Destined Directory
      String dirName = inStream.readLine();
      File dir = new File(dirName);

      //[List] Full Folder List
      String[] sortingList = dir.list();
      int mArrSize = -1;
      int check = 0;

      for (int indx = 1 ; indx < sortingList.length ; indx++) {
         if (indx >= 1000) {
            mArrSize = 0;
         }
         else if (indx >= 100) {
            mArrSize = 1;
         }
         else if (indx >= 10) {
            mArrSize = 2;
         }
         else {
            mArrSize = 3;
         }

         for (int indy = 1 ; indy <= mArrSize ; indy++) {
            outStream.write("0");
         }

         String listNo = "" + indx;

         outStream.write(listNo);
         outStream.write(".");
         outStream.write(" " + sortingList[indx - 1]);
         outStream.newLine();
      }
               
      System.out.println(sortingList.length);
   }   
}


Now, we can start looking for semantic problems.
Neville




PostPosted: Fri Dec 01, 2006 10:51 pm   Post subject: (No subject)

The main problem I see is that your counting variable, indx, goes from 1 to ( sortingList.length - 1 ). Since you actually print the array like so: sortingList[indx-1], indx should go all the way to sortingList.length. Therefore...

code:
for (int indx = 1; indx <= sortingList.length; indx++) {
   ...
}


That should get the last file taken care of. If you are missing more than one file, then something else is wrong (check the size of sortingList.length vs. the actual number of files in the directory).
Aziz




PostPosted: Sat Dec 02, 2006 4:38 pm   Post subject: (No subject)

In java (and most other languages) arrays start at 0. To access every value in an array:

code:
int[] someArray = {9, 2, 4, 5, 6, 10, -1};

for (int i = 0; i < someArray.length; i++) {
  someArray[i] = someValue //whatever you want to do
}


also, look up the for each loop.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: