Results 1 to 7 of 7
  1. #1
    syferite is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Boolean method help

    Okay, I have this program:

    Java Code:
    import java.util.*;
    public class somethingextra {
    
    		public static void main (String [] args) {
    			
    			String name = readName();
    			int age = readAge();
    			char  gender = readGender();
    			float mark = readMark();
    			
    			char grade = getGrade(mark);
    			String desc = getDescription(grade);
    			boolean eligible = eligibleNs(age, gender);
    
    			
    			System.out.println ("\n" + name + " is " + age + " years old. " + 
    					"\nGender is " + gender + " with " + mark + " marks.");
    				System.out.println ("Grade is " + grade + " " + desc + "\n" + "\n" + eligible );
    		}
    		
    	    public static boolean eligibleNs (int age, char gender) {
    	    	
    	    	
    			if ((age >= 16) && (gender == 'M'))
    	    		
    	    		return true;
    	    	else
    	    		return false;
    	    }
    		
    		public static char getGrade (float mark) {
    			char grade;
    			if (mark >=50)
    				grade = 'P';
    			else
    				grade = 'F';
    			return grade;
    		}
    
    		public static String getDescription(char grade) {
    			
    			String desc = " ";
    			switch (grade) {
    			case 'P' : desc = "Pass";
    			break;
    			case 'F' : desc = "Fail";
    			break;
    			}
    			return desc;
    		}
    
    	    public static String readName() {
    		String name;
    		Scanner sc = new Scanner(System.in);
    		System.out.print ("Enter name: ");
    		name = sc.nextLine();
    		return name;
    	}
    
    	    public static int readAge() {
    	    	int age;
    	    	Scanner sc = new Scanner(System.in);
    	    	System.out.print("Enter age :");
    	    	age = sc.nextInt();
    	    	return age;
    	    }
    	    
    	    public static char readGender() {
    	    	String str;
    	    	char gender;
    	    	Scanner sc = new Scanner(System.in);
    	    	System.out.print("Enter gender :");
    	    	str = sc.nextLine();
    	    	gender = str.charAt(0);
    	    	return gender;
    	    	
    	    	
    	    }
    
    	    public static float readMark() {
    	    	float mark;
    	    	Scanner sc = new Scanner (System.in);
    	    	System.out.print ("Enter mark :");
    	    	mark = sc.nextFloat();
    	    	return mark;
    	    }
    
    
    
    	} //end
    When I run this. Everything works fine. However I'd like to know whether I am able to do this:

    Instead of having the output of "true" and "false", I'd like it to be "Eligible" and "Not Eligible". I am not sure on how to start and go on doing that. I tried to create a new method, but I ended up just getting the output of "Eligible" only o__o''

    I have never had experience with boolean stuff, so if anyone can point me out to some good links that could teach me more about it, I'd really appreciate it!

    Thanks!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You could just use an if statement for that.
    or you could go fancy and use

    Java Code:
    String eligibleAsString = eligible?"Eligible":"Not Eligible";

  3. #3
    syferite is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I can get it to work by replacing this:

    Java Code:
    	    public static boolean eligibleNs (int age, char gender) {
    	    	
    	    	
    			if ((age >= 16) && (gender == 'M'))
    	    		
    	    		return true;
    	    	else
    	    		return false;
    	    }
    with this:

    Java Code:
    		public static String nsTest (int age, char gender) {
    			String ns;
    			if ((age >=16) && (gender=='M'))
    				ns = "Eligible";
    			else
    				ns = "Not Eligible";
    			return ns;
    			
    		}

    and of course, changing the code for calling the method accordingly.

    But I was wondering how can I get the same result using the boolean method. Its rather confusing, and I really don't understand it at all :(

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    A boolean method returns either true or false so you can't make it return strings.

    It is the calling code that should be allowed to interpret that returned value as they wish. Changing your method to returning a string may not be a good workaround. You might need to know whether eligible or not without printing out the results (e.g to perform some action based on eligibility).
    Better keep the method returning boolean and let the users of the method do the if-else bits to convert to String (if they want to) like I suggested above.

  5. #5
    syferite is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I tried what you suggested and it worked.
    Thanks!

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You'll realize with time (even as you have done now) that there are always many different ways of doing things in Java. Sometimes the options are so much that they appear to be too much. Which option you choose determines how good a developer you are.

  7. #7
    gcampton Guest

    Default

    if you prefer working with variables rather than true / false you can declare a variable:

    private boolean ns=false; and then set / get it, as you can see, just saying true or false is a lot less effort, but can be/is poor practise :P

    Java Code:
    public static String nsTest (int age, char gender) {
        String ns;
        if ((age >=16) && (gender=='M')) {
    	ns = true;
        }
        else {
    	ns = false;
        }
    	return ns;
    			
    }
    making use of this variable is very easy, code such as:
    Java Code:
        if (ns)
        {
            runSomeMethod;
            orAssignSome = variables;
        }
        else
        {
            declareSomeOtherThings;
        }
    Last edited by gcampton; 10-30-2009 at 08:38 AM.

Similar Threads

  1. Testing boolean method
    By SteroidalPsycho in forum New To Java
    Replies: 7
    Last Post: 10-23-2009, 04:12 AM
  2. destroyApp(boolean unconditional) method
    By thienphongvu in forum CLDC and MIDP
    Replies: 1
    Last Post: 08-07-2009, 09:14 AM
  3. [SOLVED] calling a boolean method, confusion!!
    By AngrYkIdzrUlE in forum New To Java
    Replies: 18
    Last Post: 03-15-2009, 10:23 AM
  4. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 02:36 AM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 02:01 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
  •