Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help in method calling~ Java
New Posts  All Forums:Forum Nav:

Help in method calling~ Java

post #1 of 9
Thread Starter 
I'm writing the codes for validating something. Basically I have a method that is supposed to call 3 other methods. Each of the 3 methods will return a boolean value and the method calling the 3 other methods is supposed to return a boolean value as well depending on the value of the 3 methods. I searched a lot on Google but I couldn't find out a way to call the three methods. Some help will be appreciated. The code's given below. Thank you
Code:
public class validateCreditCard {

public static boolean main(String[] args){
//numReader();
//numLengthCheck();
return true;
}
public static int numReader(int[] cardNum){
//java.util.Scanner numReader=new java.util.Scanner(System.in);
int size = cardNum.length;
return size;
}
public static boolean numLengthCheck(int size){
if(size==16){
return true;
}
else{
return false;

}
}
public static boolean numRangeCheck(int cardNum[]){
int i=0;
while(i<16){
if(cardNum[i]<0 || cardNum[i]>9){
return false;
}
else { 
i++;
}
}
return true;
}
public static boolean checkSum(int cardNum[]){
int sum=0;
for(int i=0;i<16;i=i+2){
cardNum[i]=2*cardNum[i];
if(cardNum[i]>9){
cardNum[i]=cardNum[i]-9;
}
}
for(int i=0;i<16;i++){
sum=sum+cardNum[i];
}
System.out.println(sum);
if(sum%10==0){
return true;
}
else{
return false;
}
}
}
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #2 of 9
I hope this is the info you are looking for. You are on the right track in the main method. A method is invoked by simply calling the method name.

The methods you have setup require parameters though. For instance numLengthCheck(int size) requires an int to be passed to it, so you can invoke the method like this:
Code:
int passedInt = 5
numLengthCheck(passedInt)
Also, you when returning a value from a method, you should have a variable to return to. Using the same method as an example, it's created as a boolean method. So you want to do something like this:
Code:
boolean response = numLengthCheck(passedInt)
I program with C#, so the syntax may not be correct. Someone please correct me if I'm wrong.
Nevermore
(17 items)
 
  
Reply
Nevermore
(17 items)
 
  
Reply
post #3 of 9
Thread Starter 
Ok. I've done what you asked me to. I still haven't managed to resolve my problem and here's why.
Quote:
import java.util.Scanner;
public class as {

public static void main(String[] args){
boolean validateCreditCard=validateCreditCard();
}
public static boolean validateCreditCard(){
Scanner reader=new Scanner(System.in);
int cardNum[] = new int[16];
for(int i=0;i<16;i++){
cardNum[i]=reader.nextInt();
}
int size = cardNum.length;
boolean cond1=numLengthCheck(size);
boolean cond2=numRangeCheck(cardNum);
boolean cond3=checkSum(cardNum);
if(cond1 && cond2 && cond3){
return true;
}
else{
return false;
}

}
public static boolean numLengthCheck(int size){
if(size==16){
return true;
}
else{
return false;

}
}
public static boolean numRangeCheck(int cardNum[]){
int i=0;
while(i<16){
if(cardNum[i]<0 || cardNum[i]>9){
return false;
}
else {
i++;
}
}
return true;
}
public static boolean checkSum(int cardNum[]){
int sum=0;
for(int i=0;i<16;i=i+2){
cardNum[i]=2*cardNum[i];
if(cardNum[i]>9){
cardNum[i]=cardNum[i]-9;
}
}
for(int i=0;i<16;i++){
sum=sum+cardNum[i];
}
System.out.println(sum);
if(sum%10==0){
return true;
}
else{
return false;
}
}
}
The problem is that I want the method that is calling the 3 other methods NOT inside my main method. If you see my code you will notice that my main method is empty. However, my porgram isn't working. How can I get it to run?
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #4 of 9
Glancing over, everything looks like it should work. Are you getting any compile or runtime errors?
Nevermore
(17 items)
 
  
Reply
Nevermore
(17 items)
 
  
Reply
post #5 of 9
Thread Starter 
I think it's a runtime error. It seems that the program is terminated as soon as i run it. It's not taking any inputs. What should I do?
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #6 of 9
It doesn't look like you ever have the program displaying anything:
Code:
System.out.print(variable);
To keep to program open after it's done executing:
Code:
Console.ReadLine(); //this just waits to accept input
Again, the syntax may not be entirely correct.
Nevermore
(17 items)
 
  
Reply
Nevermore
(17 items)
 
  
Reply
post #7 of 9
Thread Starter 
Thank you so much. It worked the first time. I found out later that I had to press enter after each digit and not after entering all the digits. +rep
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #8 of 9
No problem. Glad it's working for you.
Nevermore
(17 items)
 
  
Reply
Nevermore
(17 items)
 
  
Reply
post #9 of 9
Thread Starter 
could you please check my other post?
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help in method calling~ Java