-1

I am trying to loop through an array twice, once to create an array of Objects, and then during the creation of the array, check to make sure the address does not exist already in an another array. This is for a Java II class. I've overridden the default equals function to be a comparative one, checking the streetAddress field to see if they match.

The issue I'm trying to avoid is, that first, I create a NewspaperSubscriber object, then I check to make sure it doesn't already exist, however it will always exist if it's already been created! I'm trying to find a way in my second for loop (incrementing on y) to skip whatever element i'm currently processing in it's parent loop (incrementing on x).

Working on it for a while now, having a hard time with my exception. I don't want to modify my NeswpaperSubscriber class, as it needs to compare NeswpaperSubscriber.String to NewspaperSubscriber.String, not NewspaperSubscriber.String to String. Thoughts?

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;

}

my results!

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
scotho3
  • 123
  • 1
  • 14

1 Answers1

1

So basically I recognize the following questions:

Question 1

I'm trying to find a way in my second for loop (incrementing on y) to skip whatever element i'm currently processing in it's parent loop (incrementing on x).

Just don't insert the new element before doing the check. Hence, you want to swap these two lines:

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

To fulfill the odd requirements of your instructor you could pass the index where you insert the new subscriber to your checkAddress method and just skip it. However this restriction itself is very strange (to not call it bad design).

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

Question 2

Working on it for a while now, having a hard time with my exception.

The Exception occurs at the line

subscriberType = Integer.parseInt(userEntry);

You closed (cancelled) the popup window which makes JOptionPane.showInputDialog(...) return null. This is obviously not a valid Integer, which is why you get the mentioned Exception.

Note

In your method NewspaperSubscriber.equals(...) you should use String.equals(other) rather than ==. For more information see here.

Community
  • 1
  • 1
Izruo
  • 2,246
  • 1
  • 11
  • 23