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 5 of 5

Thread: help me with my code

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default help me with my code

    Am trying to print the grade btw 40 and 90 be those that passed and less than 40 for those that failed the exams.

    the the else statement to meet the condition (No grade is entered) is giving me error as well saying else without if

    someone pls help
    thanks
    public class NewClassIfmethod {
        private String courseName;
            //starting a constructor 
     
            public NewClassIfmethod (String name){
            courseName = name;
            } 
            //endin the constructor
            //creating a new method setNewClassIfmethod 
            public void setNewClassIfmethod (String name){
                courseName = name;
            } 
            // creating a method to getNewClassIfmethod
            public String getNewClassIfmethod(){
                return courseName;
            }
            public void displayMessage(){
                System.out.printf("Welcome to the world of Java programing %s\n\n" , getNewClassIfmethod());
            }
     
                public void averageGradeTest(){
                    Scanner input = new Scanner (System.in);
                    int grade;
                    int total;
                    int gradecounter;
                    double average;
               total = 0;
               gradecounter = 0;
               //promt user
             System.out.println("Enter Grade Number or -1 exit");
             grade = input.nextInt();
             // inserting entinel
             while (grade != -1){
                    total = total + grade;
                    gradecounter = gradecounter + 1;
                    System.out.println("Enter Grade Number or -1 exit\n");
                    grade = input.nextInt();
             }
     
             if (gradecounter != 0){
                 average = (double) total/gradecounter;
                 {
                 System.out.printf("The Class Average %.2f\n", average);
                 System.out.printf("The grade total is %2d\n", total, gradecounter);
             }
                 else
                 System.out.println("No grade Entered");
                }
              if (grade >= 40 && <= 90){
                 System.out.println("The number of " + gradecounter + " passed the Exam" );
             }
                 else 
                 System.out.println("The number of " + gradecounter + "failed the exams");
     
                }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help me with my code

    Please use whole words, punctuation, capitals, etc. Those to whom English is not their native language find the abbreviations difficult to understand. Frankly, some of us to whom English is our native language find it difficult sometimes.

    The end (close brace) if this 'if' clause is missing:

    if ( gradecounter != 0 )

    Indenting your code correctly would have revealed this.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help me with my code

    Am still getting illegal start of expression

    if (grade >= 40 && <= 90){
     
                 System.out.println("The number of " + gradecounter + " passed the Exam" );
             }
                 else {
                 System.out.println("The number of " + gradecounter + "failed the exams");
              }
     
     
     
     
                }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help me with my code

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help me with my code

    this is the code
    the if (grade >= 40 && <= 90) is underlined saying... illegal start of expression

     */
    package testing;
    import java.util.Scanner;
     
    /**
     *
     * @author Dokun
     */
    public class NewClassIfmethod {
        private String courseName;
            //starting a constructor 
     
            public NewClassIfmethod (String name){
            courseName = name;
            } 
            //endin the constructor
            //creating a new method setNewClassIfmethod 
            public void setNewClassIfmethod (String name){
                courseName = name;
            } 
            // creating a method to getNewClassIfmethod
            public String getNewClassIfmethod(){
                return courseName;
            }
            public void displayMessage(){
                System.out.printf("Welcome to the world of Java programing %s\n\n" , getNewClassIfmethod());
            }
     
                public void averageGradeTest(){
                    Scanner input = new Scanner (System.in);
                    int grade;
                    int total;
                    int gradecounter;
                    double average;
               total = 0;
               gradecounter = 0;
               //promt user
             System.out.println("Enter Grade Number or -1 exit");
             grade = input.nextInt();
             // inserting entinel
             while (grade != -1){
                    total = total + grade;
                    gradecounter = gradecounter + 1;
                    System.out.println("Enter Grade Number or -1 exit\n");
                    grade = input.nextInt();
             }
     
             if (gradecounter != 0){
                 average = (double) total/gradecounter;
                 System.out.printf("The Class Average %.2f\n", average);
                 System.out.printf("The grade total is %2d\n", total, gradecounter);
             }
                 else
                {
                 System.out.println("No grade Entered");
                }
             if (grade >= 40 && <= 90){
     
                 System.out.println("The number of " + gradecounter + " passed the Exam" );
             }
                 else {
                 System.out.println("The number of " + gradecounter + "failed the exams");
              }
     
     
     
     
                }
    }


    --- Update ---

    Thanks guys
    I have seen it that grade is missing from if (grade >= 40 && <= 90){

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM