Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading File problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Reading File problem

    how do access reading a text file to Login ?

    Here the problem!
    lgnbb.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ee){

    String acc,code;

    BufferedReader reader = null;
    String text= "";
    acc = txf1.getText();
    code = txf2.getText();
    if((txf1.getText().equals(""))||(txf2.getText().eq uals("")))
    {
    JOptionPane.showMessageDialog(null,
    "Please Type!!",
    "Incomplete",
    JOptionPane.WARNING_MESSAGE);
    }else{


    try{
    File file = new File("Database.txt");
    reader = new BufferedReader(new FileReader(file));
    String []data = text.split("\t");
    String accno = data[0];
    int cd = Integer.parseInt(data[1]);

    if((txf1.getText().equals(accno))||
    (txf2.getText().equals(cd))){
    JOptionPane.showMessageDialog(null,
    "Login Success!!!",
    "Login-Successfully!!!",
    JOptionPane.INFORMATION_MESSAGE);

    while((text=reader.readLine())!=null){
    WelcomeAcc exe = new WelcomeAcc();
    exe.wcAcc();
    dispose();
    }




    }

    }
    catch (FileNotFoundException ex){
    ex.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    } finally {
    try {
    if (reader != null){
    reader.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }

    }
    });
    Here the first Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
     
    public class LoginBank extends JFrame {
         private JButton back, lgnbb, clear, pcode;
         private JTextField txf1;
         private JPasswordField txf2;
         private JLabel ln,pd;
     
         public void lgBank (){
     
     
         	ln = new JLabel("Account No. : ");
    		ln.setFont(new Font("Serif", Font.BOLD, 24));
    		ln.setForeground(Color.BLACK);
    		ln.setLocation(20,23);
    		ln.setSize(200,30);
     
         	pd = new JLabel("Password: ");
    		pd.setFont(new Font("Serif", Font.BOLD, 24));
    		pd.setForeground(Color.BLACK);
    		pd.setLocation(20,60);
    		pd.setSize(200,30);
     
    		txf1 = new JTextField(20);
    		txf1.setBackground(new Color(204,204,255));
    		txf1.setLocation(170,25);
    		txf1.setFont(new Font("Serif", Font.PLAIN, 20));
    		txf1.setToolTipText("Type your Account ID or Number in textbox.");
    		txf1.setSize(200,25);
    		txf1.setHorizontalAlignment(JTextField.LEFT);
     
    		txf2 = new JPasswordField(20);
    		txf2.setBackground(new Color(204,204,255));
    		txf2.setLocation(170, 65);
    		txf2.setEchoChar('*');
    		txf2.setFont(new Font("Serif", Font.PLAIN, 20));
    		txf2.setToolTipText("Type 8-16 characters.");
    		txf2.setSize(200,25);
    		txf2.setHorizontalAlignment(JTextField.LEFT);
     
    		ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
    		final JButton back = new JButton(bcr);
    		back.setLocation(20,120);
    		back.setSize(70,30);
    		back.setPreferredSize(new Dimension(90,30));
    		back.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			dispose();
    			}
    		}
    		);
     
    		ImageIcon lll = new ImageIcon(getClass().getResource("/images/loginn.jpg"));
    		final JButton lgnbb = new JButton(lll);
    		lgnbb.setLocation(100, 120);
    		lgnbb.setSize(70,40);
    		lgnbb.setPreferredSize(new Dimension(70,40));
    		lgnbb.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ee){
     
    			String acc,code;
     
    						BufferedReader reader = null;
    						String text= "";
                    acc = txf1.getText();
                    code = txf2.getText();
                    if((txf1.getText().equals(""))||(txf2.getText().equals("")))
                    	{
    				JOptionPane.showMessageDialog(null,
    					"Please Type!!",
    					"Incomplete",
    					JOptionPane.WARNING_MESSAGE);
    			}else{
     
     
    					try{
    						File file = new File("Database.txt");
    						reader = new BufferedReader(new FileReader(file));
    						String []data = text.split("\t");
    						String accno = data[0];
    						int cd = Integer.parseInt(data[1]);
     
    						if((txf1.getText().equals(accno))||
    							(txf2.getText().equals(cd))){
    								JOptionPane.showMessageDialog(null,
    								"Login Success!!!",
    								"Login-Successfully!!!",
    								JOptionPane.INFORMATION_MESSAGE);
     
    							while((text=reader.readLine())!=null){
                    				WelcomeAcc exe = new WelcomeAcc();
    								exe.wcAcc();
    								dispose();
                				}
     
     
     
     
    							}
     
    						}
    						catch (FileNotFoundException ex){
    		          			ex.printStackTrace();
    		      			} catch (IOException ex) {
    		           			 ex.printStackTrace();
    		       			} finally {
    		        		    try {
    		            		   	if (reader != null){
    		                			reader.close();
    		              		}
    		           		} catch (IOException ex) {
    		            		    ex.printStackTrace();
    		           	}
    		        }
    		         }
     
    		}
    		});
     
    		ImageIcon cle = new ImageIcon(getClass().getResource("/images/clear.jpg"));
    		final JButton clear = new JButton(cle);
    		clear.setLocation(190,120);
    		clear.setSize(70,40);
    		clear.setPreferredSize(new Dimension(90,50));
    		clear.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent eee){
    			txf1.setText("");
    			txf2.setText("");
    			}
    		}
    		);
     
    		ImageIcon pco = new ImageIcon(getClass().getResource("/images/pcode.jpg"));
    		final JButton pcode = new JButton(pco);
    		pcode.setLocation(280,120);
    		pcode.setSize(70,40);
    		pcode.setPreferredSize(new Dimension(70,40));
    		pcode.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent eeee){
    			PinBank pbnk = new PinBank();
    			pbnk.pinBnk();
    			}
    		}
    		);
     
    		Container pane = getContentPane();
    		pane.setLayout(null);
    		pane.add(lgnbb);
    		pane.add(clear);
    		pane.add(pcode);
    		pane.add(back);
    		pane.add(ln);
    		pane.add(pd);
    		pane.add(txf1);
    		pane.add(txf2);
     
    		setIconImage(new ImageIcon("icon1.jpg").getImage());
    		setLayout(new BorderLayout());
    		JLabel lgn = new JLabel(new ImageIcon(getClass().getResource("/images/lgn.jpg")));
    		add(lgn);
    		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		setTitle("Login Account");
    		setSize(400,220);
    		setResizable(false);
    		setVisible(true);
    		setLocation(120, 300);
     
         }
    }

    Here the second Code for login window:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class WelcomeAcc extends JFrame {
         private JButton back;
     
         public void wcAcc (){
    		ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
    		final JButton back = new JButton(bcr);
    		back.setLocation(20,480);
    		back.setSize(70,30);
    		back.setPreferredSize(new Dimension(90,30));
    		back.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			dispose();
    			}
    		}
    		);
     
    		Container pane = getContentPane();
    		pane.setLayout(null);
    		pane.add(back);
     
    		setIconImage(new ImageIcon("/images/icon.jpg").getImage());
    		setLayout(new BorderLayout());
    		JLabel www=new JLabel(new ImageIcon("/images/welcome.jpg"));
    		add(www);
    		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		setTitle("Change Pin Code");
    		setSize(650,550);
    		setResizable(false);
    		setVisible(true);
    		setLocation(180, 5);
         }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading File problem

    Please explain what the problem is? What happens when you compile and execute the program?
    If there are errors, copy the full text and paste it here.
    If the results are not what you want, copy and paste the results here and explain what is wrong and show what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading From File Problem
    By Neobs in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: April 8th, 2012, 10:35 PM
  2. [SOLVED] Problem in File Reading...
    By Mr.777 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 20th, 2011, 07:24 AM
  3. Problem reading correctly from a file
    By stavrakas in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2011, 08:09 AM
  4. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  5. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM