d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > String Sequential Search Array Problem
Add Reply New Topic New Poll
Member
Posts: 2,322
Joined: Jun 28 2008
Gold: 0.00
Nov 8 2012 11:35pm
Okay so here is my problem, I am trying to pass a string of lets say "Bulbasaur" into getSpecific(string name) in order to get the returned array of pokemonInfo;

getSpecific(int i) does work and returns
{001,Bulbasaur,Grass,Poison,1,2,0,-1}
every value in that array is a String(just fyi)
so I want to be able to search through my IO file that i have for a specific pokemon name and return the array corresponding to it's array position.
I don't know if this will be as confusing to you as it is to me atm.

Thanks for your time reading this.

Code
import java.util.Scanner;
import java.io.*;

/**
* Write a description of class loadPokemon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class loadPokemon{
   private String filename = "poke.txt";
   private String pokemonData;
   public String[] pokemonInfo;
   public loadPokemon()throws IOException{
       File file = new File(filename);
       Scanner inputFile = new Scanner(file);
       while(inputFile.hasNext()){
           pokemonData = inputFile.nextLine();
           pokemonInfo = pokemonData.split("~");

       }
   }

   public String[] getSpecific(int i){
       return pokemonInfo[i].split(",");
   }

   public String[] getSpecific(String name)throws IOException{
       int index, element;
       boolean found;
       index =0;
       element = -1;
       found=false;
       loadPokemon lp = new loadPokemon();
       while(!false && index < pokemonInfo.length){
       String[] data = lp.getSpecific(index);
           if(name.equals(data[index]) && index < 8){
               found = true;
               element = index;
           }
           index++;
       }
       if(element != -1){
           index = (element - 1);
           return pokemonInfo[index].split(",");
       }else{
           return null;
       }
   }
}
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 9 2012 02:00am
did you have an actual question?
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Nov 9 2012 02:00pm
Code

public String[] getSpecific(String name) {
   // do whatever you need to set up the environment, like loadPokemon() or whatever

   for (int i = 0; i < pokemonInfo.length; i++) {
       String[] currentInfo = getSpecific(i);
       String currentName = currentInfo[0];

       if (currentName.equals(name)) { // or .equalsIgnoreCase() etc
           return currentInfo;
       }
   }

   // do whatever you want when there is no matching name
}


This post was edited by PumblesMumbles on Nov 9 2012 02:00pm
Member
Posts: 2,322
Joined: Jun 28 2008
Gold: 0.00
Nov 9 2012 11:00pm
Quote (PumblesMumbles @ Nov 9 2012 02:00pm)
Code
public String[] getSpecific(String name) {
   // do whatever you need to set up the environment, like loadPokemon() or whatever

   for (int i = 0; i < pokemonInfo.length; i++) {
       String[] currentInfo = getSpecific(i);
       String currentName = currentInfo[0];

       if (currentName.equals(name)) { // or .equalsIgnoreCase() etc
           return currentInfo;
       }
   }

   // do whatever you want when there is no matching name
}


ok so basically it will just push all the String arrays one by one into another String array and then you pull out the variable you want, in my case i need [1] and then compares that value against the (name) variable;

in cases like this, can you even do what I was trying to do, where you just compare it directly into the array, or is it better to pull the String out and then compare it using .equalsIgnoreCase()?

Thanks alot for the help PumblesMumbles <3
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 9 2012 11:23pm
Quote (Paudertzz @ Nov 10 2012 01:00am)
in cases like this, can you even do what I was trying to do, where you just compare it directly into the array, or is it better to pull the String out and then compare it using .equalsIgnoreCase()?

Thanks alot for the help PumblesMumbles <3


not sure i understand the question. are you confused by this?
Code
String[] currentInfo = getSpecific(i);
      String currentName = currentInfo[0];

      if (currentName.equals(name))


It's the same as doing this:
Code
if (getSpecific(i)[0].equals(name))


you're just wrapping it in another variable for cleanliness. you're not "pulling it out" since it's still in the array.

This post was edited by carteblanche on Nov 9 2012 11:24pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll