Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Junky

Thread: Review my code please - simple grade calculator program, need it perfect

  1. #1
    ronanbrowne88 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Post Review my code please - simple grade calculator program, need it perfect

    Ok so ive being doing java in college for about two months and i have been given an assignment ! Don't worry im not asking any one to do my home work for me :P i have the task completed and running, but i want some one more more knowledge than me to look at my code and be sure its ok, Its very important that i get 100% in this assignment so i don't want to submit it if the code isnt up to scratch.

    My task is "Write a programme to read in 10 students marks from the keyboard. as each mark is input it must be checked to see wheather it is a pass, fail or merit." each mark is out of a possible 100 (0-40 fail,41-59 pass,60-100merit)
    add extra checks for marks less than 1 or greater than 100 and output an invalid data message. program should continue until 10 valid marks have been entered and graded, check thoroughly so that it works under all conditions"


    "you are required to use some or all of the following
    conditional statements
    loop constructs
    array constructs
    string methods
    case statements"

    and my code is

    Java Code:
    package assignment1;
    
      import java.util.Scanner; 
    
    
    public class Assignment1 {
    
       
     
        public static void main(String[] args) {
         
            
            
         Scanner  imput = new Scanner(System.in);
         //Creating new Scannr obj
         
          
        
         System.out.print("****Student Grade Calculator*****\n");
         System.out.println("*********************************\n");
         //layout
       
              
         int [] grades = new int [arySize];  
         //declaring & initializing array of type int
         //this will hold the grades
         
              
         for(int i =0;i<grades.length;i++){ //loop to cycle through array
               
             System.out.print("Enter Grade of student number "+(i+1)+" :");
             grades[i]=imput.nextInt();
             //Getting user imput of grade
            
             if (grades[i]>=1 && grades[i]<=40){
                 System.out.println("Student mark is " + grades[i]+", Grade is Fail\n");
             }
             //if Statement when user enters mark greater than or less than one 
             // and less than or equal to 40 print to screen
             
             else if (grades[i]>=41 && grades[i]<=59){
                 System.out.println("Student mark is " + grades[i]+", Grade is Pass\n");
             }
             
             else if (grades[i]>=60 && grades[i]<=100){
                 System.out.println("Student mark is " + grades[i]+", Grade is Merit\n");
             }
             else if (grades[i]<0 || grades[i]>100){
                 System.out.println("INVALAD MARK - PLEASE RE-INPUT DATA\n");
                 i--;    
             }
            
                      }//end for loop
         
              
           
            
       
                 }
            
         }
    One thing im not sure how to do is check to see if the user entered a character instead of a number and output that same invalid data message without incrementing loop

    thanks in advance for any input and sorry if i posted the code in wrong format not sure how to get it on page looking like everyone else's i use netbeans!
    Last edited by ronanbrowne88; 11-21-2012 at 11:13 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Review my code please - simple grade calculator program, need it perfect

    The Scanner class has a hasNextInt method.

  3. #3
    ronanbrowne88 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Review my code please - simple grade calculator program, need it perfect

    Quote Originally Posted by Junky View Post
    The Scanner class has a hasNextInt method.
    could you explain further,is that different from the .nextInt() that im allready using.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Review my code please - simple grade calculator program, need it perfect

    So I mention a method you do not know about. What should be the first thing you do? Go read about it in the Java API. Write some test code to see how it works. When you are confident, add it to your code. Then if you are still stuck come back here, post your code attempt, post full error messages and ask a question.
    DarrylBurke likes this.

  5. #5
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Review my code please - simple grade calculator program, need it perfect

    You may have solved this already and not posted, but nextInt() returns the next integer that it receives from System.in, or User input in laymen terms. Whereas hasNextInt() returns whether or not(Boolean, which is a variable that is either true, or false.) there is an integer inputted. So you should safely be able to use it like this:
    Java Code:
    if(SystemInput.hasNextInt()) {
        //Put all of the stuff you want to do If the
        //Input is an integer
    }else if(SystemInput.hasNextChar()) {
        //Put all of what you want to do if the input
        //is a character.
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Review my code please - simple grade calculator program, need it perfect

    Quote Originally Posted by Wesley.laferriere View Post
    You may have solved this already and not posted, but nextInt() returns the next integer that it receives from System.in, or User input in laymen terms. Whereas hasNextInt() returns whether or not(Boolean, which is a variable that is either true, or false.) there is an integer inputted. So you should safely be able to use it like this:
    Java Code:
    if(SystemInput.hasNextInt()) {
        //Put all of the stuff you want to do If the
        //Input is an integer
    }else if(SystemInput.hasNextChar()) {
        //Put all of what you want to do if the input
        //is a character.
    }
    1. boolean, not Boolean.
    2. Variable names should start with a lowercase letter.
    3. Scanner doesn't have a method hasNextChar().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem with grade calculator?
    By MysteryForumGuy in forum New To Java
    Replies: 3
    Last Post: 02-15-2012, 02:14 AM
  2. Replies: 7
    Last Post: 01-20-2012, 10:44 PM
  3. Perfect number 1-1000 program help
    By ImWithStupid in forum New To Java
    Replies: 9
    Last Post: 04-28-2011, 09:22 AM
  4. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 04:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •