Hello, I was hoping that someone might be able to help me with this program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class CustomerInfo extends JFrame implements ActionListener
{
//construct components
JTextPane textPane = new JTextPane();
//initialize data in arrays
public void readFile()
{
BufferedReader br = null;
String[] custID = new String[100];
String[] firstName = new String[100];
String[] lastName = new String[100];
String[] addressOne = new String[100];
String[] addressTwo = new String[100];
String[] myCity = new String[100];
String[] myState = new String[100];
String[] myCountry = new String[100];
String[] productionID = new String[100];
int num = 0;
try
{
br = new BufferedReader(new FileReader("Database.txt"));
String line = null;
while ((line = br.readLine()) != null)
{
String[] values = line.split(",");
if (values.length > 1)
{
custID[num] = values[0];
firstName[num] = values[1];
lastName[num] = values[2];
addressOne[num] = values[3];
addressTwo[num] = values[4];
myCity[num] = values[5];
myState[num] = values[6];
myCountry[num] = values[7];
productionID[num] = values[8];
num++;
}
}
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
//construct instance of CustomerInfo
public CustomerInfo()
{
super("Customer Information Database");
}
//create the menu system
public JMenuBar createMenuBar()
{
//create an instance of the menu
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
//construct and populate the File menu
JMenu mnuFile = new JMenu("File", true);
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
//construct and populate the Edit menu
JMenu mnuEdit = new JMenu("Edit", true);
mnuEdit.setMnemonic(KeyEvent.VK_E);
mnuEdit.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuEdit);
JMenu mnuEditSearch = new JMenu("Search");
mnuEditSearch.setMnemonic(KeyEvent.VK_R);
mnuEditSearch.setDisplayedMnemonicIndex(3);
mnuEdit.add(mnuEditSearch);
JMenuItem mnuEditSearchByName = new JMenuItem("by Last Name");
mnuEditSearchByName.setMnemonic(KeyEvent.VK_L);
mnuEditSearchByName.setDisplayedMnemonicIndex(3);
mnuEditSearch.add(mnuEditSearchByName);
mnuEditSearchByName.setActionCommand("last");
mnuEditSearchByName.addActionListener(this);
JMenuItem mnuEditSearchByProdNum = new JMenuItem("by Product Number");
mnuEditSearchByProdNum.setMnemonic(KeyEvent.VK_P);
mnuEditSearchByProdNum.setDisplayedMnemonicIndex(3);
mnuEditSearch.add(mnuEditSearchByProdNum);
mnuEditSearchByProdNum.setActionCommand("production");
mnuEditSearchByProdNum.addActionListener(this);
JMenuItem mnuEditSearchByState = new JMenuItem("by State");
mnuEditSearchByState.setMnemonic(KeyEvent.VK_S);
mnuEditSearchByState.setDisplayedMnemonicIndex(3);
mnuEditSearch.add(mnuEditSearchByState);
mnuEditSearchByState.setActionCommand("state");
mnuEditSearchByState.addActionListener(this);
return mnuBar;
}
//create the content pane
public Container createContentPane()
{
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
//create the JTextPane and center panel
JPanel centerPanel = new JPanel();
setTabsAndStyles(textPane);
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500, 200));
centerPanel.add(scrollPane);
//create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
return c;
}
//method to create tab stops and set font styles
protected void setTabsAndStyles(JTextPane textPane)
{
//create Tab Stops
TabStop[] tabs = new TabStop[2];
tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
TabSet tabset = new TabSet(tabs);
//set Tab Style
StyleContext tabStyle = StyleContext.getDefaultStyleContext();
AttributeSet aset = tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
textPane.setParagraphAttributes(aset, false);
//set Font Style
Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", fontStyle);
StyleConstants.setFontFamily(fontStyle, "SansSerif");
Style s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, true);
s = textPane.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);
}
//method to add new text to the JTextPane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//insert title
doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
//insert detail
for (int j = 0; j < lastName.length; j++)
{
doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
}
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
return textPane;
}
//event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
//user clicks Exit on the File menu
if (arg == "Exit")
System.exit(0);
//user clicks title on the Search submenu
if (arg == "last")
search(arg, last);
//user clicks studio on the Search submenu
if (arg == "production")
search(arg, production);
//user clicks state on the Search submenu
if (arg == "state")
search(arg, state);
}
//method to search arrays
public void search(String searchField, String searchArray[])
{
try
{
Document doc = textPane.getDocument(); //assign text to document object
doc.remove(0,doc.getLength()); //clear previous text
//display column titles
doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
//prompt user for search data
String search = JOptionPane.showInputDialog(null, "Please enter the "+ searchField);
boolean found = false;
//search arrays
for (int i = 0; i < lastName.length; i++)
{
if (search.compareTo(searchArray[i])==0)
{
doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
found = true;
}
}
if (found == false)
{
JOptionPane.showMessageDialog(null, "Your search produced no results.", "No results found",JOptionPane.INFORMATION_MESSAGE);
}
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
}
//main method executes at run time
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
CustomerInfo f = new CustomerInfo();
f.readFile();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}
}
I am getting an error starting at line 205 with the variable "lastName". I am getting the 'cannont resovle symbol' error with this variable amd the other array variable I had set up to read the columns of a text file. I am thinking that since the technique of reading the text file is embedded in its own prodecure that maybe the variables are not recongized by other procedures.
I would appreciate any help that anyone can provide. I am at a loss as to what to do. Here is a copy of the text file I am using. Thanks in advance.
CustID,FirstName,Last Name,Address1,Address2,City,State,Country,ProductID
101,William,Jones,1242 Elm St,,Chicago,IL,US,5462
102,Antoine,Sargeant,83 Main Ave,Apt 3A,Castle Rock,OR,US,13569
103,Linda,Montoya,51198 3rd Ave,,Brooklyn,NY,US,5462
104,Bernard,St John,Alwyn's Mews,128 Dennis Alley,London,,UK,9073-C
105,Knute,Olmstead,3135 Kirche,,Malmo,,SE,6215-G
106,Karen,Noyes,163 Morris AVe,,Denver,CO,US,45123
107,Sam,Ng,9081 Allard St,,San Lucia,CA,US,13569
108,Carl,Johnson,62161 3rd St,,Morningside,IN,US,5462
109,Janet,Smith,1531 Maple Grove Rd,,Maple Grove,MA,US,45123
110,Trevor,Beckwith,999 Archibald Ct,5G,Little Farthington,,UK,9073-B
111,Mark,Jonson,15245 Lingle Terrace,,Two Forks,AL,US,5462
112,Millie,Dingle,16 Sixth St,Ste. 11345,Austin,TX,US,5498
113,David,Winegard,7045 Portman Dr.,,East Millersville,OH,US,13569
114,Nathan,MacDonald,61 Freedom Pl,,Carthage,MD,US,45123
115,Cathy,Walton,2 Main St,,Four Mile, SD,US,5462
116,Stephen,Morrisson,712 Lovely Dr,,Allard,MO,US,5462
117,Jacques,Millefois,77B Ave Fouchard,,Arles,,FR,6215-A
118,Andy,Dorn,359 Park Ln,,Memphis,TN,US,13569
119,Tonya,Williams,793 Forest Dr,213,Dallas,Morris Glen,NJ,US,5462