validity checks

I have most of the program done but stuck on checking for valid info. I need to check to make sure the account number is 10 numbers long, parse the amount to a double, check to make sure zip is an integer and state is a length of 2. Where and how would I write this?

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.*;

publicclass BillPayerextends JFrameimplements ActionListener

{

//Delcare output stream

DataOutputStream output;

//construct a panel for each row

JPanel firstRow =new JPanel();

JPanel secondRow =new JPanel();

JPanel thirdRow =new JPanel();

JPanel fourthRow =new JPanel();

JPanel fifthRow =new JPanel();

JPanel sixthRow =new JPanel();

JPanel seventhRow =new JPanel();

JPanel eighthRow =new JPanel();

//construct a panel for the fields and buttons

JPanel fieldPanel =new JPanel();

JPanel buttonPanel =new JPanel();

//construct labels and text boxes

JLabel accntNumLabel =new JLabel("Account Number: ");

JTextField accntNum =new JTextField(15);

JLabel pmtLabel =new JLabel("Payment Amount:");

JTextField pmt =new JTextField(10);

JLabel firstNameLabel =new JLabel("First Name:");

JTextField firstName =new JTextField(10);

JLabel lastNameLabel =new JLabel("Last Name:");

JTextField lastName=new JTextField(20);

JLabel addressLabel =new JLabel("Address:");

JTextField address =new JTextField(35);

JLabel cityLabel =new JLabel("City:");

JTextField city =new JTextField(10);

JLabel stateLabel =new JLabel("State:");

JTextField state =new JTextField(2);

JLabel zipLabel =new JLabel("Zip:");

JTextField zip =new JTextField(9);

//construct button

JButton submitButton =new JButton("Submit");

publicstaticvoid main(String[] args)

{

//set the look and feel of the interface

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error", JOptionPane.INFORMATION_MESSAGE);

}

BillPayer f=new BillPayer();

f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

f.setSize(450,300);

f.setTitle("Crandall Power and Light Payments");

f.setResizable(false);

f.setLocation(200,200);

f.setVisible(true);

}

public BillPayer()

{

Container c = getContentPane();

c.setLayout((new BorderLayout()));

fieldPanel.setLayout(new GridLayout(8,1));

FlowLayout rowSetup =new FlowLayout(FlowLayout.LEFT,5,3);

firstRow.setLayout(rowSetup);

secondRow.setLayout(rowSetup);

thirdRow.setLayout(rowSetup);

fourthRow.setLayout(rowSetup);

fifthRow.setLayout(rowSetup);

sixthRow.setLayout(rowSetup);

seventhRow.setLayout(rowSetup);

eighthRow.setLayout(rowSetup);

buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

//add fields to rows

firstRow.add(accntNumLabel);

firstRow.add(pmtLabel);

secondRow.add(accntNum);

secondRow.add(pmt);

thirdRow.add(firstNameLabel);

thirdRow.add(lastNameLabel);

thirdRow.add(lastNameLabel);

fourthRow.add(firstName);

fourthRow.add(lastName);

fifthRow.add(addressLabel);

sixthRow.add(address);

seventhRow.add(cityLabel);

seventhRow.add(stateLabel);

seventhRow.add(zipLabel);

eighthRow.add(city);

eighthRow.add(state);

eighthRow.add(zip);

//add rows to panel

fieldPanel.add(firstRow);

fieldPanel.add(secondRow);

fieldPanel.add(thirdRow);

fieldPanel.add(fourthRow);

fieldPanel.add(fifthRow);

fieldPanel.add(sixthRow);

fieldPanel.add(seventhRow);

fieldPanel.add(eighthRow);

//add button to panel

buttonPanel.add(submitButton);

//add panels to frame

c.add(fieldPanel, BorderLayout.CENTER);

c.add(buttonPanel, BorderLayout.SOUTH);

//add functionality to buttons

submitButton.addActionListener(this);

//Get current date and open the file

Date today =new Date();

SimpleDateFormat myFormat =new SimpleDateFormat("MMddyyyy");

String filename ="payments" + myFormat.format(today);

try

{

output =new DataOutputStream(new FileOutputStream(filename));

}

catch(IOException io)

{

JOptionPane.showMessageDialog(null,"The program could not create a storage location. Please check the disk drive and then run the program again.","Error", JOptionPane.INFORMATION_MESSAGE);

System.exit(1);

}

addWindowListener(

new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?","File Submission", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)

System.exit(0);

}

}

);

}

publicvoid actionPerformed(ActionEvent e)

{

String arg = e.getActionCommand();

if (checkFields())

{

try

{

output.writeUTF(accntNum.getText());

output.writeUTF(pmt.getText());

output.writeUTF(firstName.getText());

output.writeUTF(lastName.getText());

output.writeUTF(address.getText());

output.writeUTF(city.getText());

output.writeUTF(state.getText());

output.writeUTF(zip.getText());

JOptionPane.showMessageDialog(null,"The payment information has been saved.","Submission Successful", JOptionPane.INFORMATION_MESSAGE);

}

catch(IOException c)

{

System.exit(1);

}

clearFields();

}

}

publicboolean checkFields()

{

if ((accntNum.getText().compareTo("")<1) ||

(pmt.getText().compareTo("")<1)||

(firstName.getText().compareTo("")<1) ||

(lastName.getText().compareTo("")<1) ||

(address.getText().compareTo("")<1)||

(city.getText().compareTo("")<1)||

(state.getText().compareTo("")<1)||

(zip.getText().compareTo("")<1))

{

JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error", JOptionPane.WARNING_MESSAGE);

returnfalse;

}

else

{

returntrue;

}

}

publicvoid clearFields()

{

//clear fields and reset the focus

accntNum.setText("");

pmt.setText("");

firstName.setText("");

lastName.setText("");

address.setText("");

city.setText("");

state.setText("");

zip.setText("");

accntNum.requestFocus();

}

}

[11761 byte] By [dbrinea] at [2007-11-27 4:37:00]
# 1

> I have most of the program done but stuck on checking

> for valid info. I need to check to make sure the

> account number is 10 numbers long, parse the amount

> to a double, check to make sure zip is an integer and

> state is a length of 2. Where and how would I write

> this?>

This is way too vague, especially considering the reams of code that follow.

What specifically do you not know how to do? Check the length? Parse?

cotton.ma at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 2
When the user enters an account number. I want to check to make sure it's length is 10. If not display error and return to the account number box again. When the user enters an amount. I need to parse it do a double. When the user enters the state I need to make sure its only 2 letters
dbrinea at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 3

> When the user enters an account number. I want to

> check to make sure it's length is 10. If not display

> error and return to the account number box again.

> When the user enters an amount. I need to parse it do

> a double. When the user enters the state I need to

> make sure its only 2 letters long.

Okay.

So what's stopping you from doing that?

cotton.ma at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 4
Do you know there is a method in the String class that returns how long the String is?
floundera at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 5
> Do you know there is a method in the String class> that returns how long the String is?Yes.
cotton.ma at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 6
I'm not sure where to add it add? or how.
dbrinea at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 7
Wasn't talking to you, wiseass ;)
floundera at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 8
> I'm not sure where to add it add? or how.Write a method to validate user input and returns a boolean. Then you would call that method in an if statement.if(userInputValid()) {// everything OK} else {// print error message}
floundera at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 9
> I'm not sure where to add it add?Well it's your code... why don't you just take a guess.
cotton.ma at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 10
I have the if's now. another question. I am using JTextBox's, how do I parse those? I need to parse int and double, as well as find lengths
dbrinea at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 11

> I have the if's now. another question. I am using

> JTextBox's, how do I parse those? I need to parse int

> and double, as well as find lengths

You need to get string values from the text boxes. Consult the api for a method that will do this.

You already know how to check the length.

Consult the API Javadocs for the Integer and Double classes for methods that will parse the primitive values out of Strings.

cotton.ma at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...
# 12

Thanks for all the help. One last question. I figured out eh length problem. How do I parse the statements to verify the zip and payment amount are int/ doubles? Currently they are in the if statement.

/*

Chapter 8:BillPayer Power & Light

Programmer:Joy Starks

Date:November 19, 2004

Programmer Name:BillPayer

*/

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.*;

public class BillPayer extends JFrame implements ActionListener

{

//Delcare output stream

DataOutputStream output;

//construct a panel for each row

JPanel firstRow = new JPanel();

JPanel secondRow = new JPanel();

JPanel thirdRow = new JPanel();

JPanel fourthRow = new JPanel();

JPanel fifthRow = new JPanel();

JPanel sixthRow = new JPanel();

JPanel seventhRow = new JPanel();

JPanel eighthRow = new JPanel();

//construct a panel for the fields and buttons

JPanel fieldPanel = new JPanel();

JPanel buttonPanel = new JPanel();

//construct labels and text boxes

JLabel accntNumLabel = new JLabel("Account Number: ");

JTextField accntNum = new JTextField(15);

JLabel pmtLabel = new JLabel("Payment Amount:");

JTextField pmt = new JTextField(10);

JLabel firstNameLabel = new JLabel("First Name:");

JTextField firstName = new JTextField(10);

JLabel lastNameLabel = new JLabel("Last Name:");

JTextField lastName= new JTextField(20);

JLabel addressLabel = new JLabel("Address:");

JTextField address = new JTextField(35);

JLabel cityLabel = new JLabel("City:");

JTextField city = new JTextField(10);

JLabel stateLabel = new JLabel("State:");

JTextField state = new JTextField(2);

JLabel zipLabel = new JLabel("Zip:");

JTextField zip = new JTextField(9);

//construct button

JButton submitButton = new JButton("Submit");

public static void main(String[] args)

{

//set the look and feel of the interface

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel for this application.","Error", JOptionPane.INFORMATION_MESSAGE);

}

BillPayer f= new BillPayer();

f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

f.setSize(450,300);

f.setTitle("Crandall Power and Light Payments");

f.setResizable(false);

f.setLocation(200,200);

f.setVisible(true);

}

public BillPayer()

{

Container c = getContentPane();

c.setLayout((new BorderLayout()));

fieldPanel.setLayout(new GridLayout(8,1));

FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);

firstRow.setLayout(rowSetup);

secondRow.setLayout(rowSetup);

thirdRow.setLayout(rowSetup);

fourthRow.setLayout(rowSetup);

fifthRow.setLayout(rowSetup);

sixthRow.setLayout(rowSetup);

seventhRow.setLayout(rowSetup);

eighthRow.setLayout(rowSetup);

buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

//add fields to rows

firstRow.add(accntNumLabel);

firstRow.add(pmtLabel);

secondRow.add(accntNum);

secondRow.add(pmt);

thirdRow.add(firstNameLabel);

thirdRow.add(lastNameLabel);

thirdRow.add(lastNameLabel);

fourthRow.add(firstName);

fourthRow.add(lastName);

fifthRow.add(addressLabel);

sixthRow.add(address);

seventhRow.add(cityLabel);

seventhRow.add(stateLabel);

seventhRow.add(zipLabel);

eighthRow.add(city);

eighthRow.add(state);

eighthRow.add(zip);

//add rows to panel

fieldPanel.add(firstRow);

fieldPanel.add(secondRow);

fieldPanel.add(thirdRow);

fieldPanel.add(fourthRow);

fieldPanel.add(fifthRow);

fieldPanel.add(sixthRow);

fieldPanel.add(seventhRow);

fieldPanel.add(eighthRow);

//add button to panel

buttonPanel.add(submitButton);

//add panels to frame

c.add(fieldPanel, BorderLayout.CENTER);

c.add(buttonPanel, BorderLayout.SOUTH);

//add functionality to buttons

submitButton.addActionListener(this);

//Get current date and open the file

Date today = new Date();

SimpleDateFormat myFormat = new SimpleDateFormat("MMddyyyy");

String filename = "payments" + myFormat.format(today);

try

{

output = new DataOutputStream(new FileOutputStream(filename));

}

catch(IOException io)

{

JOptionPane.showMessageDialog(null,"The program could not create a storage location. Please check the disk drive and then run the program again.","Error", JOptionPane.INFORMATION_MESSAGE);

System.exit(1);

}

addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)

System.exit(0);

}

}

);

}

public void actionPerformed(ActionEvent e)

{

String arg = e.getActionCommand();

if (checkFields())

{

try

{

output.writeUTF(accntNum.getText());

output.writeUTF(pmt.getText());

output.writeUTF(firstName.getText());

output.writeUTF(lastName.getText());

output.writeUTF(address.getText());

output.writeUTF(city.getText());

output.writeUTF(state.getText());

output.writeUTF(zip.getText());

JOptionPane.showMessageDialog(null, "The payment information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);

}

catch(IOException c)

{

System.exit(1);

}

clearFields();

}

}

public boolean checkFields()

{

if ((accntNum.getText().compareTo("")<1) ||

(pmt.getText().compareTo("")<1)||

(firstName.getText().compareTo("")<1) ||

(lastName.getText().compareTo("")<1) ||

(address.getText().compareTo("")<1)||

(city.getText().compareTo("")<1)||

(state.getText().compareTo("")<1)||

(zip.getText().compareTo("")<1))

{

JOptionPane.showMessageDialog(null, "You must complete all fields.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);

return false;

}

else

{

if ((accntNum.getText().compareTo("") !=10))

JOptionPane.showMessageDialog(null, "Account Number Wrong Length.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);

accntNum.setText("");

if (state.getText().compareTo("") != 2)

JOptionPane.showMessageDialog(null, "Please only 2 letters for the state.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);

state.setText("");

String pmtAmount = pmt.getText();

double pmtVal = Double.parseDouble(pmtAmount);

JOptionPane.showMessageDialog(null, "Please enter only numbers.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);

String zipCode = zip.getText();

int zipCD = Integer.parseInt(zipCode);

JOptionPane.showMessageDialog(null, "please enter only numbers.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);

return false;

}

}

public void clearFields()

{

//clear fields and reset the focus

accntNum.setText("");

pmt.setText("");

firstName.setText("");

lastName.setText("");

address.setText("");

city.setText("");

state.setText("");

zip.setText("");

accntNum.requestFocus();

}

}

dbrinea at 2007-7-12 9:47:11 > top of Java-index,Java Essentials,Java Programming...