Java Reference
In-Depth Information
1
// main routine for balanced symbol checker.
2
// If no command line parameters, standard output is used.
3
// Otherwise, files in command line are used.
4
public static void main( String [ ] args )
5
{
6
Balance p;
7
8
if( args.length == 0 )
9
{
10
11
p = new Balance( new InputStreamReader( System.in ) );
12
if( p.checkBalance( ) == 0 )
13
System.out.println( "No errors!" );
14
return;
15
}
16
17
for( int i = 0; i < args.length; i++ )
18
{
19
FileReader f = null;
20
try
21
{
22
f = new FileReader( args[ i ] );
23
24
System.out.println( args[ i ] + ": " );
25
p = new Balance( f );
26
if( p.checkBalance( ) == 0 )
27
System.out.println( " ...no errors!" );
28
}
29
catch( IOException e )
30
{ System.err.println( e + args[ i ] ); }
31
finally
32
{
33
try
34
{ if( f != null ) f.close( ); }
35
catch( IOException e )
36
{ }
37
}
38
}
39
}
figure 11.10
The
main
routine with
command-line
arguments
1 - 2 - 4
^
5 * 3 * 6 / 7
^
2
^
2
would be quite challenging.
If the calculations are performed in integer math (i.e., rounding down on
division), the answer is
-8
. To show this result, we insert parentheses to clarify
ordering of the calculations:
( 1 - 2 ) - ( ( ( ( 4
^
5 ) * 3 ) * 6 ) / ( 7
^
( 2
^
2 ) ) )
Search WWH ::
Custom Search