# Enforcing an If/Else if statement

• March 3rd, 2011, 03:10 PM
Actinistia
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):

Code Java:

```import java.io.*; public class HW4X { public static void main(String args[]) throws IOException { BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));   int e, b, s1, s2, s3, s4, s5, t;   System.out.print("Enter the amount of exam pages: "); e = Integer.parseInt(keybd.readLine());   if (e == 0) { System.out.println("Please enter a valid amount!"); }   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: "); s1 = Integer.parseInt(keybd.readLine()); s2 = Integer.parseInt(keybd.readLine()); s3 = Integer.parseInt(keybd.readLine()); s4 = Integer.parseInt(keybd.readLine()); s5 = Integer.parseInt(keybd.readLine());   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:

Code Java:

```import java.io.*; public class x { public static void main(String args[]) throws IOException {   BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));   System.out.println("Enter a number: ");   int nums[] = new int[5]; int result=0; int i=0;   for(i=0; i < nums.length; i++) { nums[i] = Integer.parseInt(keybd.readLine()); 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!
• March 3rd, 2011, 03:18 PM
aussiemcgr
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:
Code java:

`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.
• March 3rd, 2011, 03:27 PM
Actinistia
Re: Enforcing an If/Else if statement
Actually I didn't know that. Before I changed the

Code java:

`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.
• March 3rd, 2011, 03:54 PM
aussiemcgr
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:
Code java:

```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.
• March 3rd, 2011, 06:23 PM
Actinistia
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.
• March 3rd, 2011, 07:37 PM
helloworld922
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.
• March 4th, 2011, 11:24 AM
Actinistia
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!