Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why doesnt this work?

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
US
I am using java 1.3 on AIX 5.1. This doesnt work. It gives me errors about having catch with no try?????



import java.awt.*;
import java.io.*;

public class FileViewer extends Frame {
Button close;
// Query the size of the specified file, create an array of bytes big
// enough, and read it in. Then create a TextArea to display the text
// and a "Close" button to pop the window down.
public FileViewer(String filename) throws IOException {
super("FileViewer: " + filename);
File f = new File(filename);
int size = (int) f.length();
int bytes_read = 0;
FileInputStream in = new FileInputStream(f);
byte[] data = new byte[size];
while(bytes_read < size)
bytes_read += in.read(data, bytes_read, size-bytes_read);

TextArea textarea = new TextArea(new String(data, 0), 24, 80);
textarea.setFont(new Font(&quot;Helvetica&quot;, Font.PLAIN, 12));
textarea.setEditable(false);
this.add(&quot;Center&quot;, textarea);

close = new Button(&quot;Close&quot;);
this.add(&quot;South&quot;, close);
this.pack();
this.show();
}

// Handle the close button by popping this window down
public boolean action(Event e, Object what) {
if (e.target == close) {
this.hide();
this.dispose();
return true;
}
return false;
}

// The FileViewer can be used by other classes, or it can be
// used standalone with this main() method.
static public void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println(&quot;Usage: java FileViewer <filename>&quot;);
System.exit(0);
}
// try {
// Frame f = new FileViewer(args[0]);
// catch (IOException e) System.out.println(e);
// }
}

}


with errors:


FileViewer.java:54: 'catch' without 'try'
catch (IOException e) System.out.println(e);
^
FileViewer.java:52: 'try' without 'catch' or 'finally'
try {
^
Note: FileViewer.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors





 
You never end your try block or begin and end your catch. The try and catch blocks are not like control structures, regardless how many statements are in each you must use { and }. Furthermore, the catch must fall OUTSIDE the try.

Your Code(incorrect):
Code:
try {
  Frame f = new FileViewer(args[0]);
  catch (IOException e) System.out.println(e);
}

Correct Code:
Code:
try {
  Frame f = new FileViewer(args[0]);
}
catch (IOException e) {
  e.printStackTrace();
}
 
Thank you, but it still gives me the errors:

FileViewer.java:61: 'class' or 'interface' expected
}
^
FileViewer.java:63: 'class' or 'interface' expected

^
Note: FileViewer.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors

any suggestions?


Jeremy
 
First off, what lines are 61 and 63. Secondly, you never implement a Listener so you can't possibly handle a close request. Third, you declare main as throwing an IOException, yet you catch all IOExceptions. You do not need to do both, one or the other will suffice.
 
this is an example i got off of OReillys website. i am really not sure why it doesnt work. any suggestions to make it work?

Jeremy
 
Here's some ammended source for you that does the trick :

import java.awt.*;
import java.awt.event.*; //Added
import java.io.*;

public class FileViewer extends Frame
{

// Member Variables =====================
Button close;

// Constructor ==========================
// Query the size of the specified file, create an array of bytes big
// enough, and read it in. Then create a TextArea to display the text
// and a &quot;Close&quot; button to pop the window down.
public FileViewer(String filename) throws IOException
{
super(&quot;FileViewer: &quot; + filename);
File f = new File(filename);
int size = (int) f.length();
int bytes_read = 0;
FileInputStream in = new FileInputStream(f);
byte[] data = new byte[size];
while(bytes_read < size)
{
bytes_read += in.read(data, bytes_read, size-bytes_read);
}

//TextArea textarea = new TextArea(new String(data, 0), 24, 80);
// Note : String(byte[] ascii, int hibyte) : Deprecated.
TextArea textarea = new TextArea(new String(data), 24, 80);
textarea.setFont(new Font(&quot;Helvetica&quot;, Font.PLAIN, 12));
textarea.setEditable(false);
this.add(&quot;Center&quot;, textarea);

close = new Button(&quot;Close&quot;);
this.add(&quot;South&quot;, close);

//Add Listeners...(will just use anonymous inner classes here)
//[1] WindowClosing
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) { exit(0); }
});
//[2] Button action.
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { exit(0); }
});

this.pack();
this.show();
}

//exit() ======================================
private void exit(int returnCode)
{
setVisible(false); //not strictly req - its a style thing.
System.exit(returnCode);
}

// Handle the close button by popping this window down
/* Deprecated. As of JDK version 1.1, should register this component as
* ActionListener on component which fires action events
public boolean action(Event e, Object what)
{
if (e.target == close)
{
this.hide();
this.dispose();
return true;
}
return false;
}
*/

// M A I N ======================================
// The FileViewer can be used by other classes, or it can be
// used standalone with this main() method.
static public void main(String[] args)
{
if (args.length != 1)
{
System.out.println(&quot;Usage: java FileViewer <filename>&quot;);
System.exit(0);
}
try
{
Frame f = new FileViewer(args[0]);
}
catch (IOException e)
{
//System.out.println(e);
e.printStackTrace();
}
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top