+ Start a Discussion
new_bienew_bie 

How to write a apex class print 1 to 10 numbers ?

Hi

 

i am new to salesforce how to print 1 to 10 numbers in salesforce.
can you please give me Apex class and testclass for this.

 

i wrote a class

 

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

 

how to check whether it is working or not.

 

Regards.

Vinit_KumarVinit_Kumar

Go to Developer Console and run the below code :-

 

Integer num;

 

for( num=0;num<=10;num++){
system.Debug(num);
}

new_bienew_bie
Hi vinit_kumar,

Thanks for immediate response.

your code is working fine.

but i type

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

above code it is not working can you explain what is reason?

in generally writing an apex class is there any rules is there.

Regards.
Eugene NeimanEugene Neiman

First drop the class into Apex classes.   Here's my version (simpler)

 

public class printTenNumbers{

public static void print(){
for(integer num=0;num<=10;num++){
system.Debug(String.valueof(num));
}
}
}

 

Set yourself up for debug logging then go to the developer console and execute

 

printTenNumbers.print();

 

The debug log will display 1-10 as User Logs

 

11:10:09.047 (47697000)|USER_DEBUG|[5]|DEBUG|1
11:10:09.047 (47767000)|USER_DEBUG|[5]|DEBUG|2
11:10:09.047 (47828000)|USER_DEBUG|[5]|DEBUG|3
11:10:09.047 (47888000)|USER_DEBUG|[5]|DEBUG|4
11:10:09.047 (47949000)|USER_DEBUG|[5]|DEBUG|5
11:10:09.048 (48011000)|USER_DEBUG|[5]|DEBUG|6
11:10:09.048 (48117000)|USER_DEBUG|[5]|DEBUG|7
11:10:09.048 (48178000)|USER_DEBUG|[5]|DEBUG|8
11:10:09.048 (48238000)|USER_DEBUG|[5]|DEBUG|9
11:10:09.048 (48298000)|USER_DEBUG|[5]|DEBUG|10

 

How's that?

 

 

 

 

Thomas DvornikThomas Dvornik
new_bie, you class works fine. Just execute the following:

new printTenNumbers().print();

If you want to use printTenNumbers.print(); then change your method to static.
new_bienew_bie
Hi Eugene,

Your code is working Fine.

why you use static method. If i am not using the static method how to execute this one.
Is there any rules for execution of class static is mandatory can you explain clearly.

i want to print a integer num why you use a String.Valueof(num) alredy number is integer.

Regards.
Vinit_KumarVinit_Kumar

newbie,

 

If you want to excute your code in developer console,try below :-

 

printTenNumbers pn = new printTenNumbers();

pn.print();

new_bienew_bie
Hi

how to write a test class for this code

public class printTenNumbers{
public integer num{get;set;}
public integer print(){
for(num=0;num<=10;num++){
system.Debug(num);
}
return num;
}
}

Teach me...

Regards