#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Displaying objects in a for loop from an array?


    Hi guys,

    Basically I'm trying to make an array called 'dates' that contains 3 'Date' objects and uses a for loop to display the 3 objects along with the toString() method I have made.

    I'm struggling a little with this and I wondered if you guys could guide me in the right path I'll post code of my classes and code thus far:

    UsingArrays class:

    Code:
    public class UsingArrays {
    	public static void main(String[] args) {
    
    		for (String s : args) {
    			System.out.println(s);
    		}
    			int[] squares = new int[10];
    
    			for (int i = 1; i < squares.length + 1; i++) {
    				squares[i - 1] = i * i;
    
    			}
    			
    			for(int i : squares){
    				System.out.print("["+i+"]");
    			}
    			
    		
                             Date [] dates = new Date[3];
    			
    			dates[15] = new Date();
    			dates[12] = new Date();
    			dates[1992] = new Date(); 
                            //Incomplete code, this is what I'm confused on how to accomplish.
    
    				
    			}
    		}
    	}
    Date class:

    Code:
    public class Date {
    
    	private int day;
    	private int month;
    	private int year;
    
    	public String toString() {
    		String dateString;
    		dateString = month + "/" + day + "/" + year;
    		return dateString;
    	}
    
    	public void setDay(int i) {
    		if (i >= 1 && i <= 31) {
    			day = i;
    		} else {
    			System.out.println("The given day is not valid.");
    		}
    	}
    
    	public void setMonth(int i) {
    		if (i >= 1 && i <= 12) {
    			month = i;
    		} else {
    			System.out.println("The given month is not valid");
    		}
    
    	}
    
    	public void setYear(int i) {
    		if (i > 0) {
    			year = i;
    		}
    
    	}
    
    	public int getDay(int i) {
    		return day;
    	}
    
    	public int getMonth(int i) {
    		return month;
    	}
    
    	public int getYear(int i) {
    		return year;
    	}
    
    }

    Much appreciated!

    Thanks.

    EDIT: The part I have wrapped in code tags is incomplete code, this is the part I'm confused about. I'm not too sure how to approach the matter of displaying 3 'Date' objects (from my class Date) in my UsingArrays class using a for loop and the toString() method I have. The output should be 3 Dates objects.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please edit your post and wrap the code in code tags.

    I'm struggling a little with this
    Could you explain what the problems are?
    Post the program's output, add some comments saying what is wrong with it
    and show what it should be.

    If you get errors, copy the full text and post it here.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and wrap the code in code tags.


    Could you explain what the problems are?
    Post the program's output, add some comments saying what is wrong with it
    and show what it should be.

    If you get errors, copy the full text and post it here.
    Thanks for the reply, I can't actually get it to compile. I'd like to know how I would be able to print out 3 Date objects using a for loop and the toString() method that I have in the Date class? From the UsingArrays class that I have.

    Thanks for your help.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    I can't actually get it to compile
    Please copy the full text of the error messages and post them.
    Solve the compiler errors before trying to add more to the code.

    The Java SE has a class named Date. You should use another name to prevent confusion.

    Please edit your post and wrap the code in code tags.
    When the code is properly formatted, I'll be able to load it for testing.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Please copy the full text of the error messages and post them.
    Solve the compiler errors before trying to add more to the code.

    The Java SE has a class named Date. You should use another name to prevent confusion.

    Please edit your post and wrap the code in code tags.
    When the code is properly formatted, I'll be able to load it for testing.
    I've wrapped the code I'm having issues with in my first post.

    Thanks.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please explain what the issues are.

    The code you wrapped can't be compiled. You need to wrap all the code.

    The Java SE has a class named Date. You should use another name to prevent confusion.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Please explain what the issues are.

    The code you wrapped can't be compiled. You need to wrap all the code.

    The Java SE has a class named Date. You should use another name to prevent confusion.
    Sorry about that, I've tagged to two classes and commented out the incomplete section that I'm having issues solving.

    Thanks for your help.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What are the issues?


    The Java SE has a class named Date. You should use another name to prevent confusion.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Your code does not compile yet because there is a '}' too many in your UsingArrays.java

    After resolving this, your code compiles and runs, yet it encounters an "IndexOutOfBoundsException: 15.
    I will leave it as an exercise to you as what the origin of this number 15 is. Look up how to add values to an array in java.

    Also, as NormR advised: Date is an existing java class. Change the name of your class to something like MyData.

IMN logo majestic logo threadwatch logo seochat tools logo