The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Displaying objects in a for loop from an array?
Discuss Displaying objects in a for loop from an array? in the Java Help forum on Dev Shed. Displaying objects in a for loop from an array? Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 1st, 2012, 06:43 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
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.
|

December 1st, 2012, 06:46 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

December 1st, 2012, 06:51 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
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.
|

December 1st, 2012, 06:56 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

December 1st, 2012, 07:13 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
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.
|

December 1st, 2012, 07:20 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

December 1st, 2012, 07:36 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 4
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.
|

December 1st, 2012, 07:44 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What are the issues?
The Java SE has a class named Date. You should use another name to prevent confusion.
|

December 2nd, 2012, 10:01 AM
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|