3 Replies - 401 Views - Last Post: 10 October 2012 - 02:53 PM Rate Topic: -----

#1 tarv12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-September 12

Problem with errors in program

Posted 09 October 2012 - 09:53 PM

I am having difficulty with numerous errors in the program. I am not even sure if I am doing anything correct in the program right now. Can someone help me identify what I am messing up. I was doing fairly well in my last Java class. This class has introduced GUI into the mix and that is throwing me off. I am trying to create a program that will allow a user to enter a series of three contact's names, ages, email addresses, and cell phone numbers. Then I need to display those three contacts into a JFrame individually or together. I am not getting this done right now and I need help figuring out how to get through the errors.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contact.information;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mark's
 */
public class myContactInformation extends javax.swing.JFrame {

    /**
     * Creates new form myContactInformation
     */
    public myContactInformation() {
        
    }
private JTextField CName = new JTextField(20);    
        private JTextField CAge = new JTextField(10);    
        private JTextField CEmailAddress = new JTextField(10);    
        private JTextField CCellNumber = new JTextField(10);         
        private JButton enterBtn = new JButton("Enter");   
        private JButton clearBtn = new JButton("Clear");        
        private JButton saveBtn = new JButton("Save");    
        private JButton exitBtn = new JButton("Exit");    
        private DefaultTableModel tableModel;             
        int contactCount = 0;         
        
        public Contacts()     
        {        
            super("Contact Entry");
        }        
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
        setVisible(true);        
        setResizable(false);                 
        // labels for everything        
        JPanel inputSectionPanel = new JPanel();        
        JLabel nameLabel = new JLabel("Name");        
        JLabel ageLabel = new JLabel("Age");        
        JLabel emailLabel = new JLabel("Email Address");        
        JLabel numberLabel = new JLabel ("Cell Number");                         
        inputSectionPanel.add(nameLabel);        
        inputSectionPanel.add(CName);                 
        inputSectionPanel.add(ageLabel);        
        inputSectionPanel.add(CAge);                 
        inputSectionPanel.add(emailLabel);        
        inputSectionPanel.add(CEmailAddress);                
        inputSectionPanel.add(numberLabel);        
        inputSectionPanel.add(CCellNumber);                         
        //place for buttons         
        JPanel buttonPanel = new JPanel();        
        JTable contactTable = new JTable();         
        enterBtn.addActionListener(this);        
        clearBtn.addActionListener(this);        
        exitBtn.addActionListener(this);               
        saveBtn.addActionListener(this);       
        buttonPanel.add(enterBtn);        
        buttonPanel.add(clearBtn);                
        buttonPanel.add(saveBtn);        
        buttonPanel.add(exitBtn);         
        // table layout        
        Container tableButtonContainer = getContentPane();        
        tableButtonContainer.setLayout(new BorderLayout());        
        tableButtonContainer.setLayout(new BoxLayout(tableButtonContainer, BoxLayout.Y_AXIS));       
        tableButtonContainer.add(inputSectionPanel, BorderLayout.WEST);        
        // table with 4 columns        
        tableModel = new DefaultTableModel(0,4);                         
        // Name columns       
        String col[] =  {  "Name", "Age", "Email Address", "Cell Number"  };        
        tableModel.setColumnIdentifiers(col);        
        contactTable.setModel(tableModel);                                  
        // create pane for table        
        JScrollPane tableScroller = new JScrollPane(contactTable, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);         
        tableScroller.setViewportView(contactTable);        
        tableButtonContainer.add(contactTable, BorderLayout.CENTER);        
        tableButtonContainer.add(buttonPanel, BorderLayout.SOUTH);        
        tableButtonContainer.add(tableScroller);        
        tableScroller.setViewportView(contactTable);        
        pack();     
} 
//buttons actions      
public void actionPerformed(ActionEvent event)    
{        
JButton source = (JButton) event.getSource();
}        if (source == enterBtn)         
{            
enterContact();//-----------------------------------------------        }         
else if (source == clearBtn)         
{           
clearValues();        
}                 
else if (source == saveBtn)                
{                        
saveAction();                
}            
else if (source == exitBtn)         
{            
quit();        
}    
} //Exception handling on all entries    
void enterContact()     
{                     
// test contact        
String contact = CName.getText();        
if (contact == null || contact.equals(""))         
{            
JOptionPane.showMessageDialog(null, "Please enter a validname", "Error", JOptionPane.ERROR_MESSAGE);            
return;        
}                         
//test age betweeen 0 and 120                         
String age = CAge.getText();                         
Integer.parseInt(age);                    
if (Integer.parseInt(age) > 0 && Integer.parseInt(age) < 121);                    
else {                  
System.err.println("Please enter a valid age between 0 and 120");                  
return;                
}                                                
// test email                         
String email = CEmailAddress.getText();        
if ( email == null || email.equals(""))                
{                        
JOptionPane.showMessageDialog(null, "Please Enter a valid Email Address", "Error", JOptionPane.ERROR_MESSAGE);
return;                
}                     
//test cell number                                 
String cell = CCellNumber.getText();                
try                
{                    
Integer.parseInt(cell);                        
}                      
catch(Exception e) 
{                          
System.out.println("Please enter a valid numeric phone number");                          
return;        
}                 
//determine row count and add values from text fields        
tableModel.setRowCount(contactCount + 1);        
tableModel.setValueAt(contact, contactCount, 0);        
tableModel.setValueAt(age, contactCount, 1);        
tableModel.setValueAt(email, contactCount, 2);               
tableModel.setValueAt(cell, contactCount, 3);        
contactCount++;     
} 
//Clear values     
void clearValues()     
{        
CName.setText(null);        
CAge.setText(null);        
CEmailAddress.setText(null);                
CCellNumber.setText(null);    
} 
//exit application     
void quit()     
{        
System.exit(0);    
}     
public static void main(String args[])     
{        
java.awt.EventQueue.invokeLater(new Runnable()         
{            
public void run()             
{                
new Contacts().setVisible(true);            
}        
});   
}                
public void saveAction(){             
File file = new File("data.dat");             
try{                       
FileWriter data = new FileWriter(file);         
for(int i = 0; i < tableModel.getColumnCount(); i++){            
data.write(tableModel.getColumnName(i) + "\t");        
}                
data.write("\r\n");         
for(int i=0; i< tableModel.getRowCount(); i++) 
{            
for(int j=0; j < tableModel.getColumnCount(); j++) 
{                
data.write(tableModel.getValueAt(i,j).toString()+"\t");            
}            
data.write("\r\n");        
}         
data.close();     
}
catch(IOException e){ System.out.println(e); 
}
}    
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(myContactInformation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(myContactInformation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(myContactInformation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(myContactInformation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new myContactInformation().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contact.information;


/**
 * Contact Information
 * Mark Tarvainen
 * PRG 421
 * October 9, 2012
 * Ganesh Shanmuganathan
 */
public class ContactInformation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
    }
}



This post has been edited by pbl: 10 October 2012 - 01:12 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with errors in program

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Problem with errors in program

Posted 10 October 2012 - 04:26 AM

Post the errors you need help with, copied and pasted from what you're getting at your end.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with errors in program

Posted 10 October 2012 - 01:13 PM

Post your errors. Why should we cut & paste your code and compile it ourselve ?
Was This Post Helpful? 0
  • +
  • -

#4 tarv12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-September 12

Re: Problem with errors in program

Posted 10 October 2012 - 02:53 PM

I do not want you to do my work for me. I just want help with some of the errors to start. I posted this message before I went to sleep, and I have been at work all day. Here is the start to my errors. I think that I can figure out quite a bit of it as soon as I can get through the beginning errors.
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:41: error: invalid method declaration; return type required
public Contacts()
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:45: error: invalid method declaration; return type required
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:45: error: <identifier> expected
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:46: error: invalid method declaration; return type required
setVisible(true);
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:46: error: illegal start of type
setVisible(true);
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:47: error: invalid method declaration; return type required
setResizable(false);
D:\NetBeansProjects\Contact Information\src\contact\information\myContactInformation.java:47: error: illegal start of type


View PostGregBrannon, on 10 October 2012 - 04:26 AM, said:

Post the errors you need help with, copied and pasted from what you're getting at your end.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1