Results 1 to 15 of 15
  1. #1
    gerarda is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    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!

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    you have mistake in name variable in for-each loop
    Java Code:
    for (double value : fishLengthList) {
        ...
    }
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gerarda View Post
    Probably easy enough for a well seasoned developer but Im a noob!
    Thanks for the help!
    Since you are a newbie to Java, next time try to understand the error message you comes with. It'll provide lots of useful information.

    And also if you've posted the error message, we can help a lot than this. :)

  5. #5
    gerarda is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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!

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    for a quick look at the elements in an array you can use

    System.out.println(Arrays.toString(fishLengthList) );

  7. #7
    gerarda is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    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)
    Hi,
    That's what Im trying to do? Just print out the values in the array that are higher than 28.

    Thanks

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    gerarda is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default .

    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

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  11. #11
    king2be98 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default 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.

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    So, start at index 1 if it exists, and loop to the end of the array - instead of starting with 0

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    So, start at index 1 if it exists, and loop to the end of the array - instead of starting with 0
    Did you try this?

  14. #14
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You asking me or OP?

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 03:55 PM
  2. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 01:41 AM
  3. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 04:57 AM
  4. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •