Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-14-2008, 08:25 PM
Member
 
Join Date: Sep 2008
Location: Florida
Posts: 5
dch414 is on a distinguished road
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>
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-14-2008, 10:20 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
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.
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-14-2008, 11:59 PM
Member
 
Join Date: Sep 2008
Location: Florida
Posts: 5
dch414 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Unable to access array defiened in constructor in other methods. Shyam Singh New To Java 1 07-20-2008 06:42 PM
Array Constructor Javanoob828282 New To Java 1 05-01-2008 12:25 AM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 11:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 11:01 AM
problems with asigning elements of an array to a constructor rednessc New To Java 1 12-14-2007 09:25 AM


All times are GMT +3. The time now is 10:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org