Results 1 to 15 of 15
Thread: Array question..
- 02-07-2011, 02:05 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Array question..
Hi all,
Im trying to print off values in this array higher than 28.
double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5 };
But cant quite seem to get it right, Im using the 'for' loop as shown below:
public static void main(String args[])
{
double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5 };
for (double fishLengthList: fishLengthList) System.out.print(fishLengthList + " ");
Probably easy enough for a well seasoned developer but Im a noob!
Thanks for the help!
- 02-07-2011, 02:14 PM #2
And what does your code print out? Does it work?
You want to print off values that are higher than 28- so where is the check for this?
Recommended reading: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-07-2011, 02:15 PM #3
you have mistake in name variable in for-each loop
Java Code:for (double value : fishLengthList) { ... }Skype: petrarsentev
http://TrackStudio.com
- 02-07-2011, 02:59 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-07-2011, 03:57 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Hi guys, thanks for the replies! Sorry I should have included that when I changed a few things around all the program did was repeatedly print out "28".
Thanks again!
- 02-07-2011, 04:13 PM #6
for a quick look at the elements in an array you can use
System.out.println(Arrays.toString(fishLengthList) );
- 02-07-2011, 07:48 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
- 02-08-2011, 02:23 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is it working as you expected?
- 02-08-2011, 10:29 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
.
Here is the code:
----------------------------------------------
Java Code:class fishLengthList { public static void main(String args[]) { double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5}; for(int i=0; i < fishLengthList.length; i++) { if(fishLengthList[i] > 28) { System.out.println(fishLengthList[i]); } } } }Last edited by Eranga; 02-09-2011 at 12:17 PM. Reason: code tags added
- 02-09-2011, 12:22 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's fine.
However I've found something really bad in your code. Don't use the same name as a class name and variables. It's not at issue at all, but hen you read the code again, it's unclear. User different meaningful names.
And also, when you are posting code segments next time please use code tags. Unformated codes are hard to read.
- 02-28-2011, 08:13 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Help in printing values of an array.
I know how to print one specific value of an array.
Say the input string is text.
String[] textArray = text.split(" ");
to print first value, I use textArray[0] and textArray[1] for the 2nd etc etc.
How would I go about doing it for all except the first. ie. 1 to the length of the array which has to be determined.
Would appreciate the help.
Thanks.
- 03-01-2011, 01:32 AM #12
So, start at index 1 if it exists, and loop to the end of the array - instead of starting with 0
- 03-01-2011, 03:58 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-01-2011, 04:36 PM #14
You asking me or OP?
- 03-02-2011, 01:42 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Question about array
By hei1233212000 in forum New To JavaReplies: 2Last Post: 09-18-2010, 03:55 PM -
Array question
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 06-11-2010, 01:41 AM -
Array Question
By sc001 in forum New To JavaReplies: 1Last Post: 02-14-2010, 04:57 AM -
array question
By dazednconfused in forum New To JavaReplies: 4Last Post: 09-15-2009, 05:44 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks