Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by moriarty View Post
    Hmm, it seems it won't take .getIterator, but I've tried .iterator and .listIterator and it looked like .listIterator worked, but only in the case of 2 objects with identical names.

    I'll go over the javadoc
    That's what I get for posting without testing first. .iterator() should work (although I still haven't tested it) and I have edited my post above accordingly.

    -Gary-

  2. #22
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Nvm it seems the problem is elsewhere, I added a method to check what was being stored and it's not storing anything besides the last variable. Your code worked great.

    Now onto that problem. ><

    Thanks so much for that though, I'm closely getting there. Then I gotta deal with a bunch of pain in my rear search functions.

    edit: Yeah, it appears I screwed up my constructors somehow. Hey...this is the beginners forum. :P
    Last edited by moriarty; 03-28-2010 at 12:44 AM.

  3. #23
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    So here's just the beginning bit of code for the Assignment class, can you guys tell me if I screwed up on the constructor for why it'd be throwing the same thing over and over?

    Java Code:
    import java.util.*;
    
    public class Assignment{
    	
    	private static int intDueDate;
    	private static String strAssnName;
    
    
    	public Assignment(int due, String name) {
    		Assignment.setAssnName(name);
    		Assignment.setIntDueDate(due);
    	}
    
    
    	private static void setIntDueDate(int due) {
    		Assignment.intDueDate = due;
    	}
    ...
    Last edited by moriarty; 05-02-2010 at 05:10 AM. Reason: Ease of reading

  4. #24
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    frustrating

    Java Code:
    package assg03;
    
    import java.util.*;
    import java.io.*;
    
    public class AssignmentList {
    
    	public static void main(String[] args) {
    		ArrayList<Assignment> aryAssignments = new ArrayList<Assignment>(); 
    		
    		//initialize variable for the menu choice, set to anything but 5 for loop to work.
    		int intChoice = -1;
    		
    		//New scanner object to read input from the user.
    		Scanner keyboard = new Scanner(System.in);
    		
    		//While loop to always cut back to the menu unless the user chooses 5. This breaks the loop and terminates the main program.
    		while (intChoice != 5){
    			System.out.println(aryAssignments.size());
    
    			System.out.println("\n\n\nWelcome to the Assignment List Program");
    			System.out.print(" 1. Add a new assignment. \n 2. Remove an assignment. \n 3. Provide a list of assignments in the order they were assigned. \n 4. Find the assignment(s) with the earliest due date. \n 5. Quit");
    			System.out.print("\n Please select an option:");
    		
    ...
    Last edited by moriarty; 05-02-2010 at 05:09 AM.

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by moriarty View Post
    So here's my code for the Assignment class, can you guys tell me if I screwed up on the constructor for why it'd be throwing the same thing over and over?

    Java Code:
    public class Assignment{
    	
    	private static int intDueDate;  // ??? static ???
    	private static String strAssnName;  // ??? static ???
    
    
    	public Assignment(int due, String name) {
    		Assignment.setAssnName(name);
    		Assignment.setIntDueDate(due);
    	}
    
    
    	private static void setIntDueDate(int due) {   // ??? static ???
    		Assignment.intDueDate = due;
    	}
    
    	//...
    
    	private static void setAssnName(String name) {  // ??? static ???
    		Assignment.strAssnName = name;
    	}
    
    	//...
    }
    You appear to be using the static modifier where it shouldn't be used. Why are you using it here, and are you sure that it should be used?

  6. #26
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You appear to be using the static modifier where it shouldn't be used. Why are you using it here, and are you sure that it should be used?
    So yeah, I've got no idea when it should or shouldn't be used, but eclipse was forcing me to use static on my calls, it wouldn't compile if I didn't change. But I've changed some of the code around since then so I'm not sure where it will let me or won't.

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by moriarty View Post
    So yeah, I've got no idea when it should or shouldn't be used, but eclipse was forcing me to use static on my calls, it wouldn't compile if I didn't change. But I've changed some of the code around since then so I'm not sure where it will let me or won't.
    No, Eclipse isn't forcing you to do this. You must be calling something in the class in a static way, and the correct fix is to fix this error, not to make random things static. So in some, get all statics out of the class that I've indicated and fix the errors in the code that uses this class.

  8. #28
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    I think that might have done it. I rewrote the class. Here's the code.

    Java Code:
    package assg03;
    
    import java.util.*;
    
    public class Assignment{
    	
    	private int intDueDate;
    	private String strAssnName;
    
    
    	public Assignment(int due, String name) {
    		setAssnName(name);
    		setIntDueDate(due);
    	}
    
    
    	private  void setIntDueDate(int due) {
    		intDueDate = due;
    	}
    
    	public int getIntDueDate() {
    		return intDueDate;
    	}
    
    	private  void setAssnName(String name) {
    		strAssnName = name;
    	}
    
    	public String getAssnName() {
    		return strAssnName;
    	}
    	public String toString() {
    		return strAssnName + ": " + intDueDate;
    	}
    	public boolean equals(String strCompare) {
    		if (strCompare == strAssnName)
    			return true;
    		else
    			return false;
    	}
    }
    Last edited by moriarty; 03-28-2010 at 01:33 AM.

  9. #29
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, I'll give you the full treatment, since I'm in a mood to rant:
    Java Code:
    package assg03;
    If you have a domain name registered, it's a good idea to start using it for your package names. If not, you can probably use your company or school email address (but don't use your ISP email) something like this:
    Java Code:
    [COLOR="Blue"]package edu.excelsior.gcalvin.assignment03;
    [/COLOR]
    It's a quibble, but good habits are good habits.
    Java Code:
    import java.util.*;
    
    public class Assignment {
    	
    	private static int intDueDate;
    	private static String strAssnName;
    These aren't static unless every assignment ever created will share the same name and the same due date. It's good to make them private. I still hate your names, and I would simply call them dueDate and name.
    Java Code:
    [COLOR="Blue"]	private int dueDate;
    	private String name;
    [/COLOR]
    I assume dueDate is an int because you haven't gotten into Java Date yet. That's fine for now.
    Java Code:
    	public Assignment(int intDue, String strName) {
    		Assignment.setAssnName(strName);
    		Assignment.setIntDueDate(intDue);
    	}
    I would just do this:
    Java Code:
    [COLOR="Blue"]	public Assignment(int dueDate, String name) {
                    this.dueDate = dueDate;
                    this.name = name;
    	}
    [/COLOR]
    A lot of people would disagree with me on that, and believe that it is confusing to use the same name for the passed parameter and the instance variable. I think that they are both referring to the same piece of information, and that it's more confusing if they don't have the same name.
    Java Code:
    	private static void setIntDueDate(int intDueDate) {
    		Assignment.intDueDate = intDueDate;
    	}
    Even if you insist on your little type tags for instance variable names, do you really have to impose them on users of your class in the method names too? Anyway, this method shouldn't be static. It's going to be called on instances, not on the class as a whole.
    Java Code:
    [COLOR="Blue"]	public void setDueDate(int dueDate) {
    		this.dueDate = dueDate;
    	}
    [/COLOR]
    Note that it needs to be public as well -- users of your class will need to call it.
    Java Code:
    	public int getIntDueDate() {
    		return intDueDate;
    	}
    Same here.
    Java Code:
    [COLOR="Blue"]	public int getDueDate() {
    		return dueDate;
    	}
    [/COLOR]
    Keep it simple.
    Java Code:
    	private static void setAssnName(String strAssnName) {
    		Assignment.strAssnName = strAssnName;
    	}
    
    	public String getAssnName() {
    		return strAssnName;
    	}
    And of course the same goes for these.
    Java Code:
    [COLOR="Blue"]	public void setName(String name) {
                    this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    [/COLOR]
    This isn't bad...
    Java Code:
    	public String toString() {
    		return strAssnName + ": " + intDueDate;
    	}
    ...but it could be a little more readable:
    Java Code:
    [COLOR="Blue"]        public String toString() {
                    return "Assignment: " + getName() + " is due on " + getDueDate();
            }
    [/COLOR]
    It's good practice to use your getters, even in your other class methods. You'll be happier one day if you develop the habit.
    Java Code:
    	public boolean equals(String strCompare) {
    		if (strCompare == strAssnName)
    			return true;
    		else
    			return false;
    	}
    }
    Oops... comparing Strings with ==? Anyway, equals needs to take an Object argument.
    Java Code:
    [COLOR="Blue"]	public boolean equals(Object compareTo) {
                    if (this == compareTo) return true; // they are the same object
                    if (! (compareTo instanceof Assignment)) return false; // they need to be from the same class
                    if (getName().equals((Assignment)compareTo.getName())) return true; // the names match
                    return false;
    	}
    [/COLOR]
    This, by the way, would show two Assignments as equal if they had the same name, but different dueDates -- is that what you want?

    Hope that helps!

    -Gary-
    Last edited by gcalvin; 03-28-2010 at 02:02 AM. Reason: added missing //

  10. #30
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Turns out it was from letting eclipse automatically create the get/set methods, it was calling them as Assignment.getAssnName.

    Learned my lesson, don't let eclipse do anything on it's own, lol.

    Edit;

    Gary.

    ...I love you.

    Kidding, but seriously...

    You guys have helped me beyond all recognition. I'll definitely be here more often, you've been extremely helpful. :D
    Last edited by moriarty; 03-28-2010 at 01:45 AM.

  11. #31
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Eclipse made your setters static because you made your instance variables static. And static methods would indeed need to be called with the class name, rather than with this. Sewage runs downhill. :p

    I hadn't seen your main() before, but break that up into methods. Again, it's about developing good habits. You write one thing at a time, and test one thing at a time, and then you put it away and don't mess with it. When you cram everything into main() you're going to make yourself crazy and break things that were working. Too many programmers get in the habit of thinking that methods are only about code re-use. They are about that, but they are also about encapsulation and decomposition. You break your code into methods that are small enough to understand, even if a method will only ever be called from one spot within another method.

    -Gary-

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Calling a method when using an arraylist?
    By Jamison5213 in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 08:47 PM
  2. ArrayList problems
    By komo225 in forum New To Java
    Replies: 4
    Last Post: 02-12-2009, 04:14 AM
  3. [Problems] ArrayList
    By Zuela in forum New To Java
    Replies: 1
    Last Post: 06-16-2008, 11:51 AM
  4. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM
  5. Replies: 0
    Last Post: 11-14-2007, 03:22 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
  •