Thread: Method help

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0

    Method help


    I am trying to design a couple methods for my program, and add, locate, and remove method. It is a Person class with 3 array attributes, first and last name and id number, i need to sort the attributes as the information is inputed and not apply a sort method after the fact. A lot of my confusion is the application of the methods to objects. The attributes are of type record and record is going to be the array the maintains its sort. So here is what i have so far on my add method:
    Code:
    public void add(String f, String l, String id)    
    {       
     int j;       
    for(j=0; j<nElems; j++)                 
    if(a[j].compareTo(data)>=0)                   
     break;       
    for(int k=nElems; k>j; k--)        
    a[k] = a[k-1];       
    a[j] = data;                     
    nElems++;       
    }
    This is the first of my 3 methods, thanks for your time
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what problems you are having?

    The posted code needs to be properly formatted to make it readable.
    The indentations are too small.
    There are no {}s with the if and for statements. Missing {}s make the code very hard to read and understand.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Ok sorry about the tag errors. My instance variables in the DataStructure class are as follows:
    private DataRecord[] data = new DataRecord[100];
    Code:
        private static String key = null;
        private static int nElems = 0;
        private static final String [] a = null;
        public SortedArray fName;
        public SortedArray lName;
        public SortedArray Id;
    sorted array is an inner class of DataStructure that maintains the order of the three attributes in the DataStructure.
    So right now my problem is i am trying to implement my search and add methods in my main but neither of them seem to be working properly my search method is as follows:
    Code:
    public boolean search(String key) 
        {
    	int j;
    	int count =0;
    	boolean found = false;
    	DataStructure[] a = null;
    	int nElems = 0;
    	for(j =0; j<nElems; j++)
                if(a[j].key.equals(key))
                   count = count+1;
    	if(count != 0)
    	   found = true;
    	return found;
       }
    it is implemented in the main is so:
    found = struct.search(tempID);
    the class that the methods are in are called DataStructure. The type casting of struct seems to keep the compiler happy when trying to apply the method.
    the add method is as follows:

    Code:
    public void add(String f, String l, String id)
       {
          int j;
          for(j=0; j<nElems; j++)  
             if(a[j].compareTo(a[j+1])>=0) 
                break;
          for(int k=nElems; k>j; k--)  
             a[k] = a[k-1];          
          nElems++;   
       }
    the method is applied in the main as follows:
    structure.add(name1,name2,tempID);
    I can send you the whole code if you want its just kind of lengthy so i was trying to show the important parts at this time. Thanks.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    There are no {}s with the if and for statements. Missing {}s make the code very hard to read and understand.

    trying to implement my search and add methods in my main but neither of them seem to be working properly
    Can you explain what results you get from the methods and what "working properly" means.

    Can you make a small, complete program that compiles, executes and shows the problem?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    the {}s are not needed for the method and so i don't include them because they are not apart of my method.
    My main method asks 9 options add, delete search for student, and list the information in different orders. So in my main i am asked to enter an id i apply the above method it decides the id is not found so i enter first and last name. I am then prompted again and i enter the same id number and it continues like it is not found but my loop is supposed to detect if the number is in use or not:
    Code:
     do
            {
                System.out.println("Enter a unique ID number to add");
                tempID=keyboard.nextLine();
                found = struct.search(tempID);
                if (found)
                {
                    System.out.println("ID already in use");
                    System.out.println("Please re-enter a unique ID");
                }
            }while (found);
    after i enter the id and it is not found the following part in the main goes like this:

    Code:
           System.out.println("Enter first name");
           name1=keyboard.nextLine();
           System.out.println("Enter last name");
           name2=keyboard.nextLine();
           struct.add(name1,name2,tempID);
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you make a small, complete program that compiles, executes and shows the problem?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Sure, no problem do you just want my main and my other classes? Thanks for your time.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Don't post any code not related to using the methods with the problems.
    Define some arrays with some data and call the methods for testing. Keep it as simple as possible.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Code:
    import java.util.*;
    public class tester 
    {
        private static String fname, lname, tempID;
        private static Scanner keyboard = new Scanner(System.in); 
        private static boolean found;
        private static DataStructure s = new DataStructure(5);
        public static void main(String[] args)
        {
            int response;
            Scanner keyboard=new Scanner(System.in);
            do
            {
                System.out.println(" 1 Add a new student");
                System.out.println(" 0 End");
                response=keyboard.nextInt();
                switch (response)
                {
                    case 1: add();
                            break;              
                }
             } while (response!=0);
        }
        public static void add()
        {
            do
            {
                System.out.println("Enter a unique ID number to add");
                tempID=keyboard.nextLine();
                found = s.search(tempID);
                if (found)
                {
                    System.out.println("ID already in use");
                    System.out.println("Please re-enter a unique ID");
                }
            }
           while (found);
           System.out.println("Enter first name");
           fname=keyboard.nextLine();
           System.out.println("Enter last name");
           lname=keyboard.nextLine();
           s.add(fname,lname,tempID);
        }
    }
    public class DataStructure
    {
        
        private DataRecord[] data = new DataRecord[100];
        private static String key = null;
        private static int nElems = 0;
        private static final String [] a = null;
        public OrdArray fName;
        public OrdArray lName;
        public OrdArray Id;
        public int max;
        public DataStructure(int maxSize)
        {
            max = maxSize;
            this.fName = fName;
            this.lName = lName;
            this.Id = Id;
        }
       
        public void displayDataStructure()
        {
            //System.out.print("First Name     Last Name       ID");
            System.out.println(fName + " " +lName+" "+Id);
        }
        public OrdArray getfName()
        {
            return fName;
        }
        public OrdArray getId()
        {
            return Id;
        }
        public OrdArray getlName()
        {
            return lName;
        }
        public boolean equals(DataStructure otherDataStructure)
        {
            return (Id.equals(otherDataStructure.Id));
        }
        public static boolean compareID(DataStructure Id1, DataStructure Id2)
        {
            if(Id1.getId().equals(Id2.getId()))
                return true;
            else 
                return false;
        }
       
       public boolean search(String key) 
        {
    	int j;
    	int count =0;
    	boolean found = false;
    	DataStructure[] a = null;
    	int nElems = 0;
    	for(j =0; j<nElems; j++)
                if(a[j].key.equals(key))
                    count = count+1;
    	if(count != 0)
    	   found = true;
    	return found;
       } 
       public void add(String f, String l, String id)
       {
          int j;
          for(j=0; j<nElems; j++)  
             if(a[j].compareTo(a[j+1])>=0) 
                break;
          for(int k=nElems; k>j; k--)  
             a[k] = a[k-1];          
          nElems++;   
       }
       }
    It will not add the information to the DataStructure after execution of the main.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    It will not add the information to the DataStructure after execution of the main.
    When the execution of main() ends, the program exits? Can you explain what you mean by "after execution of the main"?

    Also please copy and paste here the contents of the console window from when you execute the tester program. Add some comments to the post that shows what is wrong and what it should be.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.


    NOTE: The posted code will not compile without errors.
    Can you make a simpler testing program that does NOT require so many other classes?

    ALSO There are no {}s with the if and for statements. Missing {}s make the code very hard to read and understand.
    Please add {}s to all the if and for statements.
    Last edited by NormR; February 9th, 2013 at 11:19 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    This is kind of a larger program so it has multiple classes. I mean it's hard for me to go through and rewrite smaller code and main methods over and over when i know that this program is so much larger and contains more than meets the eye. The code i sent you isn't every class contained in the package it just is what i thought you were looking for. The console is not giving me an error it is not executing properly. So when the id is entered and not found, it allows you to enter a name. In the do while loop in add() you can see that i have a if statement set up to check if the id being entered has already been entered. When you cycle back through a second time (as this program does not exit after entering just one name, it prompts you to 1:eneter another id, or 2:exit) and enter the the same id it does not stop you it continues as if it is a new unused id.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If the code won't compile and execute it can not be tested.
    What method(s) are you trying to test? How can you call the method(s) and then verify that the method(s) worked properly?
    Code to do that is all that is needed for testing. The rest of the code is just in the way for testing one or two methods.
    it continues as if it is a new unused id
    What method fails there? Can you make a small program to test that method?

    The console is not giving me an error it is not executing properly.
    Post the contents of the console and add some comments saying what is wrong and what the code should do instead.
    Last edited by NormR; February 9th, 2013 at 12:06 PM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Ok, so i actually am getting a different error as well. Here is console output with comments:

    1 Add a new student
    0 End
    1
    Enter a unique ID number to add
    1234 //New id number here
    Enter first name
    Ernest
    Enter last name
    Bollard
    1 Add a new student
    0 End 1
    Enter a unique ID number to add
    1234 //Same id number again here
    Enter first name // Allowed me to continue anyway
    Kim
    Enter last name
    kommy //Recieved exception error here
    Exception error:
    java.lang.NullPointerException
    at DataStructure.add(DataStrucute.java:66)
    at tester.add(tester.java:42)
    at tester.main(tester.java:19)
    So something is wrong with my add method in the way i compare the elements to add them in an ordered way??
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    java.lang.NullPointerException
    at DataStructure.add(DataStrucute.java:66)
    At line 66 there was a variable with a null value when that line was executed. Look at line 66, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Okay worked it out it was the add method still having the other problem though
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo