newbie java help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: newbie java help

  1. #1
    Join Date
    May 2004
    Posts
    3

    newbie java help

    couple of questions, any help would be appreciated

    for(int y = 0; y < branchAccount.length; y++)
    {
    >>>>>if(!branchAccount.equals(>???<))
    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));
    else
    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
    Posts
    541
    for the first question, put something like this.
    Code:
    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
    Posts
    3
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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