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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Enforcing an If/Else if statement

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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
    {
    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:

    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!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Actinistia (March 3rd, 2011)

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #7
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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!

Similar Threads

  1. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  2. Loops only on the first statement
    By Liuric in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 2nd, 2010, 07:36 AM
  3. If Statement in SQL String
    By Steffi1013 in forum Loops & Control Statements
    Replies: 0
    Last Post: March 30th, 2010, 03:25 PM
  4. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM