double for for循环通过java数组,但有异常

时间:2017-02-16 20:56:39

标签: java arrays for-loop

我试图循环数组两次,一次创建一个对象数组,然后在创建数组期间,检查以确保该地址在另一个数组中不存在。这适用于Java II类。我已将默认的equals函数重写为比较函数,检查streetAddress字段以查看它们是否匹配。

我试图避免的问题是,首先,我创建一个NewspaperSubscriber对象,然后检查以确保它不存在,但是如果它已经存在,它将永远存在已经创建了!我试图在我的第二个循环中找到一种方法(在y上递增)以跳过我当前正在其中处理父元素循环的任何元素(在x上递增)。

现在对它进行一段时间的处理,我的例外很难。我不想修改我的NeswpaperSubscriber类,因为需要将NeswpaperSubscriber.String与NewspaperSubscriber.String进行比较,而不是将NewspaperSubscriber.String与String 进行比较。想法?

import javax.swing.*;
import java.util.Scanner;

public class Subscribers {
    static int x, y;
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        NewspaperSubscriber[] subscribers= new NewspaperSubscriber[5];

        for (x = 0; x < subscribers.length; ++x) {
            System.out.println("x = " + x);
            String userEntry;
            int subscriberType;
            String address;

            userEntry = JOptionPane.showInputDialog(null, "Please select the type of \n" +
                    "subscribers you want to enter: \n" +
                    "\t1 - Seven Day\n" +
                    "\t2 - Weekday\n" +
                    "\t3 - Weekend");
            subscriberType = Integer.parseInt(userEntry);

            if (subscriberType == 1) {
                address = addressEntry();
                subscribers[x] = new SevenDaySubsriber(address);
                checkAddress(subscribers, subscribers[x]);
            } else if (subscriberType == 2) {
                subscribers[x] = new WeekdaySubscriber(addressEntry());
            } else {
                subscribers[x] = new WeekendSubscriber(addressEntry());
            }

        }

        StringBuffer outString = new StringBuffer();
        for (x = 0; x < subscribers.length; ++x) {
            outString.append("\n#" + (x + 1) + " ");
            outString.append(subscribers[x].toString());
        }

        JOptionPane.showMessageDialog(null, " Our available subscribers include: \n" + outString);
    }
    public static String addressEntry() {
        String enteredAddress = JOptionPane.showInputDialog(null, "Please enter address");
        System.out.println(enteredAddress);
        return enteredAddress;
    }

    public static void checkAddress(NewspaperSubscriber[] subscribers, NewspaperSubscriber newSub) {
        for (y = 0; y < subscribers.length && y !=x; ++y) {
            System.out.println("x = " + x);
            System.out.println();
            System.out.println("y = " + y);
            System.out.println();
            if (subscribers[y].equals(newSub)) {
                JOptionPane.showMessageDialog(null, "That address already exists!");
            } else {

            }
        }
    }
}


//From base class Newspaper subscriber
     public boolean equals(NewspaperSubscriber otherSubscriber) {
    boolean result;
    if (streetAddress == otherSubscriber.streetAddress)
        result = true;
    else result = false;
    return result;

}

我的结果!

x = 0
123
x = 1
123
x = 2
123
x = 3
Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Subscribers.main(Subscribers.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

基本上我认识到以下问题:

问题1

  

我正在尝试在我的第二个循环中找到一种方法(在y上递增)以跳过我正在处理它的父循环中的任何元素(在x上递增)。

在进行检查之前,不要插入新元素。因此,您想要交换这两行:

subscribers[x] = new SevenDaySubsriber(address);
checkAddress(subscribers, subscribers[x]);

为了满足教师的奇怪要求,您可以将新订阅者的索引传递给checkAddress方法,然后跳过它。然而,这个限制本身是非常奇怪(不称之为糟糕的设计)。

public static void checkAddress(NewspaperSubscriber[] subscribers, NewspaperSubscriber newSub, int skipIndex) {
    for (int y = 0; y < subscribers.length; ++y) {
        if (y != skipIndex && subscribers[y].equals(newSub)) {
            JOptionPane.showMessageDialog(null, "That address already exists!");
    }
}

问题2

  

现在对它进行一段时间的处理,我的例外很难。

异常发生在

subscriberType = Integer.parseInt(userEntry);

您关闭(取消)弹出窗口,使JOptionPane.showInputDialog(...)返回null。这显然不是有效的Integer,这就是你得到上述异常的原因。

注意

在您的方法NewspaperSubscriber.equals(...)中,您应该使用String.equals(other)而不是==。有关详细信息,请参阅here