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

Thread: Trimming, parsing, verifying grades entered are valid

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trimming, parsing, verifying grades entered are valid

    I have been trying to trim the and get the length and parse the string into a double from what the user enters. And I think I may have gotten that? Can you tell me if I did this correctly??

    The one that I am most concerned with is how do I validate correct grades to be entered? Any valid grades should be anything greater than 0 and grades less than 100.

    So far what Ive done is asked the user to enter up to ten grades, I then added in there, if the user presses enter, then they are done entering. Then I would add up the grades and average them up and print it. I haven't added that coding in there yet, as I am still trying to trim, get length and parse the string into a double.

    I know, It would make sense to read doubles from the user, but my instructor specifically asked that we read strings then convert to doubles if and only if the user entered a length > 0 of which I already established into my code.




    import java.util.*;
    import java.io.*;
    public class Grades {
     
    	public static void main(String[] args)throws IOException {
     
       	   double validGrades = 0;
    	   double d = 0;
    	   double total = 0;
    	   final int SIZE = 10;
    	   boolean exit = false;
    	   int count = 0;
    	   String gradesEntered;
     
    	   BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    	   String[ ] grades = new String[SIZE];
    	   String[ ] trimmedGradesEntered = new String[grades.length];
     
    	   System.out.println("Please enter up to 10 grades and press enter to finish.");
     
     
    		while((count <= SIZE-1) && (exit == false)){
    			System.out.print("Grade " + (++count) + ": ");
    			gradesEntered = bf.readLine( ).trim();
     
    			if(gradesEntered.length() < 1){
    				exit = true;
    			}
    			else{
    				total = total + Double.parseDouble(gradesEntered);
    			}
    		}
     
     
     
    	}
    }


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    Try adding some print statements to figure out where the program is going.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    Start with writing down the logic/algorithm for the validation.
    For example, you want the user to repeatedly enter a value until it is valid (between 0 and 100), so you would use a loop for this.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    For verifying the grades are valid, you have a few options. The grades are entered as strings which poses the possibility for the user to enter invalid data. This is a common problem that sadly many dont tackle and get burned later in production. Anyways, how do we fix this? There are a few options ranging form trivial or complex or "bad practice but it works".

    1. (Trivial) You can have a for loop that goes through the string and checks each character to make sure it is either a digit or a dicimal point. But then you have to make sure there is only one decimal point. This can get tricky.
    2. (Complex) Use a regular expression. These are very powerful and will tell you exactly what you want to know very quickly. If you can learn these early in your career of programming it will be a great advantage.
    3. (Bad Practice) Put a try/catch block around your Double.parseDouble to catch the NumberFormatException and instead of breaking, display a message that it is not valid, subtract the one from your count, and send it back through the loop.
    --This could be the most simple, but it is considered bad practice to use a try/catch block in situations like this. Typically you should only use them for cases where your code goes wrong. There are countless debates about this. In my opinion, it is supposed to be used for exceptions...well people are supposed to give you doubles and in the exceptional case that they dont, this can handle it. Take it as you will, maybe ask your teacher about it. Some teachers take away points for "bad practice" as they see it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    Surely you would use an integer to read in marks?

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    Not if the grade can be 50.5
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    Quote Originally Posted by Chris.Brown.SPE View Post
    Not if the grade can be 50.5
    I understand that, you mentioned above about the grades being entered as Strings.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Trimming, parsing, verifying grades entered are valid

    They are being read in from the screen as strings. Perhaps changing to a different reader would correct that.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. [SOLVED] Get percent of failing grades from an array
    By thom4065 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 03:54 PM
  2. Students/Grades
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 12th, 2011, 10:42 AM
  3. Replies: 1
    Last Post: December 4th, 2010, 04:26 PM
  4. Downloading files and verifying MD5 checksums
    By Kryptix in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2010, 11:04 PM
  5. Need help with String Trimming.. and displaying Keys on maps.
    By bh-chobo in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 9th, 2009, 12:15 PM