Hello. I am writing an ATM program for a project, but I am having problems matching the users entered pin with what is stored on the text file. Here is how i read the file

import java.io.File;
import java.util.Scanner;
import machine.*;
import javax.swing.JOptionPane;


public class FileRead 
{
	Transactions query = new Transactions ();
	private Scanner read;
	private String pin;
	
	public void setPin ( String number )
	{
		pin = number;
	}
	
	public String getPin ()
	{
		return pin;
	}
	
	
	public void openFile ()
	{
		try
		{
			read = new Scanner( new File ( "Customers.txt" ) );
		}
		catch ( Exception e )
		{
			JOptionPane.showMessageDialog( null, "Customer does not exsist" );
		}
	}
	
	public void readFile ()
	{
		String input1 = "holding_name";
		String input2 = "holding_pin#";
		String input3 = "holding_balance";
		
		
		while (read.hasNext())
		{
			input1 = read.next();
			input2 = read.next();
			input3 = read.next();
		
		if ( input2.equals(pin) )
		{
			
			query.setBalance(input3);
			JOptionPane.showMessageDialog( null,String.format("Balance has $%s", query.getBalance()));
		}
	}
}	
	public void closeFile ()
	{
		read.close();
	}
	

}

Now when the program gets to this part I find that the pin is set. However, the set balance does not receive any data.What am I doing wrong, is it my "while" statement that needs help?

do you want to read in a whole list of customer information or is it only one customer that you are reading in, and are you looking for a specific customer in the file. Be specific about what you are doing in the file so we can help.

I have a hunch it is a read error.

As cam875 said, we cannot help you with what you gave us. Please provide us with more information, and also post the Customers.txt file so we'd confirm that it is not a read error.

I want to read the entire file and then pick out a specific piece of data (the users pin). This is the text file:

1111;David;10000
2222;Sammy;20000
Henry;null;null
3333;Danny;40000


The first column holds the user's pin then the second has the user's name and lastly, the third has balance on the account(ignore the third row made a mistake). P.S please re-arrange variables in previous code accordingly. Thank you.

I would start off by creating a class for holding the attributes (Customer just rings a bell to me).

I would then probably use something like BufferedReader to go through each line in the file. At this time i would create some type of collection to hold the values.

As you read each line, i would use the split method of the string class to break the line apart on the semi-colon. Providing the data in the file is the same, you should have 3 indexes on the returned array.

I would then load each of those values into the created Customer class, and then add that to the collection.

Once all the data is read you can close the text file. Then loop through all your values in the collection trying to find the proper pin number.

Although this is a lengthy (and somewhat inefficient) solution it should help you point out where your initial mistake is made.

Cheers

I would start off by creating a class for holding the attributes (Customer just rings a bell to me).

I would then probably use something like BufferedReader to go through each line in the file. At this time i would create some type of collection to hold the values.

As you read each line, i would use the split method of the string class to break the line apart on the semi-colon. Providing the data in the file is the same, you should have 3 indexes on the returned array.

I would then load each of those values into the created Customer class, and then add that to the collection.

Once all the data is read you can close the text file. Then loop through all your values in the collection trying to find the proper pin number.

Although this is a lengthy (and somewhat inefficient) solution it should help you point out where your initial mistake is made.

Cheers

I will have to do some read now, but thanks mate.

No Problems. If you get stuck feel free to post what you have any myself or others will help you out :)

using bufferedReader is a way to solve this, but another way could be is you stick to Scanner class, but sanitize your text file. for example, instead of a ";" between each attribute, use a " " (blank space) because in next() method of scanner, it stops once it finds a blank space.. in your case, you're taking the whole line as one input and that is causing your failure.

in simple:

1- change ";" to " "

2- in the program, make sure that your following the right reading format

3- you're all set :)!

This is my code which shows the user interface that accepts the password and matches it with the text file entries. The problem that I am having is the matching of the entered PIN with the text file PINs. If the PIN matches the PIN in the text file it should update the users account balance so they can perform transactions from that account (Area in red deals with textfield and links to text file, that is where the problem lies. GUYS HELP PLEASEEEEEEE IT IS DRIVING ME CRAZY! : #1

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;

import javax.swing.*;


public class AtmInterface 
{
	
	public static final String EMPTY_STRING = "";
	String numbers;
	String combine = "";
	private JFrame frame;
	private JPasswordField pin ;
	private JTextArea display;
	private JPanel panel;
	private JLabel label;
	private JPanel subPanel, subPanel2, subPanel3, subPanel4;
	private JButton deposit, withdraw, exit, transaction, balance;
	private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, button_, buttonEnter;
	

	public AtmInterface()
	{
		
		frame = new JFrame( "ATM Program" );
		panel = new JPanel( new BorderLayout ());
		label = new JLabel ( "Please Enter PIN then press enter" );
		subPanel = new JPanel();
		subPanel2 = new JPanel();
		subPanel3 = new JPanel();
		subPanel4 = new JPanel ();
		pin = new JPasswordField(10);
		
		
		
		display = new JTextArea( 15, 30 );
		balance = new JButton( "Display Account Balance" );
		deposit = new JButton( "Make a Deposit" );
		withdraw = new JButton( "Make a Withdraw" );
		transaction = new JButton ( "New Transaction");
		exit = new JButton ( "Exit" );
		//enter = new JButton ( "Enter" );
		
		/*for ( int button = 1; button <= 9; button++ )
		{
			subPanel2.add( new JButton ( "" + button ));
		}*/
		button1 = new JButton("1");
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		button5 = new JButton("5");
		button6 = new JButton("6");
		button7 = new JButton("7");
		button8 = new JButton("8");
		button9 = new JButton("9");
		button0 = new JButton("0");
		button_ = new JButton(".");
		buttonEnter = new JButton("Enter");
	
		
		subPanel.setLayout(new GridLayout (5, 0));
		subPanel.setBorder( BorderFactory.createTitledBorder("Task Buttons"));
		subPanel.add(deposit);
		subPanel.add(withdraw);
		subPanel.add(balance);
		subPanel.add(transaction);
		subPanel.add(exit);
		
		
		subPanel2.setLayout(new GridLayout(4,3));
		subPanel2.setBorder( BorderFactory.createTitledBorder("Keypad"));
		subPanel2.add(button1);
		subPanel2.add(button2);
		subPanel2.add(button3);
		subPanel2.add(button4);
		subPanel2.add(button5);
		subPanel2.add(button6);
		subPanel2.add(button7);
		subPanel2.add(button8);
		subPanel2.add(button9);
		subPanel2.add(button0);
		subPanel2.add(button_);
		subPanel2.add(buttonEnter);
		
		subPanel3.setBorder( BorderFactory.createTitledBorder( "Display"));
		display.setBorder( BorderFactory.createLineBorder( Color.RED, 5));
		subPanel3.add( display );
		
		subPanel4.setBorder( BorderFactory.createTitledBorder("Login"));
		subPanel4.setBackground( Color.BLACK );
		label.setForeground( Color.white );
		subPanel4.add( label );
		subPanel4.add(pin);
		//subPanel4.add( enter );
		
		
		panel.setBackground( Color.BLACK );
		panel.add(subPanel, BorderLayout.WEST); 
		panel.add(subPanel2, BorderLayout.SOUTH);
		panel.add(subPanel3, BorderLayout.CENTER);
		panel.add(subPanel4, BorderLayout.EAST);

		frame.add( panel );
		frame.setLocation( 250, 300 );
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);	
		frame.setSize( 850, 450 );
		frame.setVisible(true);
		
		pin.addActionListener( new PinHandler ());
		deposit.addActionListener( new ButtonHandler ());
		withdraw.addActionListener(new WithdrawListener ());
		balance.addActionListener(new ButtonHandler());
		button1.addActionListener(new ButtonHandler());
		button2.addActionListener(new ButtonHandler());
		button3.addActionListener(new ButtonHandler());
		button4.addActionListener(new ButtonHandler());
		button5.addActionListener(new ButtonHandler());
		button6.addActionListener(new ButtonHandler());
		button7.addActionListener(new ButtonHandler());
		button8.addActionListener(new ButtonHandler());
		button9.addActionListener(new ButtonHandler());
		button0.addActionListener(new ButtonHandler());
		button_.addActionListener(new ButtonHandler());
		buttonEnter.addActionListener(new ButtonHandler());
		
	}
	
	private class ButtonHandler implements ActionListener
	{
               /* Checking check = new Checking();
                double value = 0.00;
        
                public void set()
                {
                    check.setBalance(check.getBalance() + value); 
                }*/
            
		public void actionPerformed(ActionEvent event )
		{
			
			JButton clickedButton = (JButton) event.getSource();
			Transactions query = new Transactions ();
			
				
				if( clickedButton == button1 )
				{
					numbers = String.valueOf(1);
					display.append(event.getActionCommand());
				   
				}
				
				else
					if ( clickedButton == button2 )
					   {
						 numbers = String.valueOf(2);
						 display.append(event.getActionCommand());
					   }
					else
						if ( clickedButton == button3 )
						   {
							 numbers = String.valueOf(3);
							 display.append(event.getActionCommand());
						   }
						else
							if ( clickedButton == button4 )
							   {
								 numbers = String.valueOf(2);
								 display.append(event.getActionCommand());
							   }
							else
								if ( clickedButton == button5 )
								   {
									 numbers = String.valueOf(2);
									 display.append(event.getActionCommand());
								   }
								else
									if ( clickedButton == button6 )
									   {
										 numbers = String.valueOf(2);
										 display.append(event.getActionCommand());
									   }
									else
										if ( clickedButton == button7 )
										   {
											 numbers = String.valueOf(2);
											 display.append(event.getActionCommand());
										   }
										else
											if ( clickedButton == button8 )
											   {
												 numbers = String.valueOf(2);
												 display.append(event.getActionCommand());
											   }
											else
												if ( clickedButton == button9 )
												   {
													 numbers = String.valueOf(2);
													 display.append(event.getActionCommand());
												   }
				 
				
				combine = display.getText();
				JOptionPane.showMessageDialog(null, String.format ("Text has : %s", combine));

			
			}
		}
	
	
	private class WithdrawListener implements ActionListener
	{
		
		public void actionPerformed ( ActionEvent event )
		{
			
			JButton clickedButton = (JButton) event.getSource();
			Transactions query = new Transactions ();
			
			if ( clickedButton == withdraw )
			{
				display.append( "What is your withdrawal amount?: " );
				ButtonHandler keypad = new ButtonHandler ();
				keypad.actionPerformed(event);
				//query.setWithdraw( Double.parseDouble(combine));
				//query.withdraw();
				
				//numbers = String.valueOf(display.getText());
				//numbers = display.getText();
			}
			//display.setText( EMPTY_STRING );
			
		}
	}
	
	private class PinHandler implements ActionListener
	{
		
		public void actionPerformed ( ActionEvent event )
		{
			//JButton clickedButton = (JButton) event.getSource();
			FileRead read = new FileRead ();
			Transactions query = new Transactions ();
			
			
			{
				JPasswordField input = ( JPasswordField ) event.getSource();
				char []	data = input.getPassword();
				String storedPin = new String ( data );
				 //String idNumber = pin.getText();
				
				read.setPin( storedPin );
				read.openFile();
				read.readFile();
				//read.closeFile();
				//JOptionPane.showMessageDialog(null,String.format("Pin has : %s", read.getPin()) );
			
				//numbers = String.valueOf(display.getText());
				//numbers = display.getText();
			}
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.