Results 1 to 4 of 4
Thread: Try Catch Block
- 05-06-2012, 01:59 AM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Try Catch Block
My teacher for our last project is asking to add a try catch block for the text fields that will catch the error if nothing is entered into the code I was wondering if you guys have any suggestions on how to do this? Any help is welcome and thank you in advance.
Java Code:package accounting; import IO.ReadText; import IO.WriteText; import java.awt.GridLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Accounting extends JFrame implements ActionListener{ JPanel instructions,enter,click, fields, buttons, end,main; JButton create, print, exit, printfilebutton; JLabel instructionText, accountText, depositText; JTextField tfaccount, tfdeposit; private LayoutManager newGridLayout; private JLabel instruction2Text; private int i, acctIndex = 0; private Accounts accounts = new Accounts(); private String recordText; public Accounting() { createPanels(); createButtons(); createLabels(); createTextFields(); setListeners(); buildPanels(); createGUI(); } private void createButtons() { create = new JButton("Create"); print = new JButton("Print"); exit = new JButton("Exit"); printfilebutton = new JButton("Print File"); } private void createPanels() { instructions = new JPanel(); fields = new JPanel(); buttons = new JPanel(); end = new JPanel(); main = new JPanel(); } private void createTextFields() { tfaccount = new JTextField("Enter Account Name", 15); tfdeposit = new JTextField("Enter Initial Deposit", 15); } private void setListeners(){ create.addActionListener(this); print.addActionListener(this); exit.addActionListener(this); printfilebutton.addActionListener(this); } private void createGUI(){ add(main); main.setLayout (new GridLayout( 6,3)); main.add(instructions); main.add(fields); main.add(buttons); main.add(end); setVisible(true); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void createLabels() { instructionText = new JLabel("Instructions:"); instruction2Text = new JLabel("Enter the account name & the initial deposit."); accountText = new JLabel(); depositText = new JLabel(); } /** * @param args the command line arguments */ public static void main(String[] args) { Accounting accounting = new Accounting(); } private void buildPanels() { instructions.add(instructionText); instructions.add(instruction2Text); end.add(printfilebutton); fields.add(tfaccount); fields.add(tfdeposit); buttons.add(create); buttons.add(print); end.add(exit); } @Override public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Exit")){ int choice = JOptionPane.showConfirmDialog(null, "Do you want Close?", "Confirm Close", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION){ System.exit(0); } } if (e.getActionCommand().equals("Print")){ for(int i = 0; i < accounts.accountNames.length; i++){ if (accounts.accountNames[i] == null) break; else accounts.printBalance(i); } } if (e.getActionCommand().equals("Create")) { accounts.setAccountName(tfaccount.getText(), Integer.toString(acctIndex)); accounts.makeDeposit(Double.parseDouble(tfdeposit.getText()), acctIndex); accounts.printBalance(acctIndex); recordText = tfaccount.getText() + "/" + tfdeposit.getText(); WriteText writeText = new WriteText(recordText); acctIndex++; } if (e.getActionCommand().equals("Print File")) { for (int i = 0; i < acctIndex; i++) { accounts.printBalance(i); ReadText reader = new ReadText(); } } } }
- 05-06-2012, 02:33 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Try Catch Block
There doesn't appear to be any try/catch block in that code.
Read your textbook or the numerous online tutorials and write the blocks that will catch errors and do something with them. Someone may - but I think it's a bit much to expect them to - essentially recreate what is already (and more thoughtfully) written in those sources. Moreover the value of an exercise like this is for you to translate the general ideas and concepts into working code: and, clearly no-one can do this for you without robbing you of the opportunity to benefit from the exercise.
If you get stuck - if you cannot understand what you have read, or how to apply it - ask about that. That is, say what you have read, and what you have tried and what happened. Post your code. But, please, keep it brief: just enough to illustrate the problem you are having and without dependencies like the IO package which others won't have.
- 05-06-2012, 06:07 AM #3
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Re: Try Catch Block
Well this is what I have for the other code. this code catches what is in the text box then makes a file and stores the information in c:/temp/file.txt. These are the try catch blocks that I have currently. I just need the one of catching the error of nothing in the text boxes.
This reads the text
This one writes the text into the fileJava Code:package IO; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReadText { private String line; public ReadText() { try { try (BufferedReader reader = new BufferedReader(new FileReader("c:/temp/file.txt"))) { line = reader.readLine(); while (line != null) { System.out.println("Reading from file"); System.out.println(line); line = reader.readLine(); } } } catch (FileNotFoundException e) { System.out.println("File not found at c:/temp/file.txt"); } catch (IOException e) { System.out.println("Something went wrong with reading or closing the file"); } } }
Java Code:package IO; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class WriteText { public WriteText(String recordText) { try { try (PrintWriter writer = new PrintWriter(new FileWriter("c:/temp/file.txt", true))) { writer.println(recordText); System.out.println("Writing to file"); } } catch (IOException e) { System.out.println("File not created or written correctly"); } } }
- 05-06-2012, 07:00 AM #4
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
try catch block issue
By srivempala in forum Advanced JavaReplies: 29Last Post: 07-22-2011, 06:45 PM -
Try/catch block
By swati.jyoti in forum New To JavaReplies: 5Last Post: 07-02-2009, 02:32 PM -
Question reg try/catch block
By nn12 in forum New To JavaReplies: 1Last Post: 09-16-2008, 05:56 PM -
Try Catch block issues
By kewlgeye in forum New To JavaReplies: 11Last Post: 04-29-2008, 07:10 AM -
try...catch block
By javaplus in forum New To JavaReplies: 3Last Post: 11-06-2007, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks