如何将字符串转换为大量列表,

时间:2013-03-19 23:43:32

标签: java list netbeans arraylist

如何分隔要使用的字符串, 代码摘录

JTextField stockNo = new JTextField(7);
JTextField quantity = new JTextField(7);
TextArea information = new TextArea(5, 50);
JButton purchaseBtn = new JButton("Purchase");
purchaseBtn.addActionListener(this);
//set up gui ect
String key = stockNo.getText();
String quantityTxt = quantity.getText();
String info = "\n\nName: " + StockData.getName(key) + 
                    "\nPrice: £" + (StockData.getPrice(key)) + 
                    "\nNumber in stock: " + StockData.getQuantity(key) +
                    "\nNumber to buy: " + quantityTxt;
information.append(info);

当用户点击purchaseBtn时,之前的信息应该留在.append,我需要的是以某种方式从信息文本区域中提取信息,并使用它进行计算,有人告诉我arraylist但没有想法。

例如

 //in text area
Name: shirt //user inputed 1 for key
Price: £5.00
Number in stock: 15
Number to buy 7 // user inputed 7 for quantity

Name: socks //user inputed 2 for key
Price: £2.50
Number in stock: 4
Number to buy 1 // user inputed 7 for quantity

Name: trouser //user inputed 3 for key
Price: £24.00
Number in stock: 19
Number to buy 4 // user inputed 7 for quantity

我需要保存每行的密钥和数量,以便我以后可以用它来做事,并且可以说int totalcost = //the price of all of them added together in our example £31.50

1 个答案:

答案 0 :(得分:0)

您使用的是OO语言。您应该在对象中管理所有信息,这就是重点。

使用JList代替JTextArea

将您的购买信息放在Purchase对象中,该对象包含您需要的所有信息,例如nameprice, inStock and toBuy`。

然后,您可以根据需要使用ListCellRenderer呈现Purchase对象的各个元素。

这将使管理变得更加容易。

有关详细信息,请参阅How to use Lists