Pregunta

My code is supposed to read a .txt file and transfer it to a string. My code is reading the file but for some reason it saves only the last line.

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

what it is printing out the for loop with allComputers

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

what the text file looks like

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

I took out the static but now in one of the method in the computer class it is asking me to put back static in. The eclipse IDE is telling me to put back static for manufacturer etc.

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);
¿Fue útil?

Solución

Given that fact that your read loop works and is reading the file correctly, the only conclusion that could be draw is that Computer is using static fields to store the details you are assigning it.

This means that each instance of Computer will have exactly the same information as each other.

For example, I imagine that you Computer class looks something like...

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) {...

Remove the static declearations on what should be instance fields, this way, each instance of Computer will have unquie values for there properties...

Updated - static method

Because the fields are no longer static, you will either have to remove the static modifier from the method, for example...

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

}

And/or provide a Computer reference as a parameter...

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

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top