【问题标题】:how to create object using user input using array and not array list如何使用用户输入使用数组而不是数组列表创建对象
【发布时间】:2015-11-13 19:11:31
【问题描述】:

当我编译和执行时,我得到java.lang.ArrayOutOfBoundsException。我如何解决它?必须使用数组而不是数组列表。这是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!

【问题讨论】:

  • &lt;= desk.length 和其他地方 - 使用 &lt; 代替,数组在 Java 中是从零开始的
  • 现在我没有错误但是当我执行时我有所有错误作为输入。错误!错误!错误!错误!错误!错误!错误错误错误错误错误
  • 然后调试你的代码,不到3秒你就会发现问题。
  • @MarounMaroun 这是bluej,无法调试

标签: java arrays bluej


【解决方案1】:

如果用户选择 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"));

}

}

【讨论】:

  • 圈[j].getDisplayLapInfo();
  • hmm 无论如何,您是如何到达索引 100 的,您的数组大小为 5,这意味着每个数组最多可以包含 5 个对象,但不能更多!
  • 你想将多少笔记本电脑对象添加到你的膝上阵列?!!
  • 好吧,我做了一些更改,似乎没有更多错误。但显示全部来自 System.out.println。不是来自我输入的数据
  • 好吧,我只添加了笔记本电脑和台式机的 1 个详细信息
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2016-11-03
相关资源
最近更新 更多