Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

How to print from a java array?

  • 07-02-2011 11:51pm
    #1
    Registered Users, Registered Users 2 Posts: 448 ✭✭


    Hi all,
    Given an array: {10.0,15.5,18.0,29.5,45.5 } How would I go about printing values from the array that are greater than 28? I'm using eclipse and have tried variations based on this:

    class fishLengthList {



    public static void main(String args[]){
    {

    int i = 0;
    int length;


    double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5 };
    while (fishLengthList.length > 28);{
    System.out.print(i + " "); //$NON-NLS-1$
    }
    }

    }
    }


Comments

  • Moderators, Entertainment Moderators Posts: 17,994 Mod ✭✭✭✭ixoy


    Is that the code you actually tried to use? It can't work in its present state (not to mention it's declaring unused variables). For starters:
    int length;
    
    - You declare this variable, but never use it. Is this intended for the length of the array?
    while (fishLengthList.length > 28)
    
    - This loop will never execute. fishLengthList.length returns the size of the array (5 in this case), so will always be lower than 28.
    System.out.print(i + " ");
    
    - Why are you printing i out here? i was declared as 0 and has never been incremented. So it'll always print out 0. That's if it got into the loop!

    You need to start from scratch here really. Think about what you're trying to do:
    • How do I move through the array?
    • How do I find the size of the array
    • What can I do to determine the value of a particular element in the array
    • What should I compare that value against
    • How do I move to the next element of the array?

    There's a variety of approaches here and the while loop is a start, but you should try and take a step back and plan it more clearly.


  • Registered Users, Registered Users 2 Posts: 448 ✭✭gerarda


    Thanks for the reply, Ive been reading the SCJA study guide and am finding parts of it hard to grasp. I finished the "For Dummies" book and wanted to push for some kind of certification.


  • Registered Users Posts: 108 ✭✭Jaych1000


    You need to know how to extract values from an array.
    You use square brackets and a numeric value as an index.
    An example is if I create an array:
    double[] myArray = {11.0, 22.0, 33.0 ,44.0};
    To get the first value from the array, you use:
    myArray[0] //Since the first indx is 0.
    So: double myValue = myArray[0]...
    myValue would be 11.0.
    You print the values from the array in the same manner; by outputting the value at the index.

    I think you're getting mixed up with some things.
    To get the values that are greater than 28, you don't use a 'while' loop. Think about it.

    Best way to get good at coding is to get into it hands-on. Even if you read and understand it, doing practical coding is a lot better. You learn a lot that way.

    EDIT: Refer back to this page:
    http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html


  • Registered Users, Registered Users 2 Posts: 375 ✭✭unknownlegend


    gerarda wrote: »
    Hi all,
    Given an array: {10.0,15.5,18.0,29.5,45.5 } How would I go about printing values from the array that are greater than 28? I'm using eclipse and have tried variations based on this:

    class fishLengthList {



    public static void main(String args[]){
    {

    int i = 0;
    int length;


    double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5 };
    while (fishLengthList.length > 28);{
    System.out.print(i + " "); //$NON-NLS-1$
    }
    }

    }
    }


    foreach(double d in fishLengthList.length)
    {
    if(d > 28) { System.print "D"; }
    }

    sorry, havent written Java in ~5 years so don't know syntax, but that's the logic.


  • Registered Users Posts: 240 ✭✭Axe Rake


    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]);
                }
            }
        }
    }
    


  • Advertisement
  • Registered Users Posts: 108 ✭✭Jaych1000


    foreach(double d in fishLengthList.length)
    {
    if(d > 28) { System.print "D"; }
    }

    sorry, havent written Java in ~5 years so don't know syntax, but that's the logic.

    I think that's a bit advanced but that IS the basic logic to what you want.


Advertisement