Results 1 to 4 of 4

Thread: Try Catch Block

  1. #1
    Jackbower is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default 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();
                }
              
    
        }
        
    
            } 
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

  3. #3
    Jackbower is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default 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
    Java 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");
            }
        
        }
        
    }
    This one writes the text into 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");
            }
        }
    }

  4. #4
    Jackbower is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Try Catch Block

    Never mind i was able to figure it out finally =) Thank you for the responses.

Similar Threads

  1. try catch block issue
    By srivempala in forum Advanced Java
    Replies: 29
    Last Post: 07-22-2011, 07:45 PM
  2. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 03:32 PM
  3. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 06:56 PM
  4. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 08:10 AM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •