Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 1st, 2012, 06:43 PM
Bosalino Bosalino is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 Bosalino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 14 sec
Reputation 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.

Reply With Quote
  #2  
Old December 1st, 2012, 06:46 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Please edit your post and wrap the code in code tags.

Quote:
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.

Reply With Quote
  #3  
Old December 1st, 2012, 06:51 PM
Bosalino Bosalino is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 Bosalino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 14 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #4  
Old December 1st, 2012, 06:56 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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.

Reply With Quote
  #5  
Old December 1st, 2012, 07:13 PM
Bosalino Bosalino is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 Bosalino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 14 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #6  
Old December 1st, 2012, 07:20 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #7  
Old December 1st, 2012, 07:36 PM
Bosalino Bosalino is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 Bosalino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 14 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #8  
Old December 1st, 2012, 07:44 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
What are the issues?


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

Reply With Quote
  #9  
Old December 2nd, 2012, 10:01 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Displaying objects in a for loop from an array?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap