我的代码应该读取.txt文件并将其传输到字符串。我的代码正在读取文件但由于某种原因它只保存最后一行。
public static void main(String[] args){
javax.swing.UIManager.put("OptionPane.messageFont",new FontUIResource(new Font("Courier New", Font.BOLD, 16)));
String userOption = null;
String fileName = "computers.txt";
String allComputers = "";
String [] allComputer = new String[50];
String usermanufacturer = null;
String manufacturer = null;
String userCpu = null;
String cpu = null;
String userPrice = null;
String price = null;
float userPricef = 0;
String [] computersText = new String[50];
Computer [] computerArray = new Computer[50];
boolean bContinue = false;
int i=0;
/**
*
*/
try
{
Scanner input = new Scanner(new File(fileName));
while (input.hasNext()){
computersText[i] = input.nextLine();
String [] Individual = computersText[i].split(":");
computerArray[i] = new Computer(Individual[0], Individual[1], Integer.parseInt(Individual[2]), Integer.parseInt(Individual[3]), Individual[4], Individual[5], Individual[6], Float.parseFloat(Individual[7]));
i++;
}
}
catch(FileNotFoundException e)
{
System.out.println("Could not open file " + fileName);
System.exit(200);
}
for(int j=0; j<i; j++){
allComputers += String.format("%-25s %-20s %-8s %-7s %-7s %-7s %-7s %10.2f \n", computerArray[j].getManufacturer(), computerArray[j].getModel(), computerArray[j].getMemory(), computerArray[j].getDisk(), computerArray[j].getCPU(), computerArray[j].getOptical() ,computerArray[j].getOS() , computerArray[j].getRetailPrice());
allComputer[j] = String.format("%-25s %-20s %-8s %-7s %-7s %-7s %-7s %10.2f \n", computerArray[j].getManufacturer(), computerArray[j].getModel(), computerArray[j].getMemory(), computerArray[j].getDisk(), computerArray[j].getCPU(), computerArray[j].getOptical() ,computerArray[j].getOS() , computerArray[j].getRetailPrice());
}
用allComputers打印出for循环的内容
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x
文本文件的样子
Dell Computers Inc.:Inspiron 15 Touch:6:500:Intel Core i5:CD/DVD+-RW:Windows 8.1:649.99
Dell Computers Inc.:Inspiron 17:4:500:Intel Core i3:CD/DVD+-RW:Windows 7:549.99
Dell Computers Inc.:Alienware 18:16:1000:Intel Core i7:Dual Layer Blu-ray:Windows 7:2999.99
Acer Computers Inc.:Aspire AT3-600:6:2000:Intel Core i5:BlueRay:Windows 8:599.99
Apple Computers Inc.:MacBook Pro:4:500:Intel Core i5:None:Mac OS x:1199.00
Apple Computers Inc.:21.5-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x:1299.00
Apple Computers Inc.:27-inch iMac:8:1000:Intel Core i5:USB SuperDrive:Mac OS x:1799.00
我取出了静态但现在在计算机类的一个方法中它要求我放回静态。日食IDE告诉我为制造商等放回静态。
public static void displayComputer(){
UIManager.put("OptionPane.messageFont",new FontUIResource(new Font("Courier New", Font.BOLD, 16)));
JOptionPane.showMessageDialog(null, "Inventory Computer Detail \n\n" +
"Manufacturer: " + manufacturer +
"\nModel Name: " + model+
"\nMemory Size: " +memory+
" GB \nDisk Size: " +disk+
" GB \nCPU Type: " +cPU+
"\nOptical Drive: " + optical+
"\nOS Version: " + oS+
"\nRetail Price: $" + String.format("%.2f",retailPrice),
"Geekazoid Inc.", JOptionPane.INFORMATION_MESSAGE);
答案 0 :(得分:2)
鉴于您的读取循环正常并正在正确读取文件这一事实,唯一可以得出的结论是Computer
正在使用static
字段来存储您为其分配的详细信息。< / p>
这意味着Computer
的每个实例将具有彼此完全相同的信息。
例如,我想你Computer
类看起来像......
public class Computer {
private static String manufacturer;
private static String model;
private static String memory;
private static String disk;
private static String cpu;
private static String optical;
private static String os;
private static String price;
public Computer(
String manufacturer,
String model,
String memory,
String disk,
String cpu,
String optical,
String os,
String price) {...
删除应该是实例字段的static
declearations,这样,Computer
的每个实例都会有属性的unquie值...
已更新 - static
方法
由于字段不再是static
,您必须从方法中删除static
修饰符,例如...
public void displayComputer() {
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("Courier New", Font.BOLD, 16)));
JOptionPane.showMessageDialog(null, "Inventory Computer Detail \n\n"
+ "Manufacturer: " + manufacturer
+ "\nModel Name: " + model
+ "\nMemory Size: " + memory
+ " GB \nDisk Size: " + disk
+ " GB \nCPU Type: " + cPU
+ "\nOptical Drive: " + optical
+ "\nOS Version: " + oS
+ "\nRetail Price: $" + String.format("%.2f", retailPrice),
"Geekazoid Inc.", JOptionPane.INFORMATION_MESSAGE);
}
和/或提供Computer
引用作为参数...
public static void displayComputer(Computer computer) {
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("Courier New", Font.BOLD, 16)));
JOptionPane.showMessageDialog(null, "Inventory Computer Detail \n\n"
+ "Manufacturer: " + computer.getManufacturer()
+ "\nModel Name: " + computer.getModel()
+ "\nMemory Size: " + computer.getMemory()
+ " GB \nDisk Size: " + computer.getDisk()
+ " GB \nCPU Type: " + computer.getCPU()
+ "\nOptical Drive: " + computer.getOptical()
+ "\nOS Version: " + computer.getOS()
+ "\nRetail Price: $" + String.format("%.2f", computer.getRetailPrice()),
"Geekazoid Inc.", JOptionPane.INFORMATION_MESSAGE);
}