Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50
  1. #21
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    With getName method I would just like to print out the dogs name. Nothing else.
    If I add two dogs with same name it still prints out them both out.
    Like:

    Jeff
    Jeff

  2. #22
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Also add a naive hashCode method to your dog class and make it return the hashCode of the name. If it still doesn't work then post all the code you have.

  3. #23
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Is it that big to post ur code?

    Please post it completley with code tag.
    Ramya:cool:

  4. #24
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Ok here is the full code I have so far. Not sure how I should do the hashMap that you suggested.

    Mainclass:
    Java Code:
    package zoo;
    import java.io.*;
    import java.util.*;
    
    public class Zoo {
    	
    	static List<Animal> animals = new ArrayList<Animal>();
    	static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    	
    	private void start() {
    		
    		int choice = 0;
    		do{
    			printMenu();
    			System.out.print("Choice: ");
    			choice = readInt();
    			
    			switch(choice){
    			case 1:
    				printDogs();
    				break;
    			case 2:
    				addDog();
    				break;
    			case 0:
    				break;
    			default:
    				System.out.println("Nothing to do!");
    			}
    			}while(choice!=0);
    		}
    	
    	private void printDogs(){
    		System.out.println("Dogs");
    		Set dogs = new HashSet(animals);
    		Iterator i = dogs.iterator();
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    				System.out.println(d.getName());
    		}
    	}
    	
    	private void addDog(){
    		System.out.print("Give dogs name; ");
    		String name = readText();
    		System.out.print("Give a grade: ");
    		int grade = readInt();
    		System.out.print("Give info: ");
    		String info = readText();
    		System.out.print("Give type: ");
    		String type = readText();
    		Dog d = new Dog(name,grade,info,type);
    		animals.add(d);
    		
    	}
    
    
    	public void printMenu(){
    		System.out.println("1)   Print dogs");
    		System.out.println("2)   Add a Dog");
    		System.out.println("0)   Quit");
    		
    	}
    	
    	public int readInt(){
    		boolean read = false;
    		int number = 0;
    		while(!read){
    			try{
    				number = Integer.parseInt(stdin.readLine());
    				read = true;
    			}
    			catch (NumberFormatException nfe){
    				System.out.print("Incorrect number: ");
    			}
    			catch (IOException ioe){
    				System.out.print("Error, try again: ");
    			}
    		}
    		return number;
    	}
    	
    	public String readText(){
    		boolean ok = false;
    		String name = null;
    		System.out.print(": ");
    		while(!ok){
    			try{
    				name = stdin.readLine();
    				if(name.trim().length()<1){
    					System.out.print("Empty");
    				}
    				else
    					ok = true;
    			}
    			catch (IOException ioe){
    				name = "";
    			}
    		}
    		return name;
    	}
    
    	
    	public static void main(String[] args)throws IOException{
    		Zoo zoo = new Zoo();
    		zoo.start();
    	}
    }

    Animal Class:
    Java Code:
    	package zoo;
    import java.io.Serializable;
    
    
    public abstract class Animal implements Serializable{
    	
    	protected String name;
    	protected int grade;
    	protected String info;
    	
    	public Animal(String name, int grade, String info){
    		this.name=name;
    		this.grade=grade;
    		this.info=info;
    	}
    	
    	public Animal(){}
    	
    	public abstract String getName();
    	
    	public abstract int getGrade();
    	
    	public abstract String getInfo();
    	
    	
    }
    Dog Class:

    Java Code:
    package zoo;
    
    
    
    public class Dog extends Animal{
    	
    	private String type; 
    
    	public Dog(String name, int grade, String info,String type){
    		super(name,grade,info);
    		this.type=type;
    		
    	}
    	
    	public boolean equals(Object obj){
    		if(this == obj){
    			return true;
    		}
    		if(!(obj instanceof Dog)){
    			return false;
    		}
    		Dog d = (Dog)obj;
    		return name.equalsIgnoreCase(d.getName());
    	}
    	
    	public String getName(){
    		return name;
    	}
    
    	public int getGrade() {
    		return grade;
    	}
    
    	public String getInfo() {
    		return info;
    	}
    	
    	public String getType(){
    		return type;
    	}
    
    }
    Last edited by soketti; 10-26-2009 at 03:14 PM.

  5. #25
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Iam able to see lot of compilation errors.Iam getting surprised how u are able to execute it first?
    Ramya:cool:

  6. #26
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Just add
    Java Code:
    @Override
    public int hashCode() {
    	return name.hashCode();
    }
    to your Dog class.
    The article I posted above explains it all in detail.

  7. #27
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Well, I dont get any errors. Just noticed there was one error in the readInt method but fixed it. Runs fine with Eclipse.

  8. #28
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Just add
    Java Code:
    @Override
    public int hashCode() {
    	return name.hashCode();
    }
    to your Dog class.
    The article I posted above explains it all in detail.
    Thank you very much, I have to read that article more carefully cause I dont fully understand the whole point of this.

    well might get back to you if I get stuck again in this program.

    Thanks again!

  9. #29
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Stuck again!

    I am trying to do a method where it shows a list of dogs and then I can either add a new dog or make a new dog with a name that already exists. So that it wont ask the name of the dog. Just add all the other attributes.
    This is what i have done
    Java Code:
    	private void addDog(){
    		System.out.println("Add a new dog or make a review of existing dog?");
    		printDogs();
    		System.out.print("Write new or the name of the dog: " );
    		String choice = readText();
    			
    		if(choice.equalsIgnoreCase("new")){
    			System.out.print("Give dogs name; ");
    			String name = readText();
    			System.out.print("Give a grade: ");
    			int grade = readInt();
    			System.out.print("Give info: ");
    			String info = readText();
    			System.out.print("Give type: ");
    			String type = readText();
    			Dog d = new Dog(name,grade,info,type);
    			animals.add(d);
    			}
    			boolean ok = false;
    			
    			Iterator<Animal> i = animals.iterator();
    			while(i.hasNext()){
    				Dog d = (Dog) i.next();
    				if(d.getName().equalsIgnoreCase(choice)){
    					ok = true;
    					
    				}
    				if(ok){
    					System.out.print("Give a grade: ");
    					int grade = readInt();
    					System.out.print("Give info: ");
    					String info = readText();
    					System.out.print("Give type: ");
    					String type = readText();
    					Dog dnew = new Dog(choice,grade,info,type);
    					animals.add(dnew);
    					
    				}
    			}
    	}
    It gives me this kind of error if I try to write review with a dogs name that is already in the arraylist.

    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( Unknown Source)
    at java.util.AbstractList$Itr.next(Unknown Source)


    I read that it cant iterate while it is modificating the arraylist. Am I correct here? Thats why added the extra boolean there in it.

    Also I know that it is not best way for user to input his/her choices with a text. So how could i make it so that if the user want to input/review a dog
    the list would show up as
    1. Jeff
    2. Dogg
    3. Add new

    so that the last one index would be always "add new" option?
    And how this get mixed up when i make class of cats. If the dogs and cats are in the samearraylist?

    Appreciate your help

  10. #30
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Just a clarificatio needed from ur end.

    Are u strictly asked to follow this fashion for doing this task ?

    Iam feeling that u are complicating the things.
    u are having lot of simple methodologies for doing this task.
    Ramya:cool:

  11. #31
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    If there is a simple way I would appreciate info on that.

    BAsically I want to have an option to add a new dog or just write a review or something like that on a dog that names exists in the arraylist. Thats why i thought it might be easier to modify these by index or is it? and how confusing it gets if there is cats and dogs in the same arraylist?

  12. #32
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    1.You can take 2 arraylist one for dogs and another array for cats.

    2.Just get the input from the user as a string.

    3.if it exists dont add to arraylist else add.

    Just take thing as a start up and then you can put some efficient way for improving.
    Ramya:cool:

  13. #33
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Ok, if i then save these ArrayLists in a samefile lets say Zoo.dat. Do these arrays get messed up or it just adds them one after each other.
    Let say i Do it this way.

    Java Code:
    FileOutputStream fos = new FileOutputStream("Zoo.dat");
    		ObjectOutputStream oos = new ObjectOutputStream(fos);
    		oos.writeObject(dogs); //ArrayList of dogs
                    oos.writeObject(cats); //ArrayList of cats
    		fos.close();
    Will that work?

    And other questions is how do I fetch these two different arrays from the file?

  14. #34
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,654
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by soketti View Post
    Ok, if i then save these ArrayLists in a samefile lets say Zoo.dat. Do these arrays get messed up or it just adds them one after each other.
    Let say i Do it this way.

    Java Code:
    FileOutputStream fos = new FileOutputStream("Zoo.dat");
    		ObjectOutputStream oos = new ObjectOutputStream(fos);
    		oos.writeObject(dogs); //ArrayList of dogs
                    oos.writeObject(cats); //ArrayList of cats
    		fos.close();
    Will that work?

    And other questions is how do I fetch these two different arrays from the file?
    As long as your Cat and Dog classes implement the Serializable interface it will work. For reading: just create an ObjectInputStream given that file and read your two objects back in again.

    kind regards,

    Jos

  15. #35
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    As the abstract class Animal implements the Serializable so then will the dog and cat class also implements that, right?

    Is there a point having the abstract class Animal anymore then?
    If i create the two arraylist like this
    Java Code:
    static List<Dog> dogs = new ArrayList<Dog>();
    static List<Cat> cats = new ArrayList<Cat>();



    About the reading the two arraylists back again.
    If I do it this way:
    Java Code:
    FileInputStream fin = new FileInputStream("Zoo.dat");
    ObjectInputStream ois = new ObjectInputStream(fin); 
    
    dogs = ois.readObject();
    cats = ois.readObject();
    How do I check if the Object is a cat or a dog from the file and put in the right array?

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,654
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by soketti View Post
    As the abstract class Animal implements the Serializable so then will the dog and cat class also implements that, right?

    Is there a point having the abstract class Animal anymore then?
    If i create the two arraylist like this
    Java Code:
    static List<Dog> dogs = new ArrayList<Dog>();
    static List<Cat> cats = new ArrayList<Cat>();



    About the reading the two arraylists back again.
    If I do it this way:
    Java Code:
    FileInputStream fin = new FileInputStream("Zoo.dat");
    ObjectInputStream ois = new ObjectInputStream(fin); 
    
    dogs = ois.readObject();
    cats = ois.readObject();
    How do I check if the Object is a cat or a dog from the file and put in the right array?
    Yes, if a super class implements a certain interface then all of its sub classes (indirectly) implement that same interface. If two classes have nothing in common (not even behaviour defined by an interface) there is no need to make them explicitly extend a super class (the Object class is the super class of everything). Your software 'knows' which list was written first (either the List<Dog> or the List<Cat>). Your software that reads the file again also is supposed to 'know' that. You can always fiddle a bit with the 'instanceof' operator or the .getClass() method to figure things out programmatically.

    kind regards,

    Jos

  17. #37
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Have question about the ArrayLists again :)
    How can I print the arrraylist out only with the dogs name and the index number?
    And also that if there is a dog with same name twice it is printed out only once.
    Let's say I have dogs Jeff, Jeff,Doug,Charlie.
    I would like the output to be
    1)Jeff
    2)Doug
    3) Charlie
    4) Add a new one

    As I have this now that prints right. If there is two dogs with the same name it only prints it out once. But the question is how can print out the dog with a index number as mentioned above? This is because the user could just pick a number and then it would create a new object named by what Dog is at the index. This way dont have to use Strings.

    Java Code:
    	private void printDogs(){
    		
    		Set<Dog> doggies = new HashSet<Dog>(dogs);
    		Iterator<Dog> i = doggies.iterator();
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    			System.out.println(d.getName());
    		}	
    	}

  18. #38
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Suppose that you declare and initialize (to zero) an int before your while loop and increment that int as soon as you enter that loop, printing the value of that int inside the loop prints out 1,2,3 ...

  19. #39
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Suppose that you declare and initialize (to zero) an int before your while loop and increment that int as soon as you enter that loop, printing the value of that int inside the loop prints out 1,2,3 ...
    Yeah I tried that, but came up with a new problem. Suppose the menu shows it like this.

    1) Jeff
    2) Graig

    User inputs 1. How I am suppose to get that object now? So i could add a new object with name Jeff.

  20. #40
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM
  2. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •