One array reading another

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 369 Views - Last Post: 03 September 2012 - 08:20 AM Rate Topic: -----

#1 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

One array reading another

Posted 01 September 2012 - 03:12 PM

I am trying to get one array to read another based on search terms. To clarify I have one array of states and I have an array of search terms and I need to perform a sequential seach. I do not want the entire method I just want to be helped in getting started.
import java.io.*;
/**
 *
 * @author davidsnow
 */
public class stateController{
  private States[] unitedStates;
  private int numStates=0;
  private int counter;
  
 //----------------------------------------------------------------------------
  public stateController(int maxSize)
   {
    unitedStates = new States[maxSize];  
   }//end stateController()
//------------------------------------------------------------------------------
public void loadData(String filename) throws IOException
{
    FileInputStream fis1 = new FileInputStream("States.txt");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    String state, capital, abbr, region;
    int pop, regNum;
    String inputString = br1.readLine();
    
    while(inputString != null)
        {
        state = inputString.substring(0,15).trim();
        capital = inputString.substring(15,30).trim();
        abbr = inputString.substring(30,32).trim();
        pop = Integer.parseInt(inputString.substring(32,40).trim());
        region = inputString.substring(40,55).trim();
        regNum = Integer.parseInt(inputString.substring(55,56));
        unitedStates[numStates] = new States(state, capital, abbr, pop, region, 
                regNum);
        numStates++;
        inputString = br1.readLine();
        }//end while
        br1.close();
    }// end loadData();
//------------------------------------------------------------------------------
public void displayStates()
    {
     System.out.printf("%15s %15s %6s %12s %14s %5s","State", "Capital", 
                    "Abbr", "Population", "Region", "Region Number");
     System.out.println("\n----------------------------------------------------"
            + "------------------------------");
        
    for(int i=0; i<numStates; i++)//for loop to print out array of countries
        {
           System.out.println(unitedStates[i]);
        }
    }//end displayStates()
//-----------------------------------------------------------------------------
public void selectionSort()
{
        int out, in, min;
        for(out=0; out<(unitedStates.length); out++)
        {
            for(in = out+1; in<(unitedStates.length);in++){
                min=out;
                if(unitedStates[in].getStateName().compareTo
                        (unitedStates[min].getStateName())<0)
                 min=in;
                 swap(out,min);
            }//end for(in)
        }//end for(out)   
}//end selectionSort
}


public class States
{
    private String stateName;
    private String stateCapital;
    private String stateAbbr;
    private int statePopulation;
    private String stateRegion;
    private int regionNbr;
 //-----------------------------------------------------------------------------   
    public States(String state, String capital, String abbr, int pop, 
            String region, int regNum) 
    {
        stateName = state;
        stateCapital = capital;
        stateAbbr = abbr;
        statePopulation = pop;
        stateRegion = region;
        regionNbr = regNum;
    }
//------------------------------------------------------------------------------
public String getStateName()
{
    return stateName;
}
//------------------------------------------------------------------------------
public String getStateCapital()
{
    return stateCapital;
}
//------------------------------------------------------------------------------
public String getStateAbbreviation()
{
    return stateAbbr;
}
//------------------------------------------------------------------------------
public int getStatePopulation()
{
    return statePopulation;
}
//------------------------------------------------------------------------------
public String getstateRegion()
{
    return stateRegion;
}
//------------------------------------------------------------------------------
public int getRegionNumber()
{
    return regionNbr;
}
//------------------------------------------------------------------------------
public String toString()
    {
    String s = String.format("%15s %15s %5s %12d %17s %5d", stateName, 
            stateCapital, stateAbbr, statePopulation, stateRegion, regionNbr);
    return s;
    }
}



public class KeyController {
private SearchKey[] myKeys;
private int numKeys=0;
private String key;
//------------------------------------------------------------------------------
public KeyController (int nSize)
{
    myKeys = new SearchKey[nSize];
}
//------------------------------------------------------------------------------
public void loadSearchKeys(String filename) throws IOException{
    FileInputStream fis1 = new FileInputStream("SearchFile.txt");
    BufferedReader br1= new BufferedReader(new InputStreamReader(fis1));
    int end;
    String inputString=br1.readLine();
    while(inputString != null)
    {
        end = Math.min(14, inputString.length());
        key = inputString.substring(0,end);
        myKeys[numKeys]= new SearchKey(key);
        numKeys++;
        inputString = br1.readLine();
    }
    br1.close();
}
//------------------------------------------------------------------------------
public void displayKeys(){    
for(int i=0; i<numKeys; i++)//for loop to print out array of search Terms
        {
           System.out.println(myKeys[i]);
        }
}
}

public class SearchKey {
private String searchTerm;
//------------------------------------------------------------------------------
public SearchKey(String key){
    searchTerm=key;;
}
//------------------------------------------------------------------------------
public String getSearchTerm(){
    return searchTerm;
}
//------------------------------------------------------------------------------
public String toString()
{
    return searchTerm;
}

This post has been edited by macosxnerd101: 01 September 2012 - 04:03 PM
Reason for edit:: Added code tags around States class


Is This A Good Question/Topic? 0
  • +

Replies To: One array reading another

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: One array reading another

Posted 01 September 2012 - 04:04 PM

Quote

I am trying to get one array to read another based on search terms.

Arrays do not read each other. You write code that parses the arrays.

Quote

To clarify I have one array of states and I have an array of search terms and I need to perform a sequential seach.

A sequential search is when you iterate through an array (or Collection) element by element, comparing against a single term. Since you have two arrays, you will need nested loops.
Was This Post Helpful? 1
  • +
  • -

#3 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 04:15 PM

What kind of loops should I use?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: One array reading another

Posted 01 September 2012 - 04:16 PM

It doesn't really matter. I'd use for loops personally.
Was This Post Helpful? 0
  • +
  • -

#5 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 04:22 PM

Really my biggest challenge is writing the start of the method I want to write in the state controller but I have no idea how to declare it and how to feed in the elements from the SearchKey array.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: One array reading another

Posted 01 September 2012 - 04:25 PM

Let's work on the design then. Clearly we need the keys and the States.
public void foo(String[] keys, State[] states){

}



What should this method return though, if anything? I left the return type as void. Should it return true if all the keys are found in states? An int for the number of keys found in states? No return at all? Think about what you need back from the method.
Was This Post Helpful? 0
  • +
  • -

#7 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 04:29 PM

I am going to want back the search term, a statement that reads whether or not it was found and how many probes it took to locate.
Was This Post Helpful? 0
  • +
  • -

#8 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 04:37 PM

sorry, I had to reread what you were asking I believe for right now void would be the easiest and I think I know how to right most of the code but I am still struggling on how to nest the for loops do I call the search key first and then the first state?
Was This Post Helpful? 0
  • +
  • -

#9 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 05:00 PM

Based on the previous suggestions this is what I wrote, however I am getting an error at the second if statement unreachable statement. What am I doing wrong and will this even work for a sequential search using two arrays?
public void search(SearchKey[] myKeys, States[] unitedStates )
{
int i, j; 
    for(j=0; j<myKeys.length; j++){
        for(i=0; i<unitedStates.length; i++)
            if(myKeys[j].getSearchTerm().equals(unitedStates[i].getStateName()));
            break;
     if(j==numStates) 
            System.out.println("Could not find"+myKeys[j]);
        
     else 
            System.out.println(myKeys[j]);


This post has been edited by macosxnerd101: 01 September 2012 - 05:46 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: One array reading another

Posted 01 September 2012 - 05:51 PM

Quote

I am going to want back the search term, a statement that reads whether or not it was found and how many probes it took to locate.

So basically you are asking for the index of the element. Let's do a little reworking then so you have a method that searches for one term.
public int search(String key, State[] states){

    //for each element in states
    //if the element name equals the key, return the current index

    //if the element is not found, return -1, signifying the key is not found
}



The invoking method can then take the return value and organize it into the desired output.
Was This Post Helpful? 0
  • +
  • -

#11 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 01 September 2012 - 06:24 PM

So how do I set key to be the currently called element in the SearchKey array.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: One array reading another

Posted 01 September 2012 - 07:10 PM

You will need to loop through your first array (the keys), and invoke the search() method in the loop. You will pass the appropriate element based on the loop. Remember how to access an array element.

Make sure to read up on arrays and for loops. This sounds like homework, so you probably have notes somewhere on these topics. Even if you don't, there are a number of tutorials online.
Was This Post Helpful? 0
  • +
  • -

#13 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 03 September 2012 - 07:04 AM

I am trying to do a liner search by feeding elements of one array into the search method with the first for loop, then feeding the elements into the method with the second for loop. However, the IDE is telling me that their is an else without an if. I have the if so what am I doing wrong?
 public void search()
    {
        int i, j;
        for(i=0;i<numKeys; i++){
        
           for(j=0; j<numStates; j++){
                if(unitedStates[j].getStateName().equals(myKeys[i].getSearchTerm()))
                    break;
                if(j==numStates)
                   System.out.print("Can't find ");
                   System.out.print(myKeys[i]);
                else
                    System.out.print("Found ");
                    System.out.print(myKeys[i]);
                        }
  

Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: One array reading another

Posted 03 September 2012 - 07:11 AM

Your intent in this part:
if(j==numStates)
    System.out.print("Can't find ");
System.out.print(myKeys[i]);
else
    System.out.print("Found ");
System.out.print(myKeys[i]);

is uncertain. If multiple lines belong in an if clause or loop, then enclose them in braces, like:
if(j==numStates)
{
    System.out.print("Can't find ");
    System.out.print(myKeys[i]);
}
else
    System.out.print("Found ");
System.out.print(myKeys[i]);

I'm still not sure how it's supposed to end, with 2 statements in the else clause or just one.

Use whitespace, braces, and indention to write correct, easily readable code.

This post has been edited by GregBrannon: 03 September 2012 - 07:11 AM

Was This Post Helpful? 1
  • +
  • -

#15 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: One array reading another

Posted 03 September 2012 - 07:57 AM

Got it to work thank you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2