Need help in printing a line
Author |
Message |
supahsain08
|
Posted: 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
(how do i post all dir's in one straight line) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: 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
|
Posted: 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
Any other suggestion? |
|
|
|
|
|
HeavenAgain
|
Posted: 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
|
Posted: 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
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
|
|
|
|
|
supahsain08
|
Posted: 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
Thanks again |
|
|
|
|
|
|
|