Results 1 to 2 of 2
  1. #1
    javamonster is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default I need your help!!! Please?

    hi guys this is the code so far but i need the upperLimit to be input from the user please help me?

    Java Code:
    public class Prime {
        
        private static int upperLimit = 1000;
        private static boolean[] flags;
    
        public Prime() {
        }
    
        public static void main(String[] args) {
            initialize(args);
            findPrimes();
            displayPrimes();
        }
        
        private static void initialize(String[] args) {
            if (args.length > 0) {
                upperLimit = Integer.parseInt(args[0]);
            }
            flags = new boolean[upperLimit + 1];
            for (int position = 0; position <= upperLimit; position++) {
                flags[position] = false;
            }
        }
    
        private static void findPrimes() {
            for (int position = 2; position <= Math.sqrt(upperLimit); position++) {
                if (!flags[position]) {
                    int multiple = position * 2;
                    while (multiple <= upperLimit) {
                        flags[multiple] = true;
                        multiple += position;
                    }                
                }
            }
        }
        
        private static void displayPrimes() {
            for (int position = 2; position <= upperLimit; position++) { 
                if (!flags[position]) { 
                    System.out.print(position + ", "); 
                } 
            }     
        }
    
    }
    Last edited by JosAH; 11-23-2012 at 10:07 PM. Reason: added [code] ... [/code] tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need your help!!! Please?

    Read up on how to use a Scanner object (check out the tutorials that you can find on Google), and give that a try. You'll likely find that it will suit your needs well. Best of luck and welcome to the forum!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •