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: ARRAYs - Display number of students who failed

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default ARRAYs - Display number of students who failed

    Here is a practise revision question that im stuck on:

    Q32) Write a program that reads in 10 student marks into an array called marks
    and computes the number of students who have failed (got a mark less than 40).


    Ok so here is what i have so far:

    public static void main(String[] args) {
    int[] marks = new int[10];

    String strMark = "";

    int Marksread = 0;

    for (Marksread = 0; Marksread < marks.length; Marksread++){
    strMark = JOptionPane.showInputDialog("Enter a Mark:");
    marks[Marksread] = Integer.parseInt(strMark);




    I have managed to read in the 10 marks but i cant figure out how to display the number of student that have failed. I know i have to use a loop to count it up but just cant seem to get my head round it and keep getting errors Need help please... :s


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ARRAYs - Display number of students who failed

    I know i have to use a loop
    Add code inside the loop to test if the grade is a fail and if it is, add one to a counter.
    keep getting errors
    Please post the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    roronoaz (July 1st, 2012)

  4. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: ARRAYs - Display number of students who failed

    Hello roronaz!
    Quote Originally Posted by roronoaz View Post
    I know i have to use a loop to count it up but just cant seem to get my head round it and keep getting errors Need help please... :s
    Please post the full text of the error messages.
    If you know how to loop through an array which part is giving you trouble? Keep a int variable which will hold the number of students who failed. Then you just have to loop through the array and check if each of the array's elements is less than 40: if it is, you increment the counter.
    Hint: harcode an array for testing and when you make it work, then use JOptionPane.
    And please use the code tags to get highlighting in your code.

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

    roronoaz (July 1st, 2012)

  6. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ARRAYs - Display number of students who failed

    Okay so i've tried using the loop but i keep getting '1' as a result everytime i run it

    public static void main(String[] args) {
    int[] marks = new int[10];

    String strMark = "";
    int count = 0;
    int Marksread = 0;


    for (Marksread = 0; Marksread < marks.length; Marksread++){
    strMark = JOptionPane.showInputDialog("Enter a Mark:");
    marks[Marksread] = Integer.parseInt(strMark);
    }
    if (Marksread<40){
    count++;
    }




    System.out.println(count + " Students have failed!");
    }
    }


    the result:

    run:
    1 Students have failed!
    BUILD SUCCESSFUL (total time: 16 seconds)

  7. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: ARRAYs - Display number of students who failed

    You need to check the value of the array in Marksread position (ie marks[Marksread]), not Marksread itself.

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

    roronoaz (July 2nd, 2012)

  9. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ARRAYs - Display number of students who failed

    Thank you Andreas! I've finally got it working now

    public static void main(String[] args) {
    int[] marks = new int[10];

    String strMark = "";
    int count = 0;
    int Marksread = 0;


    for (Marksread = 0; Marksread < marks.length; Marksread++){
    strMark = JOptionPane.showInputDialog("Enter a Mark:");
    marks[Marksread] = Integer.parseInt(strMark);
    if (marks[Marksread]<40){
    count++;
    }

    }

    System.out.println(count + " Students have failed!");
    }}

  10. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: ARRAYs - Display number of students who failed

    Glad you got it work. Make sure you mark the thread as solved (from the Thread Tools in the top of the thread).

  11. The Following User Says Thank You to andreas90 For This Useful Post:

    roronoaz (July 3rd, 2012)

Similar Threads

  1. display all prime factors of a number
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2012, 06:55 PM
  2. Display number of threads in each running process
    By Waseem Usman in forum Threads
    Replies: 1
    Last Post: March 23rd, 2012, 09:51 AM
  3. display the generated Math.random number
    By PsYNus in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 2nd, 2011, 10:25 AM
  4. Java Web Service Client Authentication (limiting the number of failed attempts)
    By pranay_reddy in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: September 19th, 2011, 04:30 PM
  5. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM