Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Do I have the wrong line in my try block?

$
0
0
The program is supposed to read in a data file called fruit.dat and store the contents into a vector. Then the user can click on << or >> buttons on the GUI box that pops up to cycle through the list.
The contents of the data file are as follows:
apple
banana
cherry
melon
orange
strawberry



import java.io.*;
import java.util.Vector;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;

public class choosing_fruit extends JFrame implements ActionListener
{
	private JTextField dataItemTextField;
	private JButton nextButton, previousButton, closeButton;
	private Vector<String> v; 
	private int current = 0; // index value of current item shown in text field
	public static void main(String[] args) throws FileNotFoundException
	{
		choosing_fruit f = new choosing_fruit();
	}
	
	public choosing_fruit()
	{
		v = new Vector<String>();
		nextButton = new JButton(">>");
		previousButton = new JButton("<<");
		closeButton = new JButton("Close");
		dataItemTextField = new JTextField(40);
		setLayout(new FlowLayout());
				
		this.add(previousButton);
		this.add(nextButton);
		this.add(closeButton);
		this.add(dataItemTextField);
		
		nextButton.addActionListener(this);
		previousButton.addActionListener(this);
		closeButton.addActionListener(this);

		try
		{    	
			Scanner in = new Scanner(new File("fruit.dat"));
			String inputLine;
			while(in.hasNextLine()) 
			{
				inputLine = in.nextLine();
				v.add(inputLine);
			}		
		}	
		in.close();	
		catch(Exception e)		
		{		
			System.out.println("Exception thrown  :" + e);	
		}
		
		dataItemTextField.setText(v.get(current)); 
		this.setTitle("Choose Fruit");
		this.pack();
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae)
	{		
		if(ae.getSource() == nextButton) 
		{
			if(current >= 0)
			{
				current = current + 1;
				dataItemTextField.setText(v.get(current));
				
				if(current == 5)
				{	
					nextButton.setEnabled(false);
				}
			}
			else if(current < v.size () -1)
			{
				previousButton.setEnabled(false);
			}
		}
		
		else if(ae.getSource() == previousButton) 
		{
			if(current <= 5)
			{
				current = current - 1;
				dataItemTextField.setText(v.get(current));
					
				if(current == 0)
				{
					previousButton.setEnabled(false);
				} 
			}
			else if(current > v.size () -1);
			{
				nextButton.setEnabled(false);
			}	
		}
	}	
}




Here is the error message when I compile.

choosing_fruit.java:41: error: 'try' without 'catch', 'finally' or resource declarations
try
^
choosing_fruit.java:52: error: 'catch' without 'try'
catch(Exception e)
^
choosing_fruit.java:52: error: ')' expected
catch(Exception e)
^
choosing_fruit.java:52: error: not a statement
catch(Exception e)
^
choosing_fruit.java:52: error: ';' expected
catch(Exception e)
^
5 errors

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>