-
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).
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks