Results 1 to 20 of 20
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default im not familiar with boolean in method...

    i have trouble with the highlighted part and im not familar with boolean. the reason that i used it in my code because the problem required it.



    Java Code:
    import util.java.Scanner;
    public class CreditCard
    {
    	public static void main(String[]args)
    	{
    
    	Scanner input = new Scanner(System.in);
    
    	System.out.println("Enter the 16-digits of Credit Card number for vaildation.");
    		long cardNum = input.nextInt();
    
    	System.out.println(cardNum+isValid());
    
    }
    //Vaild
    	[COLOR="SeaGreen"]public boolean isValid(long number)
    	{
    	int sum = sumOfEvenPlace() + sumOfOddPlace();
    	if (sum%10 ==0 ){
    	 return 'is valid';
    	}
    	if (sum%10 != 0){
    	 return 'is invalid';
    	}
    }[/COLOR]
    //Even
    	public static int sumOfEvenPlace(long number)
    	{
    	int total= 0;
    
    	for (int i = 2; i<=16; i += 2)
    	{
    		int even = (cardNum/(Math.pow(10,i-2)))%10;
    		if (even > 4){
    		even = getDigit();
    	}
    		total += even;
    	}
    	return total;
    	}
    //Digit
    	public static int getDigit(int number)
    	{
    		int Over = even*2;
    		int fir =Over/10;
    		int sec = Over%10;
    		even = fir + sec;
    		return even;
    		}
    
    //Odd
    
    	public static int sumOfOddPlace(long number)
    	{
    	int total1=0;
    	for (int j=1; j<=15; j +=2)
    	{
    	int odd = (cardNum / Math.pow(10,j))%10;
    		total += odd;
    	}
    	return total1;
    	}
    
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    boolean is a primitive type with value either true or false.

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    boolean is a primitive type with value either true or false.
    i know the definition but not sure how to apply it.
    so what are suggesting me to do?
    change those ifs statments to true or false?
    Last edited by PureAwesomeness; 02-21-2009 at 03:55 AM.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    why don't you try it and see if it produce the result you wanted.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    public boolean isValid(long number)
    {
    int sum = sumOfEvenPlace() + sumOfOddPlace();
    true (sum%10 ==0 ){
    return 'is valid';
    }
    false (sum%10 != 0){
    return 'is invalid';
    }
    }
    it doesnt work =[

  6. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Java Code:
    public boolean isValid(long number)
    	{
    	int sum = sumOfEvenPlace() + sumOfOddPlace();
    	true (sum%10 ==0 ){
    	 return 'is valid';
    	}
    	false (sum%10 == 0){
    	 return 'is invalid';
    	}
    }
    it doesnt work =[

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    change those ifs statments to true or false?
    why didn't you try it...

    what is 'is valid' and 'is invalid' ?? thats not a valid statement you know.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh! lol you literally change the if to true and false. didn't catch that.

    nono you have to do this:
    if (somehting) return true;
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    how can i return a "system.out.println" statment in that method?

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    u can't........9A
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    the how am i gonna use "return true" or "return false"?

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    you could execute a print() method, but if thats all you're going to do then it should be declared void. It should be:
    if(sum % 10 == 0) { return false; }
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    print isValid(); when i put this in theres an error come up that i need a semicolon.
    and i cant declare void because the instructions said so.=[
    Last edited by PureAwesomeness; 02-21-2009 at 06:00 AM.

  14. #14
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    like this
    C:\Documents and Settings\HP_Administrator\Desktop\CreditCard.java: 12: ';' expected
    print isValid();
    ^

  15. #15
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    this is the modified version
    the error came up on the highlighted part
    and the "System.out.println" any suggestion on how i need to change it to display that?

    Java Code:
    import java.util.Scanner;
    public class CreditCard
    {
    	public static void main(String[]args)
    	{
    
    	Scanner input = new Scanner(System.in);
    
    	System.out.println("Enter the 16-digits of Credit Card number for vaildation.");
    		long cardNum = input.nextInt();
    
    [COLOR="Green"]	print isValid(); [/COLOR]
    
    }
    //Vaild
    	public boolean isValid(long number)
    	{
    	int a =	sumOfEvenPlace();
    	int b = sumOfOddPlace();
    	int sum = a + b;
    	if(sum%10 ==0 ){
    	 return true;
    	[COLOR="Green"] System.out.println(number+"is valid");[/COLOR]
    
    	}
    	else
    	return false;
    	 [COLOR="Green"]System.out.println(number+"is invalid");[/COLOR]
    
    }
    //Even
    	public static int sumOfEvenPlace(long number)
    	{
    	int total= 0;
    
    	for (int i = 2; i<=16; i += 2)
    	{
    	int even = ((int)(number/(Math.pow(10,i-2))))%10;
    		if (even > 4){
    		even = getDigit();
    	}
    		total += even;
    	}
    	return total;
    	}
    //Digit
    	public static int getDigit(int number)
    	{
    		int Over = number*2;
    		int fir =Over/10;
    		int sec = Over%10;
    		number = fir + sec;
    
    		return number;
    		}
    
    //Odd
    
    	public static int sumOfOddPlace(long number)
    	{
    	int total1=0;
    	for (int j=1; j<=15; j +=2)
    	{
    	int odd = ((int)(number / Math.pow(10,j)))%10;
    		total1 += odd;
    	}
    	return total1;
    	}
    
    }
    Last edited by PureAwesomeness; 02-21-2009 at 06:07 AM.

  16. #16
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    what mk12 meant was this
    Java Code:
    	[B]isValid(); [/B]
    }
    //Vaild
    	public [B]void[/B] isValid(long number)
    	{
    	int a =	sumOfEvenPlace();
    	int b = sumOfOddPlace();
    	int sum = a + b;
    	if(sum%10 ==0 ){
    	[B] //return true;[/B]
    	 System.out.println(number+"is valid");
    
    	}
    	else
    	[B]//return false;[/B]
    	 System.out.println(number+"is invalid");
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  17. #17
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Yes I just didn't include the System.out.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  18. #18
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    yea i know what he meant but the instructions said i need to use
    Java Code:
    public boolean isValid(long number)
    	{

  19. #19
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Java Code:
    public boolean isValid(long number) {
    	int a = sumOfEvenPlace();
    	int b = sumOfOddPlace();
    	int sum = a + b;
    	if(sum % 10 == 0){
    	          return true;
    	}
    	else {
    	          return false;
            }
    }
    And then in the code that uses the isValid method, do this:
    Java Code:
    String valid = (isValid([COLOR="Red"]12[/COLOR]))? "It's Valid!" : "Invalid.. try again";
    System.out.println(valid);
    change the number 12 in red to whatever you need.. and change the It's Valid/invalid messages if you wish. Hope that helped.

    Actually, a better way would be this:
    Java Code:
    System.out.println((isValid([COLOR="Red"]12[/COLOR]))? "It's Valid!" : "Invalid.. try again");
    I think that would work too, and would mean no unecassary variable.
    -MK12
    Last edited by MK12; 02-21-2009 at 07:27 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  20. #20
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    mk12 makes very good use of ?: operator, but might be a bit confusing for new comers. here's an inferior version.
    Java Code:
    boolean b = isValid(12);
    if(b)   System.out.println("valid");
    else   System.out.println("not valid");
    edit:
    if the post is solved, please mark it as solved.

    edit2:
    also, the method could be:
    Java Code:
    return sum % 10 == 0 ? true : false;
    Last edited by angryboy; 02-22-2009 at 02:39 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:01 AM
  2. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 02:01 AM
  3. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM
  4. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 AM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 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
  •