Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Enforcing an If/Else if statement

1. ## Enforcing an If/Else if statement

Hi everyone, I'm pretty new to programming and I am currently taking a beginner's programming class. I had an assignment to calculate the the number of broadsheets needed for a certain amount of exam pages as well as finding the average of 5 inputted test scores. So I finally ended up using an array to find my average. The program is suppose to enforce the rule that

if (i < 0) ---> i = 0
if (i > 100) ---> i = 100

The assignment isn't for any points but rather for practice. It's been bothering me since it won't enforce the rule no matter what I change. Well here's my original code for the whole program (the problem is in the bottom half, although it doesn't have any if/else statements):

```import java.io.*;
public class HW4X
{
public static void main(String args[]) throws IOException
{

int e, b, s1, s2, s3, s4, s5, t;

System.out.print("Enter the amount of exam pages: ");

if (e == 0)
{
}

else if ((e % 4) == 0)
{
b = e / 4;
System.out.println("You will need " + b + " broadsheet(s).");
}

else if ((e % 4) >= 1)
{
b = (e / 4) + 1;
System.out.println("You will need " + b + " broadsheet(s).");
}

/* This is the part that is suppose to calculate the average as well as the part I will be substituting */

System.out.println("Enter test scores: ");

t = (s1 + s2 + s3 + s4 + s5) / 5;
System.out.println("The average is: " + t);

}

}```

And here's the part with the array average (I will be substituting the array into the previous program) with the if/else if statements that aren't working:

```import java.io.*;
public class x
{
public static void main(String args[]) throws IOException
{

System.out.println("Enter a number: ");

int nums[] = new int[5];
int result=0;
int i=0;

for(i=0; i < nums.length; i++)
{
result = result + nums[i];

if (nums[i] < 0)
{
nums[i] = 0;
}

else if (nums[i] > 100)
{
nums[i] = 100;
}

}

System.out.println("Average is = " + result/nums.length);

}

}```

Any help is appreciated!

2. ## Re: Enforcing an If/Else if statement

Ok, well the first thing that needs to be done is I need to confirm that you understand that the code:
`if (nums[i] < 0)`

Is checking the int inside of the i'th index of the nums array, not checking i itself.

I have the suspicion that you understand that, but I just want to clarify.

3. ## Re: Enforcing an If/Else if statement

Actually I didn't know that. Before I changed the

`if (nums[i] < 0)`

I had it as just i but it still had the same problems. I apologize for my negligence about this part. To be honest, we haven't even learned about arrays yet but I looked around and felt that it was easier to use in this situation.

4. ## Re: Enforcing an If/Else if statement

Well, since you are looping the length of nums, and nums's length is 5, and 0 is the lowest number you can have for i, those if statements will never be true if you said:
```if(i < 0)
...
if(i > 100)```

So the question I have is: is your intent to set the value of the i'th index of the nums array to either 0 or 100 if their value is either below 0 or higher than 100 (respectfully)? Because that is what your current code will do. The problem is that I would assume that a test grade will never be lower than 0 and never higher than 100, which renders those statements useless.

Another thing you have to consider is that you are adding the int in the i'th index to result before you check and change their values. This is significant because even though you change their values, their new values are not added to result so when you divide by the size of nums, you are dividing by the values of each index before you made the changes.

5. ## The Following User Says Thank You to aussiemcgr For This Useful Post:

Actinistia (March 3rd, 2011)

6. ## Re: Enforcing an If/Else if statement

Yes, that is my intent. I know that they're pretty useless but the professor wanted us to add them anyways. Thanks for explaining that the statements aren't even being used during the calculations. Like I said, I haven't even learned about arrays so I never would of guessed that. I'll try to fix it later tonight with what you said in mind.

7. ## Re: Enforcing an If/Else if statement

Is just the average suppose to be capped at 0 or 100, or is each individual test score suppose to be capped at 0 or 100?

If it's the first case, you need to place the code checking for 0 or 100 outside the for loop (after it), and check the bounds of result/nums.length.

If it's the second, your original code of checking nums[i] is almost correct, except you're checking if the score is out of range after you've already added that score to the running sum, which is obviously the wrong order. Simply move the checks before you add the score to results and it should work.

As a side note, there's no need for an array here unless you want to be able to keep track of each individual test score rather than just the average. You only need a single variable to hold the score of the last test, and then another variable to hold how many tests there are.

8. ## The Following User Says Thank You to helloworld922 For This Useful Post:

Actinistia (March 4th, 2011)

9. ## Re: Enforcing an If/Else if statement

Thanks again everyone for the help, your explanations made it easier for me to solve my problem. I hope someday I'll be able to help you guys too!