How do I get my code to read the spaces between longs?
Posted
by
WahtsUpWorld
on Stack Overflow
See other posts from Stack Overflow
or by WahtsUpWorld
Published on 2014-06-02T03:19:38Z
Indexed on
2014/06/02
3:26 UTC
Read the original article
Hit count: 337
I apologize for any inconvenience that may occur in answering my question, I'm fairly new to programming and I'm so far only in the last weeks of my community college Java I class. The problem I am facing is in my code of which I cannot seem to get the PrintWriter to address the spaces in between my longs' phone number and social security I.D. The entire code consists of two classes in which one pulls from the other the information needed to parse and present the file writer/print writer. Here is the entire code w/ the second class after it:
public class FinalProjectGroup1
{
public static void main(String[] args)
{
}
public String name;
public long ssid;
public double pay;
public String address;
public long number;
public void cleanUpConstructor()
{}
public FinalProjectGroup1(String name, String address, double pay, long ssid, long number){
this.name = name;
this.pay = pay;
this.ssid = ssid;
this.address = address;
this.number = number;
cleanUpConstructor();
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setPay(double pay)
{
this.pay = pay;
}
public double getPay()
{
return pay;
}
public void setSSID(long ssid)
{
this.ssid = ssid;
}
public long getSSID()
{
return ssid;
}
public void setAddress(String address)
{
this.address = address;
}
public String getAddress()
{
return address;
}
public void setNumber(long number)
{
this.number = number;
}
public long getNumber()
{
return number;
}
}
SECOND CLASS
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JButton;
import javax.swing.JTextField;
import FinalProjectGroup1;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileWriter;
import java.io.PrintWriter;
public class FinalProjectGroup1Window {
public JFrame frmTheBosssSecretary;
public JTextField txtName;
public JTextField txtSSID;
public JTextField txtAddress;
public JTextField txtNumber;
public JTextField txtPay;
public JTextField txtFindName;
public JTextField txtFindSSID;
public JTextField txtFindPay;
public JTextField txtFolder;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FinalProjectGroup1Window window = new FinalProjectGroup1Window();
window.frmTheBosssSecretary.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FinalProjectGroup1Window() {
initialize();
}
private void initialize() {
frmTheBosssSecretary = new JFrame();
frmTheBosssSecretary.setFont(new Font("Times New Roman", Font.PLAIN, 12));
frmTheBosssSecretary.setTitle("The Boss's Secretary: Employee Generator/Finder");
frmTheBosssSecretary.setBounds(100, 100, 547, 302);
frmTheBosssSecretary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTheBosssSecretary.getContentPane().setLayout(null);
JLabel lblFile = new JLabel("Employee Folder:");
lblFile.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblFile.setBounds(10, 10, 93, 14);
frmTheBosssSecretary.getContentPane().add(lblFile);
JLabel lblFindEmployee = new JLabel("Employee Finder");
lblFindEmployee.setFont(new Font("Times New Roman", Font.BOLD, 18));
lblFindEmployee.setBounds(194, 159, 142, 20);
frmTheBosssSecretary.getContentPane().add(lblFindEmployee);
JLabel lblEmployeeName = new JLabel("Employee Name:");
lblEmployeeName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblEmployeeName.setBounds(10, 35, 93, 14);
frmTheBosssSecretary.getContentPane().add(lblEmployeeName);
JLabel lblSSID = new JLabel("Employee SSID:");
lblSSID.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblSSID.setBounds(10, 135, 85, 14);
frmTheBosssSecretary.getContentPane().add(lblSSID);
JLabel lblAddress = new JLabel("Employee Address:");
lblAddress.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblAddress.setBounds(10, 60, 105, 14);
frmTheBosssSecretary.getContentPane().add(lblAddress);
JLabel lblPhoneNumber = new JLabel("Employee Phone Number:");
lblPhoneNumber.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblPhoneNumber.setBounds(10, 85, 134, 14);
frmTheBosssSecretary.getContentPane().add(lblPhoneNumber);
JLabel lblPayRate = new JLabel("Employee Pay Rate:");
lblPayRate.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblPayRate.setBounds(10, 110, 105, 14);
frmTheBosssSecretary.getContentPane().add(lblPayRate);
JLabel lblFindEmployeeName = new JLabel("Find Employee Name:");
lblFindEmployeeName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblFindEmployeeName.setBounds(10, 183, 115, 14);
frmTheBosssSecretary.getContentPane().add(lblFindEmployeeName);
JLabel lblFindSSID = new JLabel("Find Employee SSID:");
lblFindSSID.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblFindSSID.setBounds(10, 208, 105, 14);
frmTheBosssSecretary.getContentPane().add(lblFindSSID);
JLabel lblFindPay = new JLabel("Find Employee Address:");
lblFindPay.setFont(new Font("Times New Roman", Font.PLAIN, 12));
lblFindPay.setBounds(10, 233, 124, 14);
frmTheBosssSecretary.getContentPane().add(lblFindPay);
txtFolder = new JTextField();
txtFolder.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtFolder.setBounds(105, 7, 314, 20);
frmTheBosssSecretary.getContentPane().add(txtFolder);
txtFolder.setColumns(10);
txtName = new JTextField();
txtName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtName.setBounds(99, 32, 247, 20);
frmTheBosssSecretary.getContentPane().add(txtName);
txtName.setColumns(10);
txtAddress = new JTextField();
txtAddress.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtAddress.setBounds(109, 57, 237, 20);
frmTheBosssSecretary.getContentPane().add(txtAddress);
txtAddress.setColumns(10);
txtNumber = new JTextField();
txtNumber.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtNumber.setBounds(141, 82, 160, 20);
frmTheBosssSecretary.getContentPane().add(txtNumber);
txtNumber.setColumns(10);
txtPay = new JTextField();
txtPay.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtPay.setBounds(116, 107, 105, 20);
frmTheBosssSecretary.getContentPane().add(txtPay);
txtPay.setColumns(10);
txtSSID = new JTextField();
txtSSID.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtSSID.setBounds(97, 132, 124, 20);
frmTheBosssSecretary.getContentPane().add(txtSSID);
txtSSID.setColumns(10);
txtFindName = new JTextField();
txtFindName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtFindName.setBounds(122, 180, 314, 20);
frmTheBosssSecretary.getContentPane().add(txtFindName);
txtFindName.setColumns(10);
txtFindSSID = new JTextField();
txtFindSSID.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtFindSSID.setBounds(122, 205, 122, 20);
frmTheBosssSecretary.getContentPane().add(txtFindSSID);
txtFindSSID.setColumns(10);
txtFindPay = new JTextField();
txtFindPay.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtFindPay.setBounds(141, 230, 237, 20);
frmTheBosssSecretary.getContentPane().add(txtFindPay);
txtFindPay.setColumns(10);
JButton btnAddEmployee = new JButton("Add Employee");
btnAddEmployee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
String name = txtName.getText();
String address = txtAddress.getText();
double pay = Double.parseDouble(txtPay.getText());
long ssid = Long.parseLong(txtSSID.getText());
long number = Long.parseLong(txtNumber.getText());
FinalProjectGroup1 ee = new FinalProjectGroup1(name, address, pay, ssid, number);
FileWriter writer = new FileWriter(txtFolder.getText(), true);
PrintWriter pw = new PrintWriter(writer);
pw.println(ee.getName() + ", " + ee.getAddress() + ", " + ee.getNumber()
+ ", " + ee.getPay() + ", " + ee.getSSID());
pw.close();
}
catch (Exception e)
{
return;
}
}
});
JButton btnFolder = new JButton("Folder");
btnFolder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
JFileChooser bsearch = new JFileChooser();
int result = bsearch.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION) return;
txtFolder.setText(bsearch.getSelectedFile().getAbsolutePath());
}
});
btnFolder.setFont(new Font("Times New Roman", Font.PLAIN, 12));
btnFolder.setBounds(429, 6, 75, 23);
frmTheBosssSecretary.getContentPane().add(btnFolder);
btnAddEmployee.setFont(new Font("Times New Roman", Font.PLAIN, 12));
btnAddEmployee.setBounds(356, 42, 159, 107);
frmTheBosssSecretary.getContentPane().add(btnAddEmployee);
JButton btnFindName = new JButton("Find");
btnFindName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
btnFindName.setBounds(446, 179, 69, 23);
frmTheBosssSecretary.getContentPane().add(btnFindName);
JButton btnFindSSID = new JButton("Find");
btnFindSSID.setFont(new Font("Times New Roman", Font.PLAIN, 12));
btnFindSSID.setBounds(250, 204, 85, 23);
frmTheBosssSecretary.getContentPane().add(btnFindSSID);
JButton btnFindAddress = new JButton("Find");
btnFindAddress.setFont(new Font("Times New Roman", Font.PLAIN, 12));
btnFindAddress.setBounds(389, 229, 85, 23);
frmTheBosssSecretary.getContentPane().add(btnFindAddress);
}
}
The problem here lies in the JButton Add Employee. Where, as previously mentioned, the long's phone number and social security I.D. don't show the spaces in the text file.
© Stack Overflow or respective owner