Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: Method not working

  1. #1
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Method not working

    Hello friends!

    I've got this two methods that are supposed to return an ArrayList with numbers of rows of a jtable. The thing is that the second method works just fine but the first one simply doesn't, it returns an empty list.

    Thanks all in advance

    Note:
    1) Both columns in jtable contain dates in the form "DD/MM/YYYY".
    2) ReadLine class with CompareDate method returns the current date in the same format "DD/MM/YYYY".

    CODE:

    Java Code:
    public ArrayList<Integer> getRowIndexesColumn2(JTable table){
        ArrayList<Integer> rowsinTable = new ArrayList<>();
        int rowCount = table.getRowCount();
            
            for (int i=0; i<rowCount; i++){
                for(String value : DateCount(table.getValueAt(i, 2).toString())){
                    if (new ReadLine().compareDate().equals(value)){
                        rowsinTable.add(i);
                        System.out.println(value);
                    }
                }
            }   
        return rowsinTable;
        }
        
        public ArrayList<Integer> getRowIndexesColumn6(JTable table){
        ArrayList<Integer> rowsinTable = new ArrayList<>();
        int rowCount = table.getRowCount();
            
            for (int i=0; i<rowCount; i++){
                for(String value : DateCount(table.getValueAt(i, 6).toString())){
                    if (new ReadLine().compareDate().equals(value)){
                        rowsinTable.add(i);
                    }
                }
            }   
        return rowsinTable;
        }
    Method DateCount:

    Java Code:
    public ArrayList<String> DateCount(String contString){  //this method takes one date (A) (string from jtable row) and calculates the date (B) of 15 days backwards, then returns an ArrayList of the dates between A and B.
        
        ArrayList<String> weekList = new ArrayList<>();
        String[] realDate = contString.split("/");
        int DDb = Integer.parseInt(realDate[0]);
        int MMb = Integer.parseInt(realDate[1]);
        int AAb = Integer.parseInt(realDate[2]);
        int newDDb = 0;
        int newMMb = 0;
        int newAAb = 0;
        int DMANT = 0;
        int DMPOST = 0;
        
            switch (MMb) {
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    DMANT = 30;
                    break;
                case 1:
                case 2:
                case 4:
                case 6:
                case 9:
                case 11:
                    DMANT = 31;
                    break;
                case 3:
                    DMANT = 28;
                    break;
                default:
                    break;
            }
            
            if (DDb < 16){
                newDDb = DMANT-(15-DDb);
                newMMb = MMb-1;
                newAAb = AAb;
                
                if (MMb==1){
                newAAb = AAb-1;
                newMMb = 12;
                }
            }
            
            if (DDb >= 16){
                newDDb = DDb-15;
                newMMb = MMb;
                newAAb = AAb;
            }
            
            if (DDb == 15){
                newDDb = DMANT;
                newMMb = MMb-1;
                newAAb = AAb;
                
                if (MMb == 1){
                    newAAb = AAb-1;
                    newMMb = 12;
                }
            }
            
            switch (newMMb) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    DMPOST = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    DMPOST = 30;
                    break;
                case 2:
                    DMPOST = 28;
                    break;
                default:
                    break;
            }
            
            int DIFF = DMPOST - newDDb;
            
            if(DIFF < 14){
                for(int i=1; i<(DIFF+1); i++){
                    int tempDDb = newDDb+i;
                    String StempDDb = Integer.toString(tempDDb);
                    String StempMMb = Integer.toString(newMMb);
                    if (StempDDb.length()==1){
                        StempDDb = "0"+StempDDb;
                    }
                    if (StempMMb.length()==1){
                        StempMMb = "0"+StempMMb;
                    }
                    weekList.add(StempDDb+"/"+StempMMb+"/"+newAAb);
                }
                if(newMMb==12){
                    newAAb++;
                    newMMb=1;
                }else{
                    newMMb++;
                }
                for (int i=1; i<(16-DIFF); i++){
                    String Si = Integer.toString(i);
                    String StempMMb = Integer.toString(newMMb);
                    if(Si.length()==1){
                        Si = "0"+Si;
                    }              
                    if (StempMMb.length()==1){
                        StempMMb = "0"+StempMMb;
                    }
                    weekList.add(Si+"/"+StempMMb+"/"+newAAb);
                }
                
            }
            
            if(DIFF > 14){
                for(int i=1; i<16; i++){
                    int tempDDb = newDDb+i;
                    String StempDDb = Integer.toString(tempDDb);
                    String StempMMb = Integer.toString(newMMb);
                    if (StempDDb.length()==1){
                        StempDDb = "0"+StempDDb;
                    }
                    if (StempMMb.length()==1){
                        StempMMb = "0"+StempMMb;
                    }
                    weekList.add(StempDDb+"/"+StempMMb+"/"+newAAb);
                }
            }
            
            if(DIFF == 14){
                for(int i=1; i<15; i++){
                    int tempDDb = newDDb+i;
                    String StempDDb = Integer.toString(tempDDb);
                    String StempMMb = Integer.toString(newMMb);
                    if (StempDDb.length()==1){
                        StempDDb = "0"+StempDDb;
                    }
                    if (StempMMb.length()==1){
                        StempMMb = "0"+StempMMb;
                    }
                    weekList.add(StempDDb+"/"+StempMMb+"/"+newAAb);
                }
                if(newMMb==12){
                    newAAb++;
                    newMMb=1;
                }else{
                    newMMb++;
                }
                String StempMMb = Integer.toString(newMMb);
                    if (StempMMb.length()==1){
                        StempMMb = "0"+StempMMb;
                    }
                weekList.add("01"+"/"+StempMMb+"/"+newAAb); 
            }
    
        return weekList;
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method not working

    What have you done to debug your program? In situations like this it is normal practice to sprinkle print statements
    throughout you code to track the progress of how the method is executing. Each one should indicate where you are
    in the execution and also print out what you expect key values to be.


    Regards,
    Jim
    Last edited by jim829; 11-26-2016 at 04:07 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not working

    the first one simply doesn't, it returns an empty list.
    It would help if you gave the name of the method you are asking about.

    How are you debugging the code to see why it does not find any values to add to the list?
    Add some print statements that show the values of the variables being tested so you can see what the computer sees when the code is executed.

    I thought I typed really quickly but not fast enough to be first.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Re: Method not working

    Hello Jim and Norm!!

    The method i'm asking about is getRowIndexesColumn2.

    After using print in the code, when i run the program it shows the following output at the end of DateCount:

    [19/02/2016, 20/02/2016, 21/02/2016, 22/02/2016, 23/02/2016, 24/02/2016, 25/02/2016, 26/02/2016, 27/02/2016, 28/02/2016, 01/03/2016, 02/03/2016, 03/03/2016, 04/03/2016, 05/03/2016]
    [01/11/2016, 02/11/2016, 03/11/2016, 04/11/2016, 05/11/2016, 06/11/2016, 07/11/2016, 08/11/2016, 09/11/2016, 10/11/2016, 11/11/2016, 12/11/2016, 13/11/2016, 14/11/2016, 15/11/2016]
    [05/03/2015, 06/03/2015, 07/03/2015, 08/03/2015, 09/03/2015, 10/03/2015, 11/03/2015, 12/03/2015, 13/03/2015, 14/03/2015, 15/03/2015, 16/03/2015, 17/03/2015, 18/03/2015, 19/03/2015]
    [11/03/2017, 12/03/2017, 13/03/2017, 14/03/2017, 15/03/2017, 16/03/2017, 17/03/2017, 18/03/2017, 19/03/2017, 20/03/2017, 21/03/2017, 22/03/2017, 23/03/2017, 24/03/2017, 25/03/2017]
    []
    [12/11/2016, 13/11/2016, 14/11/2016, 15/11/2016, 16/11/2016, 17/11/2016, 18/11/2016, 19/11/2016, 20/11/2016, 21/11/2016, 22/11/2016, 23/11/2016, 24/11/2016, 25/11/2016, 26/11/2016]
    [29/04/2017, 30/04/2017, 01/05/2017, 02/05/2017, 03/05/2017, 04/05/2017, 05/05/2017, 06/05/2017, 07/05/2017, 08/05/2017, 09/05/2017, 10/05/2017, 11/05/2017, 12/05/2017, 13/05/2017]
    [26/11/2016, 27/11/2016, 28/11/2016, 29/11/2016, 30/11/2016, 01/12/2016, 02/12/2016, 03/12/2016, 04/12/2016, 05/12/2016, 06/12/2016, 07/12/2016, 08/12/2016, 09/12/2016, 10/12/2016]
    [11/09/2017, 12/09/2017, 13/09/2017, 14/09/2017, 15/09/2017, 16/09/2017, 17/09/2017, 18/09/2017, 19/09/2017, 20/09/2017, 21/09/2017, 22/09/2017, 23/09/2017, 24/09/2017, 25/09/2017]

    It is correctly creating the list of dates from each row in columns 2 and 6, but it is printing something empty in the middle.

    After the methods getRowIndexesColumn2/6 it shows:
    [0, 2] (these values are expected)
    []

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not working

    Where in the print out are the values being tested to select items to add to the list?
    For example:
    Java Code:
       if (new ReadLine().compareDate().equals(value)){
                        rowsinTable.add(i);
                        System.out.println(value);
                    }
    What are the contents of the two values that are compared by the equals method?
    new ReadLine().compareDate()
    value
    If they were equal then the rowsinTable.add(i); statement would add a value to the list.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Re: Method not working

    Oh yes, sorry, i forgot to say: ReadLine().compareDate() returns the current date (e.g. 26/11/2016). So the method equals compares 26/11/2016 with the dates of the ArrayList and if it finds a match then that row (i) is added to rowsinTable.

    printing out value in both getRowIndexes methods:

    for column2:
    [] //there is actually one row in the table that should be shown.

    for column6:
    26/11/2016
    26/11/2016
    []

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method not working

    there is actually one row in the table that should be shown.
    Did you print out the values that are being compared so you can see what the computed sees when the equals() method is executed?
    If the equals() method never returns true, then nothing will be added to the list.
    The print out will show you if there are any equal values.

    Note: Creating a new instance of ReadLine and calling its compareDate() (Name should be getDate) method should be done outside of the loop and the value saved to be used inside of the loop.

    Could there be a single getRowIndexesColumn method that is passed a 2 or a 6
    instead of having two almost identical methods with hardcoded 2 and 6?
    Last edited by Norm; 11-26-2016 at 05:49 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Re: Method not working

    Thank you Norm!!

    I have created the instance of ReadLine outside the loop and now it works

    The code:
    Java Code:
    public List<Integer> getRowIndexesColumn2(JTable table){
        List<Integer> rowsinTable = new ArrayList<>();
        int rowCount = table.getRowCount();
        String currentDate = new ReadLine().compareDate();
    
            for (int i=0; i<rowCount; i++){
                for(String value : DateCount(table.getValueAt(i, 2).toString())){
                    if (currentDate.equals(value)){
                        rowsinTable.add(i);   
                    }
                }
            }
            
        return rowsinTable;
        }
    From now on, never again creating instances inside loops hehehe.

    Thank you again!

    PD: i will work on your idea of modifying the method to avoid having two identical

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method not working

    Quote Originally Posted by Piyiyo View Post
    From now on, never again creating instances inside loops hehehe.
    Well, you don't want go to extremes. There are many situations when some code demands that a new instance be
    created in a loop. For example, if you want to populate an array with some different instances of Strings,
    BigIntegers, or other classes.

    Regards,
    Jim
    Piyiyo likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. JSP EL function is not working for var arg method.
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-28-2013, 01:12 PM
  2. Method not working as intended
    By Pojahn_M in forum New To Java
    Replies: 6
    Last Post: 01-09-2012, 12:00 AM
  3. method not working correctly
    By r1b in forum New To Java
    Replies: 4
    Last Post: 01-04-2012, 08:09 PM
  4. Method Not Working
    By Jacob1028555 in forum New To Java
    Replies: 5
    Last Post: 06-10-2011, 01:08 AM
  5. ugh another method that isn't working
    By jjth39347 in forum New To Java
    Replies: 14
    Last Post: 04-17-2011, 09:57 PM

Posting Permissions

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