DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004

    newbie java help

    couple of questions, any help would be appreciated

    for(int y = 0; y < branchAccount.length; y++)
    System.out.println("Details of customer: " + (y + 1) + ": " + branchAccount[y].printDetails());


    How do I exit the loop, or skip trying to print the object when there is a null in the loop to prevent null pointer exception?

    class a
    public static void main(String[]args)
    double total = 0, average;
    double [] array1 = new double[5];
    for(int y = 0; y < array1.length; y++)
    array1[y] = Keyboard.readDouble("Enter double: ");

    for(int y = 0; y < array1.length; y++)
    total = total + array1[y];

    average = total / array1.length;

    System.out.println("Differences: ");
    for(int y = 0; y < array1.length; y++)
    if(array1[y] < average)
    System.out.println((y+1) + ". " + (average - array1[y]));
    else if(array1[y] > average)
    System.out.println((y+1) + ". " + (array1[y] - average));
    System.out.println((y+1) + ". " + " 0 ");
    >>>>for(int y = 0; y < array1.length; y++)

    I have to add code to print only the positive numbers(if there are no positive numbers an appropriate message should be printed).

  2. #2
    Join Date
    Feb 2004
    for the first question, put something like this.
    if(!branchAccount[y] == null)
        //do whatever
    Put that in the for loop, and it will check that the object at each index isn't null. If it is null nothing will happen and it will skip to the next loop iteration, if it is not null then the code at "//do whatever" will be run.

    As for the second part that sounds like a homework question. I'm not going to tell you how to do it, but i'll give you some hints. You'll need to loop through the array and test if the number at each index is greater than 0. If it is print it out. You'll also need a variable that keeps track of positive numbers. Each time you find a positive number you can increment it by one. At the end, if this variable is greater than 0 that means you've found some positive numbers. If it isn't then you haven't found any, so you can print out your appropriate message.

  3. #3
    Join Date
    May 2004
    thx mike, this java stuff is all coming together now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.