Outputing myOutputString

Can't get data to display correctly

Page 1 of 1

9 Replies - 1467 Views - Last Post: 10 December 2005 - 03:13 PM Rate Topic: -----

#1 indianabryant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-December 05

Outputing myOutputString

Posted 10 December 2005 - 11:23 AM

I'm working on a program that displays 6 catagories of file sizes (in bytes) by 200,000. I got it to work but the last line that displays reads: "1000000-1199999" and I want it to display, "> 1000000. Any thoughts?

BTW, to run the program, simply type java Lab3Part1.java "c:\\Program Files"

Here's my code so far...i think the problem is near line 63, but not sure.
import java.io.File;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Lab3Part1
{
   static int fileCount;
   static int directoryCount;
   private static int histogramSize[];
   private static int histogramDigit[];

	public static void main(String[] args) 
	{
  if (args.length == 0)
  {
         System.out.println("Give me a place to start from!");
         System.exit(1);
  }

      String start = args[0];

      File startLocation = new File(start);

      if (!startLocation.exists())
         {
            System.out.println("Start point doesn't exist: " + start);
            System.exit(1);
         }

      fileCount = 0;
      directoryCount = 0;

      histogramSize = new int[6];
      histogramDigit = new int[10];  

      parseFile(startLocation);

      System.out.println("Here's the results:\n");
      System.out.println("\nThere were "+fileCount+ " files and "
                            +directoryCount+" directories examined ");
  


      //int       histogram[] = { };
      String      myOutputString;
      JTextArea   myJTextArea;
      JScrollPane myJScrollPane;
      myOutputString = "\n";
      
      //for each histogram element, output a bar of the chart
      for (int i = 0; i < histogramSize.length; i++ )
      {
         //output bar label ( "0000000-0199999: ", ..., "0800000-0999999: ", ">1000000        : ")

      {
         histogramSize[1]=0;
      }

      {
         histogramDigit[1]=0;
      }
         if (i > 1000000 )
            myOutputString += "> 1000000";
         else
            myOutputString += String.format( "%07d-%07d: ",
               i * 200000, i * 200000 + 199999 );

         //print bar of asterisks
         int starCount = (int) (50 * ((double) histogramSize[i] / fileCount));
         for (int stars = 0; stars < starCount; stars++ )
            myOutputString += "*";

          myOutputString +="\n"; //start a new line of output
      }//end outer for
     
      myJTextArea = new JTextArea(10,40);
      myJScrollPane = new JScrollPane(myJTextArea);

      myJTextArea.setText(myOutputString);

      JOptionPane.showMessageDialog(null, myJScrollPane,"Lab3Part1 Tasks A",
         JOptionPane.PLAIN_MESSAGE);
      
   }//end of main

   private static void parseFile(File currentLocation)
   {
      String temp;
      int catagory;
      int fileSize;

      if (currentLocation.isDirectory())
        {
           directoryCount++;

           File[] contents = currentLocation.listFiles(); //get List(histogram) of files in the directory 
           if(contents != null) //some files exist to process
              {
                 for (int x=0; x<contents.length; x++ )  //for each entry in directory
                    {
                       if(contents[x].isFile())
                       {
                          fileCount++;
                          fileSize = (int)contents[x].length();
                          catagory = fileSize/200000;
                             if(catagory > 5)
                               {
                                  catagory = 5;
                               }
                             histogramSize[catagory]++;
                             temp = ""+contents[x].length();
                             int digit = (int) (temp.charAt(0) - '0');
                             histogramDigit[digit]++;
                       }
                    else  //directory
                       {
                          parseFile(contents[x]);  //note recursion
                       }
                    }//end of for loop
              }//end of if contents !=null
        }//end of if directory
   }//end of parseFile
}//end of Lab3Part1 class



Is This A Good Question/Topic? 0
  • +

Replies To: Outputing myOutputString

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Outputing myOutputString

Posted 10 December 2005 - 12:02 PM

BTW, to run the program, simply type java Lab3Part1.java "c:\\Program Files"


This would depend on the user both the compiled executable and having a windows based operating system.

I haven't compiled this yet, but could you describe the problem in a bit more detail? Is it just that you are not displaying the information in the manner you wish?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Outputing myOutputString

Posted 10 December 2005 - 12:13 PM

Just of the the top of my head, you have made this declaration
histogramSize = new int[6];


and are then running this loop
for (int i = 0; i < histogramSize.length; i++ )


using i less then the length of the array...which is 6.
This
if (i > 1000000 )
           myOutputString += "> 1000000";


only triggers if i is greater then 1000000, which 6 will never be.
Was This Post Helpful? 0
  • +
  • -

#4 indianabryant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-December 05

Re: Outputing myOutputString

Posted 10 December 2005 - 12:21 PM

I've attached what I'm trying to do. As you can see under the first figure, it should display "> 1000000" at the bottom of the histogram, but the way I have coded it it displays, "1000000 - 1199999". Also, at the bottom of the first figure it displays "there were x files and x directories examined in folder: C:\\Program Files". However, in my code, that is display in the command prompt window, not the java window.

Can't figure this one out.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Outputing myOutputString

Posted 10 December 2005 - 12:27 PM

As I mentioned, your loop is set to run six times...you have an if statement that looks for a condion of i (the loop counter) being greater than 1000000...i is se to go only until six. The condition will not be met.

As for why you're printing in the console instead of the java window, it's because
System.out.println("Here's the results:\n");
     System.out.println("\nThere were "+fileCount+ " files and "
                           +directoryCount+" directories examined ");


are console commands...you need to append those messages to your output string.
Was This Post Helpful? 0
  • +
  • -

#6 indianabryant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-December 05

Re: Outputing myOutputString

Posted 10 December 2005 - 12:34 PM

Amadeus, on 10 Dec, 2005 - 01:24 PM, said:

As I mentioned, your loop is set to run six times...you have an if statement that looks for a condion of i (the loop counter) being greater than 1000000...i is se to go only until six. The condition will not be met.

As for why you're printing in the console instead of the java window, it's because
System.out.println("Here's the results:\n");
     System.out.println("\nThere were "+fileCount+ " files and "
                           +directoryCount+" directories examined ");


are console commands...you need to append those messages to your output string.

Okay, I changed it to :

if (i > 5)
myOutputString += ("> 1000000:");

and i get the same result....I'm I missing something very obvious here...?

lol
Was This Post Helpful? 0
  • +
  • -

#7 indianabryant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-December 05

Re: Outputing myOutputString

Posted 10 December 2005 - 12:40 PM

Okay, i figured it out, i change it to:

i > 4 as i noticed i set int i = 0;

now, how do i go about appending my println to the GUI window?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Outputing myOutputString

Posted 10 December 2005 - 12:41 PM

You begin your loop at zero...that means 5 is actually the sixth spot...since the loop begins at 0 and runs six times, it stops at 5...it will not be greater than 5.
Was This Post Helpful? 0
  • +
  • -

#9 indianabryant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-December 05

Re: Outputing myOutputString

Posted 10 December 2005 - 12:52 PM

indianabryant, on 10 Dec, 2005 - 01:37 PM, said:

Okay, i figured it out, i change it to:

i > 4 as i noticed i set int i = 0;

now, how do i go about appending my println to the GUI window?

You may have missed my last post since you posted right after me. I was wondering how I go about appened my println to my GUI output.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Outputing myOutputString

Posted 10 December 2005 - 03:13 PM

myOutputString = "\nThere were "+fileCount+ " files and "
                           +directoryCount+" directories examined";


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1