Timer in the code
1 Answer(s)      5 years and 6 months ago
Posted in : Java Beginners
View Answers

November 23, 2009 at 4:34 PM

Hi Friend,

Try the following:

import java.util.*;

class PrimesCMDLine{
public static void checkPrime(long num){
long i=0;
for ( i = 2; i < num; i++) {
long n = num % i;
if (n == 0) {
System.out.println("Entered number is not Prime!");
if (i == num) {
System.out.println("Number is Prime!");

public static void findFactors(long n){
System.out.print("The prime factorization of " + n + " is: ");
for (long i = 2; i <= n / i; i++) {
while (n % i == 0) {

n = n / i;
System.out.print(i + " ");
if (n > 1) System.out.println(n);
else System.out.println();

public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(;
String menu = "";
System.out.println("p. check prime or not");
System.out.println("f. prime factors");
System.out.println("e. Exit");

boolean quit = false;
System.out.print("Please enter your choice: ");
menu =;
char ch=menu.charAt(0);
switch(ch) {
case 'p':
System.out.print("Enter number: ");
long num=scan.nextLong();
case 'f':
long start = System.currentTimeMillis();
System.out.println("Start: " + start);
System.out.print("Enter number: ");
long num1=scan.nextLong();
long end = System.currentTimeMillis();
System.out.println("End : " + end);
long elapsedTime = end - start;
System.out.println(elapsedTime + " milli seconds");
case 'e':
quit = true;
System.out.println("Invalid Entry!");
while (!quit);

If you got any error or any problem, then send your code.


Related Tutorials/Questions & Answers:
code for timer in java netbeans for online test series countdown timer
code for timer in java netbeans for online test series countdown timer  code for timer in java netbeans for online test series countdown timer
Timer in the code - Java Beginners
Timer in the code  This is the question I posted yesterday: "I'm.... I'm confused about where and what i should put into the overall code. Also I can't quite figure out how to write the code for the GUI. Could anyone please help
j2me timer and timer task
j2me timer and timer task  have anyone idea to change a direction of bubble, according to changing time.   Please go through the following links: http
Java timer
Java timer  Hi. I want to use a timer in one of my java projects... how to store the input in a string. Please show me how. Here's the code: import java.util.Timer; import java.util.TimerTask; public class ToDo { Timer
ServletContextListener with Timer
In this section, you will learn how to use timer with ServletContextListener to run code snippet after every 1 minute immediate after the application run
Timer and TimerTask
Timer and TimerTask  i have problem of java.lang.IllegalStateException: Task already scheduled or cancelled
timer using javascript
timer using javascript  How to display timer on HTML page using JavaScript
Difference between Timer and Thread?
Difference between Timer and Thread?  Can anyone tell me about the difference between Timer and Thread, Why we need to have Timer in case we have Thread implimentation startegy in Java
javascript timer countdown
javascript timer countdown  How to disable/enable the javascript timer countdown?   <!-- CSS Code --> #txt { border:none; font... you to start and stop the timer after few seconds. Run the code to see how
Java Timer Task
Java Timer Task  How to assign a task in java at specified date & time (with seconds) using threads
Countdown timer to show a link
Countdown timer to show a link  Hi to everyone! I have this code.... The day arrives, and the link that leads to the songs appears. This is the code (you have to click on the countdown so the page with the code appears at the left
JavaScript Clock Timer
JavaScript Clock Timer...; In this section, we are going to show you a clock timer using JavaScript... than 9, then it will add 0 in front of min, sec or hour value like 09 in the timer
Insertion Sort Timer
Insertion Sort Timer  Welcome all I wanna program in java find the timer of insertion sort and find time complexity for random value thanks all
Retrieval data from database against timer
Retrieval data from database against timer  Please send me a sample timer code which retrieve data from the database against timer in JSP
Timer - Swing AWT
Timer  How can I make a simple countdown timer in my GUI?  ... { ProgressMonitor progress; static int counter = 0; Timer timer...("Progress bar timer Demo"); setSize(250, 100); setDefaultCloseOperation
Java + Timer concept - Java Beginners
interval) and retrieve values and print it in console. I want code using timer...Java + Timer concept  sir, I am using swing based model. My form... will change after some span of time) and print it in console. I want code and idea
J2ME Timer MIDlet Example
J2ME Timer MIDlet Example       This Example shows how to use of timer class. In this example we are using the Timer class to create the time of execution  of application
Servlet and Timer - JSP-Servlet
me with some code......... Thanks in advance
Registration timer expired, but client is still registering!
Registration timer expired, but client is still registering!  Registration timer expired, but client is still registering
java hello and timer alert application
java hello and timer alert application  Hi, Please help me create a simple Java application that can display "Hello world" and current Time alert on mobile device screen using mobile screen emulator. Regards
Scheduling a Timer Task
Scheduling a Timer Task       In this section, you can learn how to schedule a timer task to run... the timer task at a certain time means the task will work only once while you
J2ME Timer Animation
J2ME Timer Animation       This application illustrates how to use the Timer class..., which helps you to understand using of timer class for drawing the canvas
Please tell me how can i convert string to timer
Please tell me how can i convert string to timer  Please tell me how can i convert string to timer
code  code for android sample program
code    how to write this in java
Code  code for connecting c lang to database
code for this is
code for this is  a b c d c b a a b c c b a a b b a a a code
code  please provide code for custom using currdate tag we need to get current date?please give me code
code   hi I need help in creating a java code that reminds user on a particular date about their festival. i have no clue of how to do it.. am looking forward to seek help from you
the correct code for a program.The output of the program is listed below...: {Block 5} ADDRESS-3: {San Juan} POST CODE:{6745} ENTER CODE (XX TO stop) CODE:{FF1} QUANTITY:{2} CODE:{TR4} QUANTITY:{1} CODE:XX INVOICE FOR YOUR ORDER Harry