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

Thread: Validation based program that doesn't function properly

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Validation based program that doesn't function properly

    These are the instructions for the program I wrote:

    A student object should validate its own data. The client runs this method, called validateData(), with a student object, as follows:
    String result = student.validateData();
    if(result == null)
    <use the student>
    else
    System.out.println(result);
    If the student's data are valid, the method returns the value null; otherwise, the method returns a string representing an error message that describes the error in the data. The client can then examine this result and take the appropriate action.

    A student's name is invalid if it is an empty string. A student's test score is invalid if it lies outside the range from 0 to 100. Thus, sample error message might be

    "Sorry : name required"
    and
    "Sorry: must have 0 <= test score <=100"

    This is my Student class:
    public class Student {
    private static String name;
    private static int test1;
    private int test2;
    private int test3;
     
    public Student(){
    name = "";
    test1 = 0;
    test2 = 0;
    test3 = 0;
    }
    public void setName (String nm){
    name = nm;
    }
    public static String getName(){
    return name;
    }
    public void setScore (int i, int score){
    if(i==1)test1 = score;
    else if (i == 2)test2 = score;
    else test3 = score;
    }
    public int getScore (int i){
    if(i==1) return test1;
    else if(i==2)return test2;
    else return test3;
    }
    public int getAverage(){
    int average;
    average = (int)Math.round((test1 + test2 + test3)/3.0);
    return average;
    }
    public int getHighScore(){
    int highScore;
    highScore = test1;
    if (test2 > highScore)
    highScore = test2;
    if (test3 > highScore)
    highScore = test3;
    return highScore;
    }
    public String toString(){
    String str;
    str = "Name : " + name + "\n" +
    "Test1: " + test1 + "\n" +
    "Test2: " + test2 + "\n" +
    "Test3: " + test3 + "\n" +
    "Average: " + getAverage();
    return str;
    }
    public String validateData()
    {
    String message = "";
    if(name == "")
    message = "sorry please enter a name";
     
    if(test1<0 || test1>100)
    message += "Sorry - Test 1 is not in between 0 and 100 \n";
    if(test2<0 || test2>100)
    message += "Sorry - Test 2 not in between 0 and 100\n";
    if(test3<0 || test3>100)
    message += "Sorry - Test 3 not in between 0 and 100";
    return message;
    }
    }

    And this is my ValidateData class:
    import java.util.Scanner;
    public class ValidateData {
    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    Student student = new Student();
    String name;
    System.out.println("What is the students name? " );
    name = reader.nextLine();
    student.setName(name);
    System.out.println("Test #1 score: " );
    student.setScore(1, reader.nextInt());
    System.out.println("Test #2 score: " );
    student.setScore(2, reader.nextInt());
    System.out.println("Test #3 score: " );
    student.setScore(3, reader.nextInt());
     
    String result = student.validateData();
    if(result==null)
    System.out.println("All is OK\n" );
    else
    System.out.println(result+"\n" );
    }
    }

    The program works fine with the exception that it does not prompt the user if the "name" is missing in case the name wasn't entered. The program also does not display the average of the scores and the coding for that is inserted into the Student class. The program does, however prompt the user that the scores entered are not in range if they are >100.
    Any help with the syntax of these codes would be greatly appreciated,
    Thank you.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Validation based program that doesn't function properly

    Debug your program, and watch the value returned by name = reader.nextLine(); Maybe it's a null, and in the validation method, you test only for "". Try to test for length for example:
     if (name.length() < 3) message = "sorry please enter a name";
    because you should have at least 3 chars for a valid name.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:03 PM.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Validation based program that doesn't function properly

    Quote Originally Posted by daniel.j2ee View Post
    Debug your program, and watch the value returned by name = reader.nextLine(); Maybe it's a null, and in the validation method, you test only for "". Try to test for length for example:
     if (name.length() < 3) message = "sorry please enter a name";
    because you should have at least 3 chars for a valid name.
    What if
    name="1234";

    @kratos: You must read the difference between equals() and ==

Similar Threads

  1. Help With Validation
    By bengregg in forum Loops & Control Statements
    Replies: 4
    Last Post: February 1st, 2011, 04:24 AM
  2. Help with String based program!!!
    By astroann in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 27th, 2010, 10:07 PM
  3. Help with String based program!!!
    By astroann in forum Member Introductions
    Replies: 3
    Last Post: December 27th, 2010, 10:07 PM
  4. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 3rd, 2010, 12:33 AM
  5. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 09:43 AM