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

Username:   Password: 
 RegisterRegister   
 Need help in printing a line
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
supahsain08




PostPosted: Sun Mar 30, 2008 6:14 pm   Post subject: Need help in printing a line

hi, is it possible to post a line using coordinates, forexample if i want to post a line at the following coordinates (5,6)
Like I remember in Turing you can do that

For example here is the following code which shows all the directories and files

code:
import java.io.*;
import java.util.*;
import java.text.*;
class Asn62
{
    public static void main (String [] args)
    {
       
        System.out.println ("\t\t\t\tDirectory\n");
            System.out.print ("Name");
            System.out.print ("\t                   Type");
            System.out.print ("\t\tLast Modified");
            System.out.println ("\t\t\tLength");
        String [] Directories;
        String [] Files;
        double [] Modified;
        double [] theLength;
        Files = new String [6] ;
        Directories = new String [6];
        Modified = new double [6];
        theLength = new double [6];
 
 
        File folder = new File ("F:/");
        File [] listOfFiles = folder.listFiles ();
 
        for (int i = 0 ; i < listOfFiles.length ; i++)
 
            {
                if (listOfFiles [i].isFile ())
                {
                    Files [0] = listOfFiles [i].getName ();
 
                                System.out.println (Files [0]);
                }
                else if (listOfFiles [i].isDirectory ())
                {
                    Directories [0] = listOfFiles [i].getName ();
                                System.out.print (Directories [0]);
                                System.out.println("      Dir");
                }
 
                long t = listOfFiles [i].lastModified ();
 
                Modified [0] = t;
 
                long s = listOfFiles [i].length ();
                theLength [0] = s;   
 
            }         
   
}
}


Here is the output
Posted Image, might have been reduced in size. Click Image to view fullscreen.
(how do i post all dir's in one straight line)
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Sun Mar 30, 2008 6:51 pm   Post subject: RE:Need help in printing a line

printf or String.format() or Formatter class, use the "indent" flag
eg.
code:
System.out.printf("%10s%n","hi");
System.out.printf("%10s%n","supahsain08");
supahsain08




PostPosted: Sun Mar 30, 2008 7:08 pm   Post subject: Re: Need help in printing a line

k i replaced this
code:
System.out.println ("Dir");

with this
code:
System.out.printf("%25s%n","Dir");

but still the same output
Posted Image, might have been reduced in size. Click Image to view fullscreen.

Any other suggestion?
HeavenAgain




PostPosted: Sun Mar 30, 2008 7:37 pm   Post subject: RE:Need help in printing a line

code:
System.out.printf("%-25s%s%n","myHomeWork","dir");
System.out.printf("%-25s%s%n","games","dir");
the negative sign cause it to left(?) align text, and by default its right(?) aligned, so we change it and add the extra space we wanted
supahsain08




PostPosted: Sun Mar 30, 2008 7:52 pm   Post subject: Re: Need help in printing a line

Thank you very much

Can you please help me one more time plz
I got that part now i need help with the last modified part, how will i format that correctly

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Here is the code
code:
import java.io.*;
import java.util.*;
import java.text.*;

class Asn62
{
    public static void main (String [] args)
    {
       
        System.out.println ("\t\t\t\tDirectory\n");
            System.out.print ("Name");
            System.out.print ("\t                      Type");
            System.out.print ("\tLast Modified");
            System.out.println ("\t\t\tLength");
            System.out.println ("");
        String [] Directories;
        String [] Files;
        double [] Modified;
        double [] theLength;
        Files = new String [6] ;
        Directories = new String [6];
        Modified = new double [6];
        theLength = new double [6];


        File folder = new File ("F:/");
        File [] listOfFiles = folder.listFiles ();

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

            {
                if (listOfFiles [i].isFile ())
                {
                    Files [0] = listOfFiles [i].getName ();

                                System.out.printf("%-30s%s%n",Files [0],"File");;
                }
                else if (listOfFiles [i].isDirectory ())
                {
                Directories [0] = listOfFiles [i].getName ();
               
               
                System.out.printf("%-30s%s%n",Directories [0],"Dir");
               
               
                               
                               
                }

                long t = listOfFiles [i].lastModified ();

                Modified [0] = t;
                                System.out.println ("\t\t\t\t\t" + Modified [0]);
                long s = listOfFiles [i].length ();
                theLength [0] = s;
         

            }
 
}
}


EDIT: how will i add another another thing in this code, forexample if i want to add another word like "hi" after 35s?
code:
System.out.printf("%-30s%s%n",Files [0],"File");;
HeavenAgain




PostPosted: Sun Mar 30, 2008 8:18 pm   Post subject: RE:Need help in printing a line

welcome Smile
now, i have a question for you
code:
                double [] Modified;               
Modified = new double [6];
                long t = listOfFiles [i].lastModified ();
                Modified [0] = t;
modified is an array of double, length of 6, ok, and now you are only making use of the first element, what's up with that? (unless i missed some part of course Confused)

and now you have
code:

            System.out.println ("\t\t\t\t\t" + Modified [0]);
why cant you just do the same with the dir and file name?

and sorry i dont get your last question, what do you mean by, add hi after 35s? after 35 seconds? what is s Smile
supahsain08




PostPosted: Sun Mar 30, 2008 8:24 pm   Post subject: RE:Need help in printing a line

i think i figured it out

code:
import java.io.*;
import java.util.*;
import java.text.*;

class Asn62
{
    public static void main (String [] args)
    {

        System.out.println ("\t\t\t\tDirectory\n");
        System.out.print ("Name");
        System.out.print ("\t                  Type");
        System.out.print ("\tLast Modified");
        System.out.println ("\t\t       Length (bytes)");
        System.out.println ("");
        String [] Directories;
        String [] Files;
        double [] Modified;
        double [] theLength;
        Files = new String [6];
        Directories = new String [6];
        Modified = new double [6];
        theLength = new double [6];


        File folder = new File ("F:/");
        File [] listOfFiles = folder.listFiles ();

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

            {

                long t = listOfFiles [i].lastModified ();

                Modified [0] = t;

                long s = listOfFiles [i].length ();
                theLength [0] = s;


                if (listOfFiles [i].isFile ())
                {
                    Files [0] = listOfFiles [i].getName ();

                    System.out.printf ("%-26s%s%-3s%s%-3s%s%n", Files [0], "FIL", "", new Date (t), "", theLength [0]);
                }
                else if (listOfFiles [i].isDirectory ())
                {
                    Directories [0] = listOfFiles [i].getName ();

                    System.out.printf ("%-26s%s%-3s%s%-3s%s%n", Directories [0], "DIR", "", new Date (t), "", theLength [0]);

                }

            }

    }
}


Output
Posted Image, might have been reduced in size. Click Image to view fullscreen.


Thanks again Smile
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  [ 7 Posts ]
Jump to:   


Style:  
Search: