Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,192 people online right now. Registration is fast and FREE... Join Now!




Need help with displaying names that are equal

 
Reply to this topicStart new topic

Need help with displaying names that are equal, Aliens

crazyskateface
7 Feb, 2008 - 11:49 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 9


My Contributions
I have a class called Alien and then 3 classes extending that called SnakeAlien, MarshMallowAlien, and OgreAlien...I also have a class called AlienPack which creates an Array of Alien Objects with their NAME, type of alien and health... and in my TestAlien Class i need to sort through the AlienPack and find out which Aliens are equal(type and health)

so i wrote a for loop to do this....but what I got was each Aliens name = other Aliens name TWICE, because it displays that and then the other way too like this

Aliens name = other Aliens name
other Aliens name = Aliens name

So what i need help with is...I only need one of those to appear!! here is my testAlien source and output...let me kno if you need to see any other classes .



CODE


public class TestAlien {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int maxPackSize = 15;
        
        int [] health = {50, 80, 70, 60, 70, 80, 100, 90, 80, 0, 100, 30};
        
        String [] name = {"Tim", "Agypt", "Lee", "Jason", "Mike", "Victor", "David", "Tiffany", "Katie", "Dan", "Cody", "Leeanna"};
        
        String [] type = {"O", "S", "M", "S", "M", "M", "O", "O", "S", "S", "O", "S"};
        
        
        
        
        
        
        
        System.out.println("List of pack members: ");
        
        
        
        
        
        
        
        AlienPack pack = new AlienPack(12);
        
        
        for(int i=0; i <=11; i++){
            
            if(type[i] == "O"){
                Alien al = new OgreAlien(health[i], name[i]);
                pack.addAlien(al);
                System.out.println(al.toString());
            }
            else if(type[i] == "S"){
                Alien al = new SnakeAlien(health[i], name[i]);
                pack.addAlien(al);
                System.out.println(al.toString());
            }
            else if(type[i] == "M"){
                Alien al = new MarshmallowAlien(health[i],name[i]);
                pack.addAlien(al);
                System.out.println(al.toString());
            }
        }
        
        
        
        
        System.out.println();
        System.out.println("Total pack damage: " + pack.calculateDamage());
        System.out.println();
        
        
        
        
        
        
        System.out.println("Names of equal pack members: ");
        
        
        
        String[] used = new String[12];
        boolean isUsed = false;
        
        
        int i = 0;
        
        do{
            
            for(int j=1; j < pack.getPackSize();j++){
                
                
                for(int k=0;k < used.length;k++){
                    if(pack.getAlien(i).name == used[k]){
                        isUsed = false;
                    }
                    else
                        isUsed = true;
                }
                
                
                
                if(pack.getAlien(i).equals(pack.getAlien(j))){
                    if(pack.getAlien(i) == pack.getAlien(j)){
                    ;
                    }
                    else{
                        
                        
                        
                        
                        
                        
                            System.out.println(pack.getAlien(i).name +" " + pack.getAlien(j).name);
                    
                    }
                
                    
                }// end if
                
                
                
            }// end for loop
            used[i]= pack.getAlien(i).name;
            i++;
            
            
        }while(i < pack.getPackSize());
        
        
        
    
        
        
        
    }//end main
    
    
    

}




and the output is
CODE

List of pack members:
Tim 50 Ogre
Agypt 80 Snake
Lee 70 MarshMallow
Jason 60 Snake
Mike 70 MarshMallow
Victor 80 MarshMallow
David 100 Ogre
Tiffany 90 Ogre
Katie 80 Snake
Dan 0 Snake
Cody 100 Ogre
Leeanna 30 Snake

Total pack damage: 67.4

Names of equal pack members:
Agypt Katie
Lee Mike
Mike Lee
David Cody
Katie Agypt
Cody David




Thanks smile.gif
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Need Help With Displaying Names That Are Equal
8 Feb, 2008 - 12:01 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
if(type[i] == "O"){
Alien al = new OgreAlien(health[i], name[i]);
pack.addAlien(al);
System.out.println(al.toString());
}
else if(type[i] == "S"){
Alien al = new SnakeAlien(health[i], name[i]);
pack.addAlien(al);
System.out.println(al.toString());
}
else if(type[i] == "M"){
Alien al = new MarshmallowAlien(health[i],name[i]);
pack.addAlien(al);
System.out.println(al.toString());
}



Use type[i].equals("Whatever")

== doesn't work if you are checking if the contents of strings are equal. It only checks if both object references are equal. (String is a final Immutable class).



User is offlineProfile CardPM
+Quote Post

crazyskateface
RE: Need Help With Displaying Names That Are Equal
8 Feb, 2008 - 12:30 AM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 9


My Contributions
Oh I was just talking about the last part of it where I find the Names of Equal Pack Members,
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month