Results 1 to 2 of 2
  1. #1
    hockey9999 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Programming Project trouble..

    I am working on a project for my class and am completely stuck..

    There is also another thread about the same project here - How do I create a class called Dog in Java?

    My assignment is as follows:

    Design and implement a class called LostDog that contains instance data that represents the dog’s name, gender, breed, and owner status (as a Boolean flag to represent whether the owner is unknown or found).

    Define the LostDog constructor to accept and initialize instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one line description of the dog.

    Create a driver class call DogPound that will contain an array that will reference all lost dogs, using the LostDog object. You will use an input file to upload lost dogs into the array (the file and code to process it will be provided). Copy the input file lostdogs.csv to the project folder of your eclipse workspace.

    In addition, include code to enable someone to search the pound for a lost dog.

    Prompt the user to enter the name of the dog, and search the array to see if any dogs match that name (you can assume all dog names in the pound are unique). If there is a match, change the dog’s status to “owner found” and report to the owner that you have found the dog.
    My issue is with getting the data from the input file into the array. The professor provided us with the code, but never covered it in class so it has me stumped.. I am fairly confident that once I figure this out, the rest of the project will go fairly easily..

    I know that the problem lies within this piece of code. The problem is that the professor didn't teach anything about the StringTokenizer, or really anything related to this code..

    Java Code:
    File file = new File("lostdogs.csv");
             
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
            String line = null;
                     
            //read each line of text file
            while((line = bufRdr.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(line,",");
                inName = st.nextToken();
                //input the breed and the gender
                //create new LostDog object and add it to the array
                //increment index            
            }
             
            //close the file
            bufRdr.close();
    What is the purpose of the inName variable here? If I were to follow the professors naming conventions for the variables, should I have an inName variable in my LostDog class? I assume that I would also need inBreed and inGender too in order to get all of the data into the array?

    This is what I have so far for both classes. I am fairly certain that my LostDog class is correct, but I know I am WAY off on the DogPound class..

    Any help anyone can give us would be greatly appreciated!!

    DogPound.java
    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class DogPound {
    
        /**
         * @param args
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            
            
            int SIZE = 10;
            LostDog [] dogPoundArray = new LostDog[SIZE];
            int index = 0;
            int count = 0;
            
            //Not sure if any of this is correct or needs to be here in the first place. 
            String inName, inBreed, inGender, userInput;
            LostDog input = null; 
            
            
            //put any other needed variables here
            
            Scanner scan = new Scanner (System.in);
                     
            File file = new File("lostdogs.csv");
             
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
            String line = null;
                     
            //read each line of text file - This is where I am really stuck.. 
            while((line = bufRdr.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(line,",");
                
                while (st.hasMoreElements()&& count<SIZE)
                {
                
                inName = st.nextToken();
                dogPoundArray[index]= input;
                index++;
                count++;
                }
                
                
                //input the breed and the gender
                //create new LostDog object and add it to the array
                //increment index            
            }
             
            //close the file
            bufRdr.close();
            
            //add code to search for a dog    
            //add in optional extras            
    
            //Test to see if the array has data in it - So far returns Null. 
            for (int i = 0; i<dogPoundArray.length; i++)
                System.out.println(dogPoundArray[i]);
            
            //System.out.println("Please enter the name of your lost dog: "); 
            //userInput = scan.next();
            
        }
    
    }
    LostDog.java

    Java Code:
    public class LostDog {
    
    private String name, breed, gender; 
    private boolean ownerStatus;     
    
        public LostDog (String called, String type, String sex, boolean location)
    {
    
        name = called;
        gender = sex;
        breed = type;
        ownerStatus = location; 
        
        
    }
    
        public String getName ()
        
        {
            
            return name;
        }
        
        public void setName(String called)
    
        {
            
        name = called;
            
        }
        
        public String getBreed()
        
        {
        
            return breed;
            
        }
        
        public void setBreed(String type)
        
        {
            
        breed = type;
            
        }
        
        public String getGender ()
        
        {
            return gender;
        }
        
        
        public void setGender (String sex)
        
        {
            gender = sex;
        }
        
        
        public boolean getOwnerStatus ()
        
        {
            return ownerStatus;
        }
        
        public void setOwnerStatus ()
        
        {
            ownerStatus = true;
        }
        
        
        public String toString()
        
        {
        return  "Breed - " + breed + ", Name - " + name + ", Gender - " + gender + "Owner Status" + ownerStatus; 
        }
        
    }
    Last edited by hockey9999; 12-19-2010 at 10:01 PM.

  2. #2
    rahilz is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    So I imported the file Lostdog.csv in default package by drag and drop and it gives me this error


    Exception in thread "main" java.io.FileNotFoundException: lostdogs.csv (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at DogPound.main(DogPound.java:30)

Similar Threads

  1. Fairly Simple (I think) programming project
    By Glyph in forum Jobs Offered
    Replies: 0
    Last Post: 10-22-2010, 12:16 AM
  2. Replies: 2
    Last Post: 04-12-2010, 05:06 AM
  3. Having trouble with Java Project
    By raakesh in forum New To Java
    Replies: 7
    Last Post: 06-05-2008, 06:38 AM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •