Results 1 to 20 of 20
- 02-21-2009, 03:44 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
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; } }
- 02-21-2009, 03:46 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
boolean is a primitive type with value either true or false.
- 02-21-2009, 03:49 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
- 02-21-2009, 04:09 AM #4
why don't you try it and see if it produce the result you wanted.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-21-2009, 04:24 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
it doesnt work =[public boolean isValid(long number)
{
int sum = sumOfEvenPlace() + sumOfOddPlace();
true (sum%10 ==0 ){
return 'is valid';
}
false (sum%10 != 0){
return 'is invalid';
}
}
- 02-21-2009, 04:24 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
it doesnt work =[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'; } }
- 02-21-2009, 04:47 AM #7
why didn't you try it...change those ifs statments to true or false?
what is 'is valid' and 'is invalid' ?? thats not a valid statement you know.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-21-2009, 04:49 AM #8
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)
- 02-21-2009, 05:22 AM #9
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
how can i return a "system.out.println" statment in that method?
- 02-21-2009, 05:40 AM #10
- 02-21-2009, 05:50 AM #11
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
the how am i gonna use "return true" or "return false"?
- 02-21-2009, 05:51 AM #12
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; }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 05:58 AM #13
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
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.
- 02-21-2009, 06:01 AM #14
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
like this
C:\Documents and Settings\HP_Administrator\Desktop\CreditCard.java: 12: ';' expected
print isValid();
^
- 02-21-2009, 06:05 AM #15
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
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.
- 02-21-2009, 06:12 AM #16
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)
- 02-21-2009, 03:54 PM #17
Yes I just didn't include the System.out.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 06:04 PM #18
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
yea i know what he meant but the instructions said i need to use
Java Code:public boolean isValid(long number) {
- 02-21-2009, 07:25 PM #19
And then in the code that uses the isValid method, do this: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; } }
change the number 12 in red to whatever you need.. and change the It's Valid/invalid messages if you wish. Hope that helped.Java Code:String valid = (isValid([COLOR="Red"]12[/COLOR]))? "It's Valid!" : "Invalid.. try again"; System.out.println(valid);
Actually, a better way would be this:
I think that would work too, and would mean no unecassary variable.Java Code:System.out.println((isValid([COLOR="Red"]12[/COLOR]))? "It's Valid!" : "Invalid.. try again");
-MK12Last 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.
- 02-22-2009, 02:36 AM #20
mk12 makes very good use of ?: operator, but might be a bit confusing for new comers. here's an inferior version.
edit:Java Code:boolean b = isValid(12); if(b) System.out.println("valid"); else System.out.println("not valid");
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
-
Simple Boolean
By jigglywiggly in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:01 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM -
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks