I have the folowing code so far which outputs the make and price of a car,but i cant seem to figure out were to add the discritpion of the car so that it can also be diplayed.HELP! i tried adding
discription[k] = st.nextToken();
but the program did not run.
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Lab22V3 extends JFrame implements ListSelectionListener
{
String []products = new String[100];
double []prices = new double[100];
JLabel unitPrice;
JList productList;
public static void main(String []args)
{
Lab22V3 x = new Lab22V3();
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setTitle("Lab#22 Version#3");
x.setSize(350, 250);
x.setVisible(true);
} // main
public Lab22V3() // constructor
{
String s;
StringTokenizer st;
int k=0;
try
{
BufferedReader inFile = new BufferedReader(new FileReader("Lab22data.txt"));
while ((s = inFile.readLine()) != null)
{
st = new StringTokenizer(s);
products[k] = st.nextToken();
prices[k] = Double.valueOf(st.nextToken());
++k;
} // while not EOF
inFile.close();
} catch (Exception e) { System.err.println(e); }
unitPrice = new JLabel();
getContentPane().add(unitPrice, BorderLayout.NORTH);
JPanel p = new JPanel();
productList = new JList(products); // create a JList object
productList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane sp = new JScrollPane(productList); // adding scrolling capability
p.add(sp);
getContentPane().add(p, BorderLayout.SOUTH);
// Event registration
productList.addListSelectionListener(this);
} // constructor
// Event handling
public void valueChanged(ListSelectionEvent event)
{
unitPrice.setText(" " + prices[productList.getSelectedIndex()]);
} // valueChanged
} // Lab22V3
and the text file is
1000 100.00
A dozen of jokes
2000 5.91
Used BMW
3000 19.28
Cookie Jar
4000 21.90
Birthday Cake
5000 35.28
Used keyboard
6000 50.00
Love
7000 92.27
Gone with Wind