简体   繁体   中英

how to create object using user input using array and not array list

When i compile and execute, I get java.lang.ArrayOutOfBoundsException . How do i fix it? Must use array and not arrayList. This is BlueJ . I'm inheriting the methods from desktop and laptop and those 2 classes are getting the values of parent class in computer.


import java.util.Scanner;

public class testComputer {
    public static void main(String args[]) {
        String a;
        Scanner scanner = new Scanner(System.in);
        Desktop[] desk = new Desktop[5];
        Laptop[] lap = new Laptop[5];
        int Desk = 0;
        int Lap = 0;

        do {
            System.out.println("*************** Artificial Intelligence Co.***************");
            System.out.println("Computer Menu");
            System.out.println("1. Add a new Desktop Information");
            System.out.println("2. Add a new Laptop Information");
            System.out.println("3. Display all Computer Information");
            System.out.println("4. Quit");
            System.out.println("***********************************************************");
            System.out.print("Please enter either 1 to 4: ");
            a = scanner.nextLine();

            if (a.equals("1")) {
                desk[Desk] = new Desktop();
                desk[Desk].setDisplayDesktopInfo();
            } else if (a.equals("2")) {
                lap[Lap] = new Laptop();
                lap[Lap].setDisplayLaptopInfo();
            } else if (a.equals("3")) {
                int i = 0;
                for (i = 0; i <= desk.length; i++) {
                    if (desk.length == i) {
                        System.out.println("================Desktop================");
                        desk[i] = new Desktop();
                        desk[i].getDisplayDeskInfo();
                        System.out.println("");
                    } else {
                        System.out.println("Error!");
                    }
                }

                int j = 0;
                for (j = 0; j <= lap.length; j++) {
                    if (lap.length == j) {
                        System.out.println("================Laptop================");
                        lap[j] = new Laptop();
                        lap[j].getDisplayLapInfo();
                        System.out.println("");
                    } else {
                        System.out.println("Error");
                    }
                }
            } else if (a.equals("4")) {
                System.out.println("Good Bye!");
            }
        } while (!a.equals("4"));

    }
}

import java.util.Scanner;
public class Desktop extends Computer
{    
private static final String alphanumeric_regex_pattern = "^[a-zA-Z0-9]*$";
private String Monitor;

public Desktop()
{

}
void setMonitor(String monitor)
{
    Monitor = monitor;
}
String getMonitor()
{
    return Monitor;
}

void setDisplayDesktopInfo()
{
    Scanner scanner = new Scanner(System.in);
    System.out.println("========================================");
    System.out.println("Information for new Desktop");
    System.out.println("========================================");
    System.out.print("What is the Computer ID: " + "");
    setComputerID(scanner.nextLine());
    System.out.print("What is the Processor Speed: " + "");
    setprocessorSpeed(scanner.nextLine());
    System.out.print("What is the RAM: " + "");
    setRAM(scanner.nextLine());
    System.out.print("What is the Harddisk size: " + "");
    setHarddisk(scanner.nextLine());
    System.out.print("What is the Monitor Type: " + "");
    setMonitor(scanner.nextLine());
    System.out.println("");
    System.out.println("Your information has been added successfully.");
    System.out.print("");
}
void getDisplayDeskInfo()
{
    System.out.println("Computer ID: " + getComputerID());
    System.out.println("Processor Speed: " + getprocessorSpeed());
    System.out.println("RAM: " + getRAM());
    System.out.println("Harddisk: " + getHarddisk());
    System.out.println("Monitor: " + getMonitor());
    System.out.print("");
}

}

import java.util.Scanner;
public class Laptop extends Computer
{
private String Weight;

void setWeight(String weight)
{
    Weight = weight;
} 
String getWeight()
{
    return Weight;
}

void setDisplayLaptopInfo()
{
    Scanner scanner = new Scanner(System.in);
    System.out.println("========================================");
    System.out.println("Information for new Laptop");
    System.out.println("========================================");
    System.out.print("What is the Computer ID: " + "");
    setComputerID(scanner.nextLine());
    System.out.print("What is the Processor Speed: " + "");
    setprocessorSpeed(scanner.nextLine());
    System.out.print("What is the RAM: " + "");
    setRAM(scanner.nextLine());
    System.out.print("What is the Harddisk size: " + "");
    setHarddisk(scanner.nextLine());
    System.out.print("What is the Weight: " + "");
    setWeight(scanner.nextLine());
    System.out.println("");
    System.out.println("Your information has been added successfully.");
    System.out.print("");
}
void getDisplayLapInfo()
{
    System.out.println("Computer ID: " + getComputerID());
    System.out.println("Processor Speed: " + getprocessorSpeed());
    System.out.println("RAM: " + getRAM());
    System.out.println("Harddisk: " + getHarddisk());
    System.out.println("Weight: " + getWeight());
    System.out.print("");
}

}

*************** Artificial Intelligence Co.***************
Computer Menu
1. Add a new Desktop Information
2. Add a new Laptop Information
3. Display all Computer Information
4. Quit
***********************************************************
Please enter either 1 to 4: 1
========================================
Information for new Desktop
========================================
What is the Computer ID: D001
What is the Processor Speed: 3.3GHZ
What is the RAM: 4GB
What is the Harddisk size: 80GB
What is the Monitor Type: CRT

Your information has been added successfully.
*************** Artificial Intelligence Co.***************
Computer Menu
1. Add a new Desktop Information
2. Add a new Laptop Information
3. Display all Computer Information
4. Quit
***********************************************************
Please enter either 1 to 4: 3
If you see this message means you have not done yet!
If you see this message means you have not done yet!
If you see this message means you have not done yet!
If you see this message means you have not done yet!
If you see this message means you have not done yet!
Error
Error
Error
Error
Error
*************** Artificial Intelligence Co.***************
Computer Menu
1. Add a new Desktop Information
2. Add a new Laptop Information
3. Display all Computer Information
4. Quit
***********************************************************
Please enter either 1 to 4: 4
Good Bye!

If the user chooses 3 you are creating a new computer, laptop object before displaying the information, I removed those lines, and your code will obiously generate ArrayIndexOutOfBoundsException , now this code is error-free and the rest is up to you, Enjoy.

import java.util.Scanner;

public class testComputer {
    public static void main(String args[]) {
        String a;
        Scanner scanner = new Scanner(System.in);
        Desktop[] desk = new Desktop[5];
        Laptop[] lap = new Laptop[5];
        int Desk = 0;
        int Lap = 0;

        do {
            System.out.println("*************** Artificial Intelligence Co.***************");
        System.out.println("Computer Menu");
        System.out.println("1. Add a new Desktop Information");
        System.out.println("2. Add a new Laptop Information");
        System.out.println("3. Display all Computer Information");
        System.out.println("4. Quit");
        System.out.println("***********************************************************");
        System.out.print("Please enter either 1 to 4: ");
        a = scanner.nextLine();

        if (a.equals("1")) {
            desk[Desk] = new Desktop();
            desk[Desk].setDisplayDesktopInfo();
            ++Desk;
        } else if (a.equals("2")) {
            lap[Lap] = new Laptop();
            lap[Lap].setDisplayLaptopInfo();
            ++Lap;
        } else if (a.equals("3")) {
            int i = 0;
            for (i = 0; i < desk.length; i++) {

                    System.out.println("================Desktop================");

                    desk[i].getDisplayDeskInfo();
                    System.out.println("");

            }

            int j = 0;
            for (j = 0; j < lap.length; j++) {

                    System.out.println("================Laptop================");

                    lap[j].getDisplayLapInfo();
                    System.out.println("");

            }
        } else if (a.equals("4")) {
            System.out.println("Good Bye!");
        }
    } while (!a.equals("4"));

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM