Here is an example program that prints a table of powers of two.
import java.io.*;
class PowerTable
{
public static void main ( String[] args )
{
// Get filename and create the file
FileWriter writer = null;
Scanner scan = new Scanner( System.in );
String fileName = "";
System.out.print("Enter Filename-->");
try
{
fileName = scan.next();
// create the PrintWriter and enable automatic flushing
out = new PrintWriter( new BufferedWriter( new FileWriter( fileName )), true );
}
catch ( IOException iox )
{
System.out.println("Error in creating file");
return;
}
// Write out the table.
int value = 1;
out.println( "Power\tValue" );
for ( int pow=0; pow<=20; pow++ )
{
out.print ( pow );
out.print ( '\t' );
out.println( value );
value = value*2;
}
out.close();
}
}
The character '\t' is the tab character.
Instead of using three statements in a row, would the following have worked as well?
out.println( pow + "\t" + value );