Results 1 to 3 of 3
  1. #1
    dch414 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    5
    Rep Power
    0

    Default Sending an array in a constructor?

    My problem is this:I was supposed to read two text files and store them as State array objects: easy. Then I am supposed to do other things like display it and so on. Problem is, as my code sits now, the method seqSearch() only accesses one array either arrState or arrSearch, not both like I need it to. I need to compare the arrays from the ArrayObjects class search and a and if they match at any time to say so. I need a way to have both arrays called and searched in respect from one another but I need to do it from main with no other static methods than main. Hope that made sense. Here is my code:
    Any help is appreciated.

    <code>
    import java.io.*;
    public class SortMain {

    public static void main(String[] args)
    {
    String line, line1, name, cap, abbrev, pop, region, regionNumber;
    int maxSize = 75;
    ArrayObjects arrState = new ArrayObjects(maxSize);
    ArrayObjects arrSearch= new ArrayObjects(maxSize);

    try{
    File nFile = new File("States.txt");
    FileReader fReader1 = new FileReader(nFile);
    BufferedReader bReader1 = new
    BufferedReader(fReader1);

    while((line = bReader1.readLine()) != null) {

    name = line.substring(0, 14);
    cap = line.substring(15, 29);
    abbrev = line.substring(30, 32);
    pop = line.substring(33, 39);
    region = line.substring(40, 55);
    regionNumber = line.substring(55);

    arrState.insertState(name, cap, abbrev, pop, region, regionNumber);

    }
    bReader1.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    try{
    File nFile1 = new File("Search.txt");
    FileReader fReader2 = new FileReader(nFile1);
    BufferedReader bReader2 = new
    BufferedReader(fReader2);


    while((line1 = bReader2.readLine()) != null)
    {
    arrSearch.insertSearch(line1);
    }
    bReader2.close();
    }
    catch (Exception c)
    {
    c.printStackTrace();
    }
    // arrState.displayState(1);
    //arrSearch.displayState(2);
    // arrState.bubbleSort();
    //arrState.displayState(1);
    //arrSearch.displayState(2);
    arrState.seqSearch(arrSearch);//Here is what I was talking about

    }//end main
    }//end class SortMain

    public class ArrayObjects {
    private State[] a;
    boolean found;
    private int sElems;
    private int nElems;
    private int counter;
    private State[] search;
    private State temp;
    private int swapCounter;
    public ArrayObjects(int max){

    a = new State[max]; // create a ref to an array of ‘max’ States
    search = new State[max];
    sElems = 0;
    nElems = 0;
    }

    public void insertState(String name, String cap, String abbrev, String pop, String region, String regionNumber){

    a[nElems] = new State(name, cap, abbrev, pop, region, regionNumber);
    nElems++;
    counter = nElems;

    }
    public void insertSearch(String line1){


    search[sElems] = new State(line1);
    sElems++;

    }

    //--------------------------------------
    public void displayState(int displayWhat) { // displays array contents

    if (displayWhat == 1){
    System.out.println("State Name Capital Abbr. Popul. "
    + "Region Region #");
    System.out.println(" ");

    for (int j=0; j<counter; j++) // for each element,
    a[j].displayState(1); } // display it
    else
    if (displayWhat == 2){
    System.out.println("State Name");
    System.out.println(" ");

    for (int j=0; j<sElems; j++) // for each element,
    search[j].displayState(2); } // display it

    else{System.out.println("Invalid display input.");}

    }

    public void bubbleSort()
    {
    int n = counter;
    swapCounter = 0;
    for (int pass = 1; pass < n; pass++) {

    for (int i = 0; i < n - pass; i++) {

    if (a[i].getState().compareTo(a[i + 1].getState()) > 0) {
    temp = a[i];
    a[i] = a[i + 1];
    a[i + 1] = temp;
    swapCounter++;
    }
    }
    }
    System.out.println(" ");
    System.out.println("There were " + swapCounter + " swaps during this sort.");
    System.out.println(" ");
    }
    public void seqSearch(State[] search){
    int n = nElems, m = sElems;
    System.out.println(sElems + " " + nElems);
    for (int k = 0; k<m; k++){
    System.out.println("outerloop");
    for (int i = 0; i < n; i++) {
    System.out.println("inner loop");
    if (search[k]!= null && a[i] != null && search[k].getState().equals(a[i].getState())) {
    System.out.println("The word 'search' found at index " + i);
    break;
    }
    }
    }

    }
    } //end class ArrayObjects

    public class State
    {
    private String stName;
    private String stCap;
    private String stAbbrev;
    private String stPop;
    private String stRegion;
    private String stRegionNumber;


    public State(String name, String cap, String abbrev, String pop, String region, String regionNumber)
    { //Constructor for state Object
    stName = name;
    stCap = cap;
    stAbbrev = abbrev;
    stPop = pop;
    stRegion = region;
    stRegionNumber = regionNumber;
    }
    public State(String line1){
    stName = line1;
    }
    public void displayState(int caller)
    {
    if (caller == 1){
    System.out.print(stName);
    System.out.print(" " + stCap);
    System.out.print(" " + stAbbrev);
    System.out.print(" " + stPop);
    System.out.print(" " + stRegion);
    System.out.print(" " + stRegionNumber);
    System.out.println ("");}
    else
    if (caller == 2)
    System.out.println(stName);

    else
    System.out.println("Illegal value passed, please try again.");
    }
    public String getState() // get last name
    { return stName;}


    }//end of class State
    </code>

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    There are usually a lot of possibilities when it comes to class design. Some suggestions:
    Don't try to do too much in the ArrayObjects class.
    Java Code:
    public class SortMain {
        public static void main(String[] args) {
            ArrayObjects arrState = new ArrayObjects(maxSize);
            String[] names = new String[maxSize];
            ...
    
            try{
                File nFile = new File("States.txt");
            }
    
            try{
                File nFile1 = new File("Search.txt");
                ...
                int i = 0;
                while((line1 = bReader2.readLine()) != null) {
                    names[i++] = line1;
                ...
            }
            ...
            arrState.seqSearch(names);
            ...
    public class ArrayObjects {
        private State[] a;
        ...
    //    private State[] search;
        ...
        public void seqSearch(String[] names){
            // Run through (member variable) State[] a,
            // get the "stName" field value and compare it with
            // each name in the (local variable) "names" array.
    Or you use the same instance of ArrayObjects for the two State arrays.
    Java Code:
    public class SortMain {
        public static void main(String[] args) {
            ArrayObjects arrState = new ArrayObjects(maxSize);
            ...
            try{
                File nFile = new File("States.txt");
                ...
                    arrState.insertState(name, cap, abbrev, pop,
                                         region, regionNumber);
            }
    
            try{
                File nFile1 = new File("Search.txt");
                ...
                while((line1 = bReader2.readLine()) != null) {
                    arrState.insertState(line1);
                ...
            }
            ...
            arrState.seqSearch();
            ...
    public class ArrayObjects {
        private State[] a;
        ...
        private State[] search;
        ...
        public void seqSearch(){
            // use both arrays of this class
            // Look through "search" for each name in "a"
    Or you could stay with what you've got. Just be sure that the State array argument is distinguishable from the member variable array: the argument array has the names only; the member variable array has much more information.

  3. #3
    dch414 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    5
    Rep Power
    0

    Default Thank you for your rapid response!

    Thanks hardwired! I decided to go with the same instance scenario as you suggested. Looking forward to making more acquaintances here.

Similar Threads

  1. Replies: 1
    Last Post: 07-20-2008, 04:42 PM
  2. Array Constructor
    By Javanoob828282 in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:25 PM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Replies: 1
    Last Post: 12-14-2007, 07:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •