如何使用数组而不是数组列表使用用户输入创建对象

时间:2015-08-20 08:27:59

标签: java arrays bluej

当我编译并执行时,我得到java.lang.ArrayOutOfBoundsException。我如何解决它?必须使用数组而不是arrayList。这是BlueJ。我将从桌面和笔记本电脑继承这些方法,这两个类正在计算机中获取父类的值。

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!

1 个答案:

答案 0 :(得分:0)

如果用户选择3您正在创建新计算机,笔记本电脑对象,然后显示信息,我删除了这些行,您的代码将生成ArrayIndexOutOfBoundsException,现在此代码没有错误,其余的是由你决定,享受。

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

}

}