How to modify array elements from a separate class?

I am trying to modify array elements in a class which populates these fields by reading in a text file. I have got mutator methods to modify the array elements and they work fine in the same class.

public class Outlet {

private Outlet[] outlet;

private String outletName;

private int phoneNumber;

private int category;

private int operatingDays;

static final int DEFAULT = 99;

public void setPhoneNumber(int newPhoneNumber) {

phoneNumber = newPhoneNumber;

}

public void setCategory(int newCategory) {

category = newCategory;

}

public void setOperatingDays(int newOperatingDays) {

operatingDays = newOperatingDays;

}

public static void readFile(Outlet[] outlet) throws FileNotFoundException {

Scanner inFile = new Scanner(new FileReader("outlets.txt"));

int rowNo = 0;

int i = 0;

String outletValue;

while (inFile.hasNext() && rowNo < MAXOUTLETS) {

outlet[rowNo] = new Outlet();

outletValue = inFile.nextLine();

outlet[rowNo].setOutletName(outletValue.toUpperCase());

outlet[rowNo].setPhoneNumber(DEFAULT);

outlet[rowNo].setCategory(DEFAULT);

outlet[rowNo].setOperatingDays(DEFAULT);

}

inFile.close();

}

public static void displayUnassignedOutlets(Outlet[] outlet) {

int i = 0;

System.out.println("Showing all unassigned Outlets");

System.out.println(STARS);

for (i = 0; i < MAXOUTLETS; i++ ) {

if (outlet.getCategory() == DEFAULT) {

System.out.println("\nOutlet Number: " + (i + 1) + "\t" +

outlet.getOutletName());

}

}

}

Now in the other class that I want to modify the array elements I use the following code but I get an error that "The expression type must be an array but a Class ' Outlet' is resolved".

So how can I modify the array elements? What do I have to instantiate to get the following code to work?

public class Franchise {

private Franchise[] franchise;

public Outlet[] outlet;

public static void createFranchise(Franchise[] franchise) throws FileNotFoundException {

Scanner console = new Scanner(System.in);

int choice = -1;

++++++++++++++++++++++++++++++++++++

Outlet outlet = new Outlet();

Outlet.readFile(outlet.getOutlet());

++++++++++++++++++++++++++++++++++++

boolean invalidChoice = true;

while (invalidChoice) {

System.out.println("\nCreating a New Franchise...");

System.out.println(STARS);

System.out.println("Please select an outlet from the list below");

Outlet.displayUnassignedOutlets(outlet.getOutlet());

choice = console.nextInt();

if (choice < 0 || choice > 10) {

System.out.println("Error! Please choose a single number between 1 and 10");

}

else {

invalidChoice = false;

}

}

invalidChoice = true;

while (invalidChoice) {

System.out.println("Please enter the Phone Number for this Outlet");

choice = console.nextInt();

String phone = new String();

phone = new Integer(choice).toString();

if (phone.length() < 8 || phone.length() > 10) {

System.out.println("Error! Please enter 8 to 10 digits only");

}

else {

+++++++++++++++++++++++++++++++++++++++

outlet[(choice - 1)].setPhoneNumber(choice);

+++++++++++++++++++++++++++++++++++++++

invalidChoice = false;

}

}

}

[3557 byte] By [karpathiana] at [2007-11-27 4:54:19]
# 1

Outlet and the array are two different animals. The array is being held as a private field in the Outlet class, and that is not the best place for it as now, each outlet entity carries around a new and separate copy of this array. Perhaps it could be in there as a static variable, but my guess is that it belongs somewhere else, perhaps in its own class w/ it's own getter and setter methods.

Oh, and please use code tags to make your code readable. You will find that more (and smarter) people will be willing to read your code if it is formated in a readable way.

Good luck

/Pete

petes1234a at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...
# 2

Hi Pete!

Thanks for your comments. I have included my full classes below with their respective driver modules. Hope this helps out a bit more using the code tags. Sorry, it was my first posting. Thanks for the heads up!

import java.util.*;

import java.io.*;

public class Outlet {

public Outlet[] outlet;

private String outletName;

private int phoneNumber;

private int category;

private int operatingDays;

//private Applicant chosenApplicant;

static boolean SHOWDETAILS = false;

static final String STARS = "****************************************";

static final int MAXOUTLETS = 10;

static final int DEFAULT = 99;

public Outlet[] getOutlet() {

return outlet;

}

public String getOutletName() {

return outletName;

}

public int getPhoneNumber() {

return phoneNumber;

}

public int getCategory() {

return category;

}

public int getOperatingDays() {

return operatingDays;

}

public void setOutletName(String newOutletName) {

outletName = newOutletName;

}

public void setPhoneNumber(int newPhoneNumber) {

phoneNumber = newPhoneNumber;

}

public void setCategory(int newCategory) {

category = newCategory;

}

public void setOperatingDays(int newOperatingDays) {

operatingDays = newOperatingDays;

}

public Outlet() {

outlet = new Outlet[10];

}

public static void readFile(Outlet[] outlet) throws FileNotFoundException {

Scanner inFile = new Scanner(new FileReader("outlets.txt"));

int rowNo = 0;

int i = 0;

String outletValue;

while (inFile.hasNext() && rowNo < MAXOUTLETS) {

outlet[rowNo] = new Outlet();

outletValue = inFile.nextLine();

outlet[rowNo].setOutletName(outletValue.toUpperCase());

//System.out.println(rowNo % 2);

//if (rowNo % 2 == 0) {

outlet[rowNo].setPhoneNumber(DEFAULT);

outlet[rowNo].setCategory(DEFAULT);

outlet[rowNo].setOperatingDays(DEFAULT);

//}

//System.out.println("Outlet Name+++++++ " + rowNo + "\n" + outlet[rowNo].getOutlet());

rowNo++;

//System.out.println(rowNo);

}

if (SHOWDETAILS) {

if (rowNo > 6) {

for (i = 0; i < rowNo; i++ ) {

System.out.println("\nOutlet Name+++++++ " + (i + 1) + "\t" +

outlet[i].getOutletName());

}

}

}

inFile.close();

}

public static void displayAllOutlets(Outlet[] outlet) {

int i = 0;

System.out.println("Showing All Outlets");

System.out.println(STARS);

for (i = 0; i < MAXOUTLETS; i++ ) {

System.out.println("\nOutlet Number: " + (i + 1) + "\t" +

outlet[i].getOutletName());

}

}

public static void displayUnassignedOutlets(Outlet[] outlet) {

int i = 0;

System.out.println("Showing all unassigned Outlets");

System.out.println(STARS);

for (i = 0; i < MAXOUTLETS; i++ ) {

if (outlet[i].getCategory() == DEFAULT) {

System.out.println("\nOutlet Number: " + (i + 1) + "\t" +

outlet[i].getOutletName());

}

}

}

public static void main(String[] args) throws FileNotFoundException {

Outlet start = new Outlet();

Outlet.readFile(start.getOutlet());

Outlet.displayUnassignedOutlets(start.getOutlet());

}

}

================================

So in the below Franchise class, when I specify:

outlet[(choice - 1)].setPhoneNumber(choice);

I get the error that an array is required but the class Outlet is resolved. Any feedback is greatly appreciated!

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.*;

public class Franchise {

private Franchise[] franchise;

public Outlet[] outlet;

static final int MAXOUTLETS = 10;

static final int DEFAULT = 99;

static boolean SHOWDETAILS = false;

static final String STARS = "****************************************";

static final double REGHOTDOG = 2.50;

static final double LARGEHOTDOG = 4;

static final int SALESPERIOD = 28;

static final int OPERATINGHOURS = 8;

public Franchise[] getFranchise() {

return franchise;

}

public Franchise() {

}

public static void createFranchise(Franchise[] franchise) throws FileNotFoundException {

Scanner console = new Scanner(System.in);

int choice = -1;

//franchise[i] = new Franchise();

Outlet outlet = new Outlet();

//outlet[i] = new Franchise();

Outlet[] myOutlet = new Outlet[10];

Outlet.readFile(outlet.getOutlet());

boolean invalidChoice = true;

while (invalidChoice) {

System.out.println("\nCreating a New Franchise...");

System.out.println(STARS);

System.out.println("Please select an outlet from the list below");

Outlet.displayUnassignedOutlets(outlet.getOutlet());

choice = console.nextInt();

if (choice < 0 || choice > 10) {

System.out.println("Error! Please choose a single number between 1 and 10");

}

else {

invalidChoice = false;

}

}

//System.out.println(j);

invalidChoice = true;

while (invalidChoice) {

System.out.println("Please enter the Phone Number for this Outlet");

choice = console.nextInt();

String phone = new String();

phone = new Integer(choice).toString();

if (phone.length() < 8 || phone.length() > 10) {

System.out.println("Error! Please enter 8 to 10 digits only");

}

else {

outlet[(choice - 1)].setPhoneNumber(choice);

invalidChoice = false;

}

}

invalidChoice = true;

while (invalidChoice) {

System.out.println("Please enter the category number for this Outlet");

choice = console.nextInt();

if (choice < 1 || choice > 4) {

System.out.println("Error! Please choose a single number between 1 and 4");

}

else {

outlet.setCategory(choice);

invalidChoice = false;

}

}

invalidChoice = true;

while (invalidChoice) {

System.out.println("Please enter the Operating Days for this Outlet");

choice = console.nextInt();

if (choice < 5 || choice > 7) {

System.out.println("Error! Please choose a single number between 5 and 7");

}

else {

outlet.setOperatingDays(choice);

invalidChoice = false;

}

}

//Applicant chosenApplicant = new Applicant();

//Applicant.readFile(chosenApplicant.getApplicant());

//Applicant.checkCriteria(chosenApplicant.getApplicant());

//System.out.println("This Franchise has been assigned to : " +

//chosenApplicant.displayOneEligibleApplicant());

Outlet.displayUnassignedOutlets(outlet.getOutlet());

}

public static void main(String[] args) throws FileNotFoundException {

Franchise start = new Franchise();

Franchise.testing(start.getFranchise());

//Franchise.createFranchise(start.getFranchise());

//Franchise.displaySalesForcast();

//Franchise.displayAllFranchises(start.getOutlet());

}

}

karpathiana at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...
# 3
again, i strongly urge you to remove the Outlet array out of the Outlet class. The one doesn't belong within the other.
petes1234a at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...
# 4

I have modified the code and separated out the array in to it's own class (OutletArray.class). The outlet class works fine when accessing this class using the driver module that I have already included previously (obviously with some naming changes).

The problem I have now is when I try to access that array from the Franchise class, I get a NullPointer exception which is probably correct as I use the following code:

OutletArray outletArray = new OutletArray();

OutletCopy.displayUnassignedOutlets(outletArray.getOutlet());

My question is, how can I access the already existing array created from one class (Outlet) by another class (Franchise) without calling the "new" method?

karpathiana at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...
# 5

you know that you can use Collections framework Hashmaps or arraylist to take care of your lengthy program. It would be much easier if you use Map or ArrayList. Actually sometime arrays are useful but most of the time ArrayList will take care of everything.

I have'nt run your program. On my way .

lrngjavaa at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...
# 6

I agree 100% with lrngjava about using an arraylist. What you probably need to do is to keep the arrayList private and access it with selected public methods like listed below that mimic the functionality of an arraylist in a limited way. What I'm doing is implementing many of the methods from the Collection interface and some from List.

import java.util.ArrayList;

import java.util.Iterator;

// by implementing Iterable, you can "foreach" through the collection

class OutletArray implements Iterable<Outlet>

{

private ArrayList<Outlet> myOutlets;

public OutletArray()

{

myOutlets = new ArrayList<Outlet>();

}

// this allows you to do a foreach w/ this class

public Iterator<Outlet> iterator()

{

return myOutlets.iterator();

}

public boolean add(Outlet o)

{

return myOutlets.add(o);

}

public boolean contains(Outlet o)

{

return myOutlets.contains(o);

}

public boolean remove(Outlet o)

{

return myOutlets.remove(o);

}

public int size()

{

return myOutlets.size();

}

public Outlet get(int index)

{

return myOutlets.get(index);

}

}

petes1234a at 2007-7-12 10:08:56 > top of Java-index,Java Essentials,New To Java...