Results 1 to 2 of 2
  1. #1
    pirate18 is offline Member
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default Problem with reading from record

    Hello guys. I have created two swing simple programs. The one gets info from the user and the other displays them. The problem is that the second class doesnot read the record. Does anynone have any idea what is the problem?

    The first class is the following:

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class InstructrorRecord extends JFrame implements ActionListener
    
    {
    	JLabel idLabel , nameLabel , addressLabel , cellLabel , phoneLabel;
    	private JTextField idText, nameText, addressText, cellText, phoneText;
    	
    	private JButton enter , done;
    	
    	private DataOutputStream output;
    	Container c;
    	
    		public InstructrorRecord()
    		{
    			super("Create Client File");
    			
    			try 
    			{
    			output = new DataOutputStream (new FileOutputStream ("client.dat"));	
    			}
    			catch (IOException e)
    			{
    				System.err.println("File not open properly" + e.toString());
    				System.exit(1);
    			}		
    		
    		    c = getContentPane();
    		    this.setSize(600,300);
    			this.setLayout(new GridLayout(6,2));
    			
    			idLabel = new JLabel ("Enter ID Number");
    			c.add(idLabel);
    			idText = new JTextField(20);
    			c.add(idText);
    			
    			nameLabel = new JLabel ("Enter Name");
    			c.add(nameLabel);
    			nameText = new JTextField(30);
    			c.add(nameText);
    			
    			addressLabel = new JLabel ("Enter Address");
    			c.add(addressLabel);
    			addressText = new JTextField (20);
    			c.add(addressText);
    			
    			cellLabel = new JLabel("Enter Cell Phone");
    			c.add(cellLabel);
    			cellText = new JTextField(20);
    			c.add(cellText);
    			
    			phoneLabel = new JLabel ("Enter Phone");
    			c.add(phoneLabel);
    			phoneText = new JTextField (20);
    			c.add(phoneText);
    			
    			enter = new JButton ("Enter");
    			enter.addActionListener(this);
    			c.add(enter);
    			
    			done = new JButton ("Done");
    			done.addActionListener(this);
    			c.add(done);
    		}
    		
    		public void addRecord()
    		{
    			int intId = 0;
    			int intCell = 0;
    			int intPhone = 0;
    			String name = ""; 
    			String address = "";
    	
    			if (! idText.getText().equals("")) 
    			{
    				
    			try { 
    				intId = Integer.parseInt(idText.getText());
    				if (intId > 0)
    				output.writeInt(intId);	
    				idText.setText("");
    				
    			    }
    				
    			   catch(IOException io)
    			    { 
    				 System.err.println("Error when writing to file" + 
    			    io.toString());
    				 System.exit(1);
    				 
    			    }
    			}
    			 
    			if(! cellText.getText().equals(""))
    			{
    				try
    				{
    					intCell = Integer.parseInt(cellText.getText());
    					if (intCell >0)
    					
    						output.writeInt(intCell);
    					cellText.setText("");
    					
    				}
    				
    				catch(IOException io)
    				{
    					System.err.println("Error when writing to file" +
    				io.toString());
    					System.exit(1);
    				}
    				
    			}
    			
    			if(! phoneText.getText().equals(""))
    			{
    				try
    				{
    					intPhone = Integer.parseInt(phoneText.getText());
    					if(intPhone >0)
    					
    						output.writeInt(intPhone);
    					phoneText.setText("");
    					
    				}
    				
    				catch(IOException io)
    				{
    					System.err.println("Error when writing to file" + 
    				io.toString());
    					System.exit(1);
    				}
    			}
    			
    			if(! nameText.getText().equals(""))
    			{
    				try
    				{
    					name = nameText.getText();
    					output.writeUTF(name);
    					nameText.setText("");
    				}
    				catch ( IOException io ) 
    				{
    			          System.err.println( "Error when writing to file" +
    			            io.toString( ) ) ;
    			          System.exit(1) ;
    				}
    			}
    			
    			if(! addressText.getText().equals(""))
    			{
    				try
    				{
    					address = addressText.getText();
    					output.writeUTF(address);
    					addressText.setText("");
    				}
    				catch(IOException io)
    				{
    					System.err.println("Error when writig to file" +
    				io.toString());
    					System.exit(1);
    				}
    			}	
    		}
    		
    		public void actionPerformed( ActionEvent e)
    		
    		{ 
    			if(e.getSource()==enter){
    			addRecord();
    		}
    			
    			
    			else	if(e.getSource() == done)
    			{
    				try
    				{
    					output.close();
    				}
    				catch (IOException io)
    				{
    					System.err.println("File not closed properly" + 
    				io.toString());
    					System.exit(1);
    				}
    				System.exit(0);
    			}
    		}
    		public static void main (String args[])
    		{
    			InstructrorRecord frame1 = new InstructrorRecord();
    			frame1.setVisible(true);
    		}
    }
    The read class is the following:
    Java Code:
     import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.io.*;
     
    public class ReadInstructrorRecord extends JFrame implements ActionListener
    {
    	private JTextField idText, nameText, addressText, 
    	                      cellText, phoneText;
    
        private JButton next , done;
        
        private DataInputStream input;
        
        JLabel idLabel, nameLabel, addressLabel,
                   cellLabel, phoneLabel;
        
        Container c;
        public ReadInstructrorRecord()
        {
        	super("Read Client File");
        	
        	try 
        	{
        		input = new DataInputStream ( new 
        				FileInputStream("client.dat"));
        	}
        	catch(IOException e)
        	{
        		System.err.println("File not opened properly\n" +
        	e.toString());
        		System.exit(1);
        	}
        	
        	 c = getContentPane();
    		    this.setSize(600,300);
    			this.setLayout(new GridLayout(6,2));
    			
        	
        	idLabel = new JLabel("Instructor's ID");
        	c.add(idLabel);
        	idText = new JTextField(20);
        	idText.setEditable(false);
        	c.add(idText);
        	
        	nameLabel = new JLabel("Instructor's Name");
        	c.add(nameLabel);
        	nameText = new JTextField(30);
        	nameText.setEditable(false);
        	c.add(nameText);
        	
        	addressLabel = new JLabel("Instructor's Address");
        	c.add(addressLabel);
        	addressText = new JTextField(20);
        	addressText.setEditable(false);
        	c.add(addressText);
        	
        	cellLabel = new JLabel ("Instructor's Cell Phone");
        	c.add(cellLabel);
        	phoneText = new JTextField(20);
        	phoneText.setEditable(false);
        	c.add(phoneText);
        	
        	phoneLabel = new JLabel ("Instructor's Phone");
        	c.add(phoneLabel);
        	phoneText = new JTextField(20);
        	phoneText.setEditable(false);
        	c.add(phoneText);
        	
        	next = new JButton ("Next");
        	next.addActionListener(this);
        	c.add(next);
        	
        	done = new JButton ("Done");
        	done.addActionListener(this);
        	c.add(done);
        	
        	
        }
        
        public void actionPerformed(ActionEvent e)
        { 
        	if (e.getSource() == next)
        		
        		readRecord();
        	
        	else closeFile();
        	
        }
        
        public void readRecord()
        { 
            int intId , intPhone , intCell;
        	String name , address;
    
        	try
        	{
        		intId = input.readInt();
        		name = input.readUTF();
        		address = input.readUTF();
        		intCell = input.readInt();
        		intPhone = input.readInt();
        		
        		idText.setText (String.valueOf(intId));
        		nameText.setText(name);
        		addressText.setText(address);
        	cellText.setText(String.valueOf(intCell));
        		phoneText.setText(String.valueOf(intPhone));
        	}
        	catch (EOFException eof)
        	{
        		closeFile();
        	}
        	
        	catch (IOException e)
        	{
        		System.err.println("Error during reading from file\n" + 
        	e.toString());
        		System.exit(1);
        	}
        }
        
        public void closeFile()
        {
        	try
        	{
        		
        	input.close();
       
        	System.exit(0);
        	}
        	catch (IOException e)
        	{
        		System.err.println("Error when closing file\n"+
        	e.toString());
        		System.exit(1);
        	}
        }
        
        public static void main (String args[])
        {
       ReadInstructrorRecord frame1 =  new ReadInstructrorRecord();
       frame1.setVisible(true);
        }
    }
    Last edited by JosAH; 05-19-2015 at 06:08 PM. Reason: added [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problem with reading from record

    Please edit your post and wrap the code in code tags to preserve its formatting.

    the second class doesnot read the record.
    What record is that?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: 07-09-2011, 07:15 PM
  2. Problem with deleting record.
    By javaguy345 in forum JDBC
    Replies: 18
    Last Post: 05-21-2011, 02:37 AM
  3. problem with inserting new record
    By gerard kowara in forum JDBC
    Replies: 1
    Last Post: 10-26-2010, 11:33 AM
  4. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  5. Some problem in fetching record
    By Shivraj in forum NetBeans
    Replies: 1
    Last Post: 03-27-2009, 11:19 AM

Posting Permissions

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