Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Angry Gremlins in my code, I can't seem to trace

    I can't seem to tract back what is wrong with class.
    The course provides the Main.java, and we are asked to write the phone book class to plug in. I have written it, but it's spitting out the wrong data!
    so here is the Main.java

    Java Code:
    public class Main {
    
      public static void main(String[] args) {
        int x;
        PhoneBook pb = new PhoneBook();
        System.out.println(pb);
        x = pb.nextIndex();
        
        if(x != -1) {
          pb.setName("Bob", x);
          pb.setNumber("0987654321", x);
        }
        else {
          System.out.println("Error: No empty phone book entries.BOB");
        }
        pb.setName("Mike", 0);
        pb.setNumber("1234567890", 0);
        pb.setName(null, 1);
        pb.setNumber(null, 1);
        System.out.println(pb);
        x = pb.nextIndex();
        if(x != -1) {
          pb.setName("Bob", x);
          pb.setNumber("0987654321", x);
        }
        else {
          System.out.println("Error: No empty phone book entries.");
        }
        System.out.println("Entry for Mike at index: " + pb.getRecord("Mike"));
        System.out.println("Entry number 2 (index 1) is: ");
        String temp[] = pb.getRecord(1);
        if(temp != null) {
          System.out.println("\t" + temp[0]);
          System.out.println("\t" + temp[1]);
        }
        else {
          System.out.println("Null entry.");
        }
        System.out.println("\n" + pb);
      }
    }
    and here is my broken PhoneBook class. Something is causing the indexes to be off, and I can't tell what. I Thought it was an issue of putting the arguments into the whole currentName/currentNumber/recordNumber thing, but it still fails when I just use i.The first output is correct, names matching numbers etc, then it starts to fill in the elements from listOfNames[9] into everything else, so the last output reads null on numbers and everyones name is Satan(so named for his chronic evil appearance in my code today!).

    Java Code:
    public class PhoneBook 
    {
      private final short entries = 10;
      private String[] listOfNames = new String[entries];
      private String[] listOfPhoneNumbers = new String[entries];
      private String currentName = "";
      private String currentPhone = "";
      private int recordNumber = 0;
      
      
                    //--Constructor--//
            public PhoneBook()
                  {
                    //--Initialize default Values by  init()--//
                    init();
                      
                  }
    public void init()
          {//--Init--//
              //-- I originally had this as a loop to avoid hard numbers, but the output was useless--//
      listOfNames[0] = new String("Billy");
      listOfPhoneNumbers[0] = new String("4529261444");
      
      listOfNames[1] = new String("Jenny");
      listOfPhoneNumbers[1] = new String("2428675309");
      
      listOfNames[2] = new String("null");
      listOfPhoneNumbers[2] = new String("null");
      
      listOfNames[3] = new String("Lena");
      listOfPhoneNumbers[3] = new String("8663219898");
      
      listOfNames[4] = new String("Jared");
      listOfPhoneNumbers[4] = new String("6157151141");
      
      listOfNames[5] = new String("Payton");
      listOfPhoneNumbers[5] = new String("5802295678");
      
      listOfNames[6] = new String("Derek");
      listOfPhoneNumbers[6] = new String("2561387835");
      //System.out.println(listOfNames[6] + " TEST 0 ");//DEBUG
      listOfNames[7] = new String("Yanu");
      listOfPhoneNumbers[7] = new String("6548261573");
      
      listOfNames[8] = new String("Fred");
      listOfPhoneNumbers[8] = new String("1258641853");
      
      listOfNames[9] = new String("Satan");
      listOfPhoneNumbers[9] = new String("18666666666");
          }//--Init--//
    
      public String toString()
          {
            String dString = "";
            for(int i = 0;i < entries;i++)
                {
              //-- Set Data for formatting loop--//
                  recordNumber = i;
                  currentName = listOfNames[recordNumber];
                  currentPhone = listOfPhoneNumbers[recordNumber];
                  //System.out.println(currentName + " TEST ");
                  dString = dString + " Entry #" + recordNumber + "::" ;
                  dString = dString + " " + "Name is " + currentName;
                  dString = dString + " and their Number is " + currentPhone +" \n";
                }
            return dString;
          }
      
      public boolean setName(String name, int index)
          {
            boolean success = false;
            recordNumber = index;
            currentPhone = name;
            if(index < entries)
                {
                 
                  //--No check for NULL,UNCONFIRMED OVERWRITE this is not final--//
                  listOfNames[recordNumber] = currentName;
                  System.out.println(currentName + recordNumber);//debug
                  success = true;
                 }
                  else
                      {
                    //--Jive Tukey!--//
                        success = false;
                      }
              
            
            return success;
        
          }
      public boolean setNumber(String number, int index)
      {
        boolean success = false;
        if(index < entries )
            {
              recordNumber = index;
              currentPhone = number;
            
              listOfPhoneNumbers[recordNumber] = currentPhone;
              success = true;
             }
              else
                  {
                //--Jive Tukey!--//
                    success = false;
                  }
          
        
        return success;
    
      }
      
     public String[] getRecord(int recordNumber)
       {
       String[] returnData = new String[2];
           if("NULL".equalsIgnoreCase(listOfNames[recordNumber]))
               {
               returnData[0] = "NULL";
               returnData[1] = "NULL";
               }   
               else
                   {
                 currentName = listOfNames[recordNumber];
                 currentPhone = listOfPhoneNumbers[recordNumber];
                 returnData[0] = currentName;
                 returnData[1] = currentPhone;
                   }
       return returnData;
       }
     
     public int nextIndex()
           {
             int position = -1;
             
                for(int i = 0; i < entries ;i++)
                {
                  recordNumber = i;
                  if("NULL".equalsIgnoreCase(listOfNames[recordNumber]))
                      {
                        
                        position = i;
                        break;
                      }
                      else
                          {
                        position = -1;
                        //break;
                          }
                     
                }
             return position;
           }
     public int getRecord(String name)
     {
       int position = -1;
       //--//
       for(int i = 0; i < entries;i++)
       {
         recordNumber = i;
         if(name.equalsIgnoreCase(listOfNames[recordNumber]))
             {
              position = i;
               break;
             }
             else
                 {
                   position = -1; 
                 }
       }
       //--//
       
       
       return position;
     }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Gremlins in my code, I can't seem to trace

    What are you seeing and what do you expect to see?
    I'm assuming there's output that you are getting that is incorrect in some way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Gremlins in my code, I can't seem to trace

    Here is the console output. The first block is correct.
    At the second block it starts spitting out Index 0 = Satan, Index 1 = Satan and so on.
    The middle ones are still correct.
    Then it says the name isn't found, even though I have debugged it and it says that the return from setName is true!
    It is also setting that records number to Null. I have been banging my head on this for a day or so now, am really frustrated, and I think I am once again missing some stupid small thing that is obvious to everyone else. I marked the erros with a comment


    Entry #0:: Name is Billy and their Number is 4529261444
    Entry #1:: Name is Jenny and their Number is 2428675309
    Entry #2:: Name is null and their Number is null
    Entry #3:: Name is Lena and their Number is 8663219898
    Entry #4:: Name is Jared and their Number is 6157151141
    Entry #5:: Name is Payton and their Number is 5802295678
    Entry #6:: Name is Derek and their Number is 2561387835
    Entry #7:: Name is Yanu and their Number is 6548261573
    Entry #8:: Name is Fred and their Number is 1258641853
    Entry #9:: Name is Satan and their Number is 18666666666

    Satan2//should be 9
    Satan0//should be 9
    Satan1//should be 9
    Entry #0:: Name is Satan and their Number is 1234567890 //shouldn't be satan
    Entry #1:: Name is Satan and their Number is null //shouldn't be null
    Entry #2:: Name is Satan and their Number is 0987654321 //shouldn't be satan
    Entry #3:: Name is Lena and their Number is 8663219898
    Entry #4:: Name is Jared and their Number is 6157151141
    Entry #5:: Name is Payton and their Number is 5802295678
    Entry #6:: Name is Derek and their Number is 2561387835
    Entry #7:: Name is Yanu and their Number is 6548261573
    Entry #8:: Name is Fred and their Number is 1258641853
    Entry #9:: Name is Satan and their Number is 18666666666 //Actually Correct

    Error: No empty phone book entries.
    Entry for Mike at index: -1 //should be two at this point
    Entry number 2 (index 1) is:
    Satan //so wrong
    null//again, not null at all

    Entry #0:: Name is Satan and their Number is 1234567890 //nope
    Entry #1:: Name is Satan and their Number is null //nope
    Entry #2:: Name is Satan and their Number is 0987654321 //why!?!
    Entry #3:: Name is Lena and their Number is 8663219898
    Entry #4:: Name is Jared and their Number is 6157151141
    Entry #5:: Name is Payton and their Number is 5802295678
    Entry #6:: Name is Derek and their Number is 2561387835
    Entry #7:: Name is Yanu and their Number is 6548261573
    Entry #8:: Name is Fred and their Number is 1258641853
    Entry #9:: Name is Satan and their Number is 18666666666

  4. #4
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Gremlins in my code, I can't seem to trace

    I am a total idiot.
    line 73 currentPhone = name; should have been currentName = name;

    So this lesson is about writing code for maintainability/utility, am I just missing the lesson altogether?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Gremlins in my code, I can't seem to trace

    Java Code:
    currentPhone = name;
    That's in the setName method.

    I would question why you need these variables (currentName and currentPhone at all), but that there is your problem...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Gremlins in my code, I can't seem to trace

    Ha!
    You beat me to it...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Gremlins in my code, I can't seem to trace

    As we use the project down the road, the phonebook class will apparently need to return the names and numbers of the currently worked on records. Thus I need to track them as I go. Thus, after any method completes, I can get the last one from the class, instead of the calling code.
    "Classes should know everything about themselves and do everything for themselves ideally.", or something to that effect. Looking into the next lesson, I kind of see how doing it this way allows for extension down the road. I wonder if it would be better to get what I needed, and just use int i for the index, and then assign the values to currentPhone and stuff afterwards.

    nope...'i' is local to the loop. Dang it.

    is there an easier way to do this for when I need to have accessors and mutators for currentName and currentPhone?
    Last edited by LaughingSeraph; 03-13-2013 at 09:21 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Gremlins in my code, I can't seem to trace

    Just store the index?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Gremlins in my code, I can't seem to trace

    Quote Originally Posted by LaughingSeraph View Post
    nope...'i' is local to the loop. Dang it.
    Give the field a more appropiate name. Usually the name 'i' is used for integers in a for-loop (as you already have).

  10. #10
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Gremlins in my code, I can't seem to trace

    So just storing i (aware of what i is for) and use that to fetch the current phone and name? I think you might be right, I will update the code for that, save a few lines. Definitely seems logical to just store the current record number instead of all 3 things.
    I appreciate the help gentlemen.

Similar Threads

  1. Replay Java Trace Execution
    By gonny in forum New To Java
    Replies: 1
    Last Post: 06-24-2012, 03:48 AM
  2. Error stack trace
    By Arnold in forum Android
    Replies: 1
    Last Post: 05-13-2010, 05:54 PM
  3. Can't trace where the error, need help
    By Hnin Hnin in forum Java Servlet
    Replies: 1
    Last Post: 03-16-2009, 03:24 PM
  4. Trace and track hp number on jsp
    By angelicsign in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-16-2008, 06:39 AM
  5. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:29 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
  •