Results 1 to 7 of 7
Thread: Boolean method help
- 10-23-2009, 12:54 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
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
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!
- 10-23-2009, 01:01 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
You could just use an if statement for that.
or you could go fancy and use
Java Code:String eligibleAsString = eligible?"Eligible":"Not Eligible";
- 10-23-2009, 03:30 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
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; }
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 :(
- 10-23-2009, 03:39 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
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.
- 10-28-2009, 07:47 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
I tried what you suggested and it worked.
Thanks!
- 10-28-2009, 09:42 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
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.
- 10-28-2009, 01:32 PM #7gcampton Guest
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; }
Java Code:if (ns) { runSomeMethod; orAssignSome = variables; } else { declareSomeOtherThings; }
Last edited by gcampton; 10-30-2009 at 08:38 AM.
Similar Threads
-
Testing boolean method
By SteroidalPsycho in forum New To JavaReplies: 7Last Post: 10-23-2009, 04:12 AM -
destroyApp(boolean unconditional) method
By thienphongvu in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 09:14 AM -
[SOLVED] calling a boolean method, confusion!!
By AngrYkIdzrUlE in forum New To JavaReplies: 18Last Post: 03-15-2009, 10:23 AM -
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM
Bookmarks