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

Recommended Answers

All 4 Replies

i can't seem to figure out where to add the description of the car

Is this a GUI design problem?
Does the file you posted have a description? What is the layout for the data in the file?
2 numbers on one line and text on the next.


Where do you want to display the description? In a label like you are doing with the price?

Is this a GUI design problem?
Does the file you posted have a description? What is the layout for the data in the file?
2 numbers on one line and text on the next.


Where do you want to display the description? In a label like you are doing with the price?

its a GUI program.I posted the text file just as it appeares.
2 numbers in one line then a string line.
the discription is displayed in a gui jframe that,with the scroll pane for the product.when u select an item on the scroll pen it displays price and discription of the car.so yes in a label!
thanks

You have the logic for displaying the price.
Write some more logic just like it to display the description.

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.