Zee Jay Zee Jay - 11 days ago 5
Java Question

Previous Input gets overwritten by Newer Input

Hi I really need help with my program, I have searched high and low for a solution but can't seem to find what I'm looking for.

I'm making a program where the user adds a desktop, enters various information for it and then it gets added in an array list.

Here's the code:

Intro:

Scanner scan = new Scanner(System.in);
String input;
boolean looper = true;
DecimalFormat f = new DecimalFormat("#.00");
ArrayList<Desktop> desktopList = new ArrayList<>();
ArrayList<Laptop> laptopList = new ArrayList<>();

while (looper) {
System.out.println("");
System.out.println("******************* Artificial Intelligence Co. *************************");
System.out.println("1. Add Information for new Desktop");
System.out.println("2. Add Information for new Laptop");
System.out.println("3. Display all computer information");
System.out.println("4. Quit");
System.out.println("5. Credits");
System.out.println("*************************************************************************");


Switch Statement and Case 1:

switch (input) {
case "1":
System.out.println("");
System.out.println("=========================================================================");
System.out.println("Information for new Desktop");
System.out.println("=========================================================================");

Desktop xx = new Desktop();
boolean loop = true;


while (loop) {
System.out.print("What is the Computer ID: ");
xx.setComputerID(scan.nextLine().toUpperCase());

if ((xx.getComputerID().startsWith("D")) && (xx.getComputerID().length() == 4)) {
loop = false;
} else {
System.out.println("Computer ID should start with a letter \"D\". and have 4 characters.");
System.out.println("");
}
}

loop = true;
while (loop) {
System.out.print("What is the Processor Speed: ");
xx.setCPUspeed(scan.nextLine().toUpperCase());
try {
if (StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 2))) ||
StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 3)))) { //checks the value before GHZ or HZ if its a double
if (xx.getCPUspeed().endsWith("GHZ") || xx.getCPUspeed().endsWith("HZ")) {
loop = false;
} else {
System.out.println("CPU Speed input should end with \"GHZ\" or \"HZ\".");
System.out.println("");
}
} else {
System.out.println("CPU Speed input should contain a decimal or number followed by a \"GHZ\" or a \"HZ\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("CPU Speed input should contain a decimal or number followed by a \"GHZ\" or a \"HZ\".");
System.out.println("");
}

}

loop = true;
while (loop) {
System.out.print("What is the RAM: ");
xx.setRAM(scan.nextLine().toUpperCase());
try {
if (StringisInteger(xx.getRAM().substring(0, (xx.getRAM().length() - 2)))) { //checks the value if it is numeric and ending with GB or MB
if (xx.getRAM().endsWith("GB") || xx.getRAM().endsWith("MB")) {
loop = false;
} else {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}
} else {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}


}

loop = true;
while (loop) {
System.out.print("What is the Harddisk size: ");
xx.setHarddisk(scan.nextLine().toUpperCase());
try {
if (StringisInteger(xx.getHarddisk().substring(0, (xx.getHarddisk().length() - 2)))) { //checks the value if it is numeric and ending with GB or MB
if (xx.getHarddisk().endsWith("GB") || xx.getHarddisk().endsWith("TB")) {
loop = false;
} else {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
} else {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
}


loop = true;
while (loop) {
System.out.print("What is the Monitor: ");
xx.setMonitor(scan.nextLine().toUpperCase());
if (xx.getMonitor().equals("CRT") || xx.getMonitor().equals("LCD")) {
loop = false;
} else {
System.out.println("Please enter in CRT or LCD only.");
System.out.println("");
}
}

loop = true;
while (loop) {
try {
System.out.print("What is the price: $");
xx.setPrice(Double.parseDouble(scan.nextLine()));
loop = false;
} catch (NumberFormatException e) {
System.out.println("Price input should be numeric.");
System.out.println("");
}
}

desktopList.add(xx);

System.out.println("Information successfully added.");
System.out.println("");
System.out.println("");
break;


Case 3, where user gets to see what he/she enters:

case "3":
int DesktopCounter = 1;
int LaptopCounter = 1;

System.out.println("");
if (desktopList.isEmpty()) {
System.out.println("No desktop added!");
System.out.println("");
} else {
for (int i = 0; i < desktopList.size(); i++) {
System.out.println("");
System.out.println("Desktop " + DesktopCounter);
System.out.println("Computer ID: " + desktopList.get(i).getComputerID());
System.out.println("Processor Speed: " + desktopList.get(i).getCPUspeed());
System.out.println("RAM: " + desktopList.get(i).getRAM());
System.out.println("Harddisk:" + desktopList.get(i).getHarddisk());
System.out.println("Monitor: " + desktopList.get(i).getMonitor());
System.out.println("Price: $" + f.format(desktopList.get(i).getPrice()));
DesktopCounter++;
}
}
break;


Desktop Class:

public class Desktop extends Computer //Child class of Computer
{
private static String Monitor;

public Desktop()
{
ComputerID = "-- No ID specified --";
CPUspeed = "-- No processor speed specified --";
RAM = "-- No RAM specified-";
Harddisk = "-- No Harddisk size specified --";
Monitor = "-- No Monitor specified --";
Price = 0.0;
}



//Setters and Getters
public String getMonitor()
{
return Monitor;
}

public void setMonitor(String monitor)
{
Monitor = monitor;
}


}


Computer Class:

public class Computer //Parent class

{
protected static String ComputerID;
protected static String CPUspeed;
protected static String RAM;
protected static String Harddisk;
protected static double Price;


public Computer() //Initializer
{
ComputerID = "-- No ID specified --";
CPUspeed = "-- No processor speed specified --";
RAM = "-- No amount RAM specified-";
Harddisk = "-- No Harddisk size specified --";
Price = 0.0;
}

public Computer(String computerID, String cpuspeed, String ram, String harddisk, double price) {
ComputerID = computerID;
CPUspeed = cpuspeed;
RAM = ram;
Harddisk = harddisk;
Price = price;


}


//Getters and Setters
public String getComputerID() {
return ComputerID;
}

public void setComputerID(String computerID) {
ComputerID = computerID;
}

public String getCPUspeed() {
return CPUspeed;
}

public void setCPUspeed(String cpuspeed) {
CPUspeed = cpuspeed;
}

public String getRAM() {
return RAM;
}

public void setRAM(String ram) {
RAM = ram;
}

public String getHarddisk() {
return Harddisk;
}

public void setHarddisk(String harddisk) {
Harddisk = harddisk;
}

public double getPrice() {
return Price;
}

public void setPrice(double price) {
Price = price;
}

//End of getters and setters


}

Now say if I add a Desktop using case 1 with the following information entered in:


  • Computer ID: D001

  • Processor Speed: 3.2GHZ

  • RAM: 512MB

  • Harddisk: 80GB

  • Monitor: CRT



And then proceed to add another Desktop with these:


  • Computer ID: D123

  • Processor Speed: 4.4GHZ

  • RAM: 8GB

  • Harddisk: 1TB

  • Monitor: LCD



When I display the information using the case 3 code block, it outputs:


  • Desktop 1

  • Computer ID: D123

  • Processor Speed: 4.4GHZ

  • RAM: 8GB

  • Harddisk: 1TB

  • Monitor: LCD







  • Desktop 2

  • Computer ID: D123

  • Processor Speed: 4.4GHZ

  • RAM: 8GB

  • Harddisk: 1TB

  • Monitor: LCD



When by right, Desktop 1 should display its own unique attributes.

I would appreciate any help.

EDIT: I solved this problem by making my variables non-static.

Answer

update after extra code was posted

Your fields should not be static in your Desktop, Computer and Laptop class. Change these fields:

protected static String ComputerID;
protected static String CPUspeed;
protected static String RAM;
protected static String Harddisk;
protected static double Price;

to

protected String ComputerID;
protected String CPUspeed;
protected String RAM;
protected String Harddisk;
protected double Price;

static fields are the same for every object of a class. So if you have multiple Desktops and you declare the Price static, all Desktops will share the same price field. This is not what you want, obviously all Desktops have another price.

Also change

private static String Monitor;

to

private String Monitor;

You really want to have a seperate monitor per Computer and not share the same monitor for all computers.

old answer

The code runs perfectly here, so there must be some missing information: the problem must be in some part of the code you did not post:

******************* Artificial Intelligence Co. *************************
1. Add Information for new Desktop
2. Add Information for new Laptop
3. Display all computer information
4. Quit
5. Credits
*************************************************************************
3

Desktop 1
Computer ID: D123
Processor Speed: 2GHZ
RAM: 2GB
Harddisk:1TB
Monitor: CRT
Price: $100.00

Desktop 2
Computer ID: D002
Processor Speed: 2GHZ
RAM: 16GB
Harddisk:2TB
Monitor: CRT
Price: $500.00

Some thoughts:

  • you did not post the Desktop class, if the fields in the Desktop class are declared static, that would explain the results, make sure they are not declared static
  • it looks like you are using the same variable somewhere e.g. Desktop xx = new Desktop(); is not in the case statement as is in the code above
  • you might be using a wrong index to get the item of the list, you are looping with a for-i loop and do desktopList.get(i) if you happen to do desktopList.get(0) or desktopList.get(someVariableThatIsAlwaysZero) you will always print the same result. It's safer to use the new foreach syntax: for (Desktop desktop : desktopList){ ... }
  • you have to learn to use the debugger, go step by step through the code and see where the duplicate values are inserted, and if no duplicate values are inserted, see where the print statements are printing and why they print the same value; using a debugger is really important to find problems
  • your code might be out of sync

I will just give you my version that works, so you might want to compare:

public static void main(String args[]) {

        Scanner scan = new Scanner(System.in);
        String input;
        boolean looper = true;
        DecimalFormat f = new DecimalFormat("#.00");
        ArrayList<Desktop> desktopList = new ArrayList<>();
        ArrayList<Laptop> laptopList = new ArrayList<>();

        while (looper) {
            System.out.println("");
            System.out.println("******************* Artificial Intelligence Co. *************************");
            System.out.println("1. Add Information for new Desktop");
            System.out.println("2. Add Information for new Laptop");
            System.out.println("3. Display all computer information");
            System.out.println("4. Quit");
            System.out.println("5. Credits");
            System.out.println("*************************************************************************");

            input = scan.nextLine();

            switch (input) {
                case "1":
                    System.out.println("");
                    System.out.println("=========================================================================");
                    System.out.println("Information for new Desktop");
                    System.out.println("=========================================================================");

                    Desktop xx = new Desktop();
                    boolean loop = true;


                    while (loop) {
                        System.out.print("What is the Computer ID: ");
                        xx.setComputerID(scan.nextLine().toUpperCase());

                        if ((xx.getComputerID().startsWith("D")) && (xx.getComputerID().length() == 4)) {
                            loop = false;
                        } else {
                            System.out.println("Computer ID should start with a letter \"D\". and have 4 characters.");
                            System.out.println("");
                        }
                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the Processor Speed: ");
                        xx.setCPUspeed(scan.nextLine().toUpperCase());
                        try {
                            if (StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 2))) ||
                                    StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 3)))) {   //checks the value before GHZ or HZ if its a double
                                if (xx.getCPUspeed().endsWith("GHZ") || xx.getCPUspeed().endsWith("HZ")) {
                                    loop = false;
                                } else {
                                    System.out.println("CPU Speed input should end with \"GHZ\" or \"HZ\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                            System.out.println("");
                        }

                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the RAM: ");
                        xx.setRAM(scan.nextLine().toUpperCase());
                        try {
                            if (StringisInteger(xx.getRAM().substring(0, (xx.getRAM().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                                if (xx.getRAM().endsWith("GB") || xx.getRAM().endsWith("MB")) {
                                    loop = false;
                                } else {
                                    System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                            System.out.println("");
                        }


                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the Harddisk size: ");
                        xx.setHarddisk(scan.nextLine().toUpperCase());
                        try {
                            if (StringisInteger(xx.getHarddisk().substring(0, (xx.getHarddisk().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                                if (xx.getHarddisk().endsWith("GB") || xx.getHarddisk().endsWith("TB")) {
                                    loop = false;
                                } else {
                                    System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                            System.out.println("");
                        }
                    }


                    loop = true;
                    while (loop) {
                        System.out.print("What is the Monitor: ");
                        xx.setMonitor(scan.nextLine().toUpperCase());
                        if (xx.getMonitor().equals("CRT") || xx.getMonitor().equals("LCD")) {
                            loop = false;
                        } else {
                            System.out.println("Please enter in CRT or LCD only.");
                            System.out.println("");
                        }
                    }

                    loop = true;
                    while (loop) {
                        try {
                            System.out.print("What is the price: $");
                            xx.setPrice(Double.parseDouble(scan.nextLine()));
                            loop = false;
                        } catch (NumberFormatException e) {
                            System.out.println("Price input should be numeric.");
                            System.out.println("");
                        }
                    }

                    desktopList.add(xx);

                    System.out.println("Information successfully added.");
                    System.out.println("");
                    System.out.println("");
                    break;

                case "3":
                    int DesktopCounter = 1;
                    int LaptopCounter = 1;

                    System.out.println("");
                    if (desktopList.isEmpty()) {
                        System.out.println("No desktop added!");
                        System.out.println("");
                    } else {
                        for (int i = 0; i < desktopList.size(); i++) {
                            System.out.println("");
                            System.out.println("Desktop " + DesktopCounter);
                            System.out.println("Computer ID: " + desktopList.get(i).getComputerID());
                            System.out.println("Processor Speed: " + desktopList.get(i).getCPUspeed());
                            System.out.println("RAM: " + desktopList.get(i).getRAM());
                            System.out.println("Harddisk:" + desktopList.get(i).getHarddisk());
                            System.out.println("Monitor: " + desktopList.get(i).getMonitor());
                            System.out.println("Price: $" + f.format(desktopList.get(i).getPrice()));
                            DesktopCounter++;
                        }
                    }
                    break;
            }
        }
    }

    private static boolean StringisInteger(String substring) {return true;}

    private static boolean StringisDouble(String substring) { return true; }

My Desktop class looks like this:

@Data
public class Desktop {

    private double price;
    private String computerID;
    private String CPUspeed;
    private String RAM;
    private String harddisk;
    private String monitor;

}

Where @Data is a Lombok annotation that generates setters and getters. As you can see, the fields are not static.

Comments