Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串打印不正确时出错_Java_String_Format_String Formatting - Fatal编程技术网

Java 字符串打印不正确时出错

Java 字符串打印不正确时出错,java,string,format,string-formatting,Java,String,Format,String Formatting,我的代码应该读取一个.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; St

我的代码应该读取一个.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());
    }
文本文件的外观

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
我去掉了静电,但现在在计算机课的一个方法中,它要求我把静电放回去。EclipseIDE告诉我为制造商等恢复静态

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);

考虑到您的读取循环工作正常并且正在正确读取文件,唯一可以得出的结论是
计算机
正在使用
静态
字段来存储分配给它的详细信息

这意味着
计算机
的每个实例将具有彼此完全相同的信息

例如,我想象你的
计算机
类看起来像

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) {...
删除“应该是什么实例”字段上的
静态
清除,这样,
计算机
的每个实例都将具有这些属性的unquie值

更新-
静态
方法

由于字段不再是
静态
,因此您必须从方法中删除
静态
修饰符,例如

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);

}
和/或提供
计算机
参考作为参数

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);

}

使用errorwhile(input.hasNext()){//change to input.hasNextLine()发布的整个代码已更改但仍打印出与您的
计算机
类相同的邮件…my Computer classi已删除静态,并且可以正常工作,但现在它在计算机类中导致另一个错误,这是因为您有一个名为
displayComputer
静态
方法。删除
静态
引用或修改它以接受实例电脑的…好的,我没看到。谢谢你的帮助。这是两个问题的答案
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);

}