简体   繁体   中英

How to Change a String on your Array

Well I have an array of people. They contain instance variables such as: NAME, AGE, LOCATION . These are private instance variables.

Now there are various of people in my array with their own instance variables. Ill use myself to make it easier.

Oscar 22 USA

However after successfully creating my array, I need to create a boolean method that will allow me to search throughout my array, find my name and change USA to Japan (or whatever). So if Oscar is in Japan, fine, but if Oscar is in USA change it to Japan.

This project is supposedly a fairly simple one and it is not really homework since it was a challenge but it has been a week since the professor assigned it and I can not understand it. I know how to do other methods but Boolean trips me up.


public class Frogs {

    private String breed;
    private String nativeTo;


    public Frogs(String breed, String nativeTo)
    {
        this.breed=breed;
        this.nativeTo=nativeTo;

    }

    //accessor(GETTER)

    public String getRegion() 
    {
        return nativeTo;
    }

    public String getBreedName()
    {
        return breed;
    }

    public String toString() ////(RETURNS A STRING (not the memory) ////////////
    {
        return ("Frog Breed -  " +breed+ "From: " +nativeTo);
    }}
public class WildAnimalsDemo {

public static void main(String[]args)
        {

            Frogs [] stuff = new Frogs[6];  //array of objects of type frogs

            stuff[0] = new Frogs ("Tree Frog " , "NJ");
            stuff[1] = new Frogs ("Lizard Frog " , "PA");
            stuff[2] = new Frogs ("Kermit Frog " , "Muppets");
            stuff[3] = new Frogs ("Bear Frog " , "Philipines");
            stuff[4] = new Frogs ("Bull Frog " , "New York");
            stuff[5] = new Frogs ("Lizard " , "PA");           // DOES NOT HAVE "FROG" IN IT

            System.out.println(stuff[0]);
            System.out.println(stuff[1]);
            System.out.println(stuff[2]);
            System.out.println(stuff[3]);
            System.out.println(stuff[4]);
            System.out.println(stuff[5]);
            System.out.println();
            //////////////////////////////////////////////////////////////////////////////////////////////////1stPRINT
            int count=findFrogFromPlace(stuff,"New York");

            System.out.println("Found: " + count);
            //////////////////////////////////////////////////////////////////////
            int count1=findFrogFromPlace(stuff,"PA");

            System.out.println("Found: " + count1);
            /////////////////////////////////////////////////////////////////////////////////////////////////2ndPrint


        /*  boolean possible = findAndSetBreed(stuff, "Frog");
            System.out.println(possible + " Species " + stuff[5] );
            boolean possible1 = findAndSetBreed(stuff, "Frog");
            System.out.println(possible1 + " Species " + stuff[4] );*/

        }

    public static int findFrogFromPlace(Frogs[] F , String theRegion)
    {
        int count=0;             //////THIS COUNT IS NOT THE SAME AS COUNT FROM THE MAIN

        for(int x=0; x<F.length; x++)
        {

               //Returns String/
            if (F[x].getRegion().equalsIgnoreCase(theRegion) )
                count++;
        }

        return count;
    }

    /*public static boolean findAndSetBreed(Frogs[] frog, String theBreed)
    {


        for(int x=0; x<frog.length; x++)
        {


        if (frog[x].getBreedName().equalsIgnoreCase(theBreed) )
                System.out.println(x);

        }
        return frogg;
    }   */
}

The example you posted is a bit different from what you asked. Anyway, you have to implement getter and setter methods for each field of the class. Then you can write a method

public boolean checkName(String name) {
   return (name.equals(this.name));
}

In the main you can set the right location by checking every object of your array, and calling setLocation() if checkName return true

Person[] array={.......}
for(Person p: array) {
   if(p.checkName("Oscar"))
       p.setLocation("Japan");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM