Having trouble printing an array of prime numbers that has been resized

HI, im having trouble with my printPrimeNumbers() method printing the current state of an array, I can only get it to print the original state it was in. The array is inside of a primeNumbers object. I used checkAndResize to resize that array. my printPrimeNumbers method must be void without a parameter. How could i get my PrintPrimeNumbers method to print out a resized array without modifying the parameter? Any ideas.
Here is my PrimeNumbers class:
* Created by IntelliJ IDEA.
* User: Kevin
* Date: Mar 4, 2007
* Time: 1:53:56 AM
* To change this template use File | Settings | File Templates.
package primes;
public class PrimeNumbers {
    private boolean [] sieve;
    public PrimeNumbers(int  upper) {
        initializeSieve(upper);
    public int getNthPrime (int n){
    int prime = 0;
    double num;
    if (n >= sieve.length)
        checkAndResize(n + 1);
    for (int i = 0; i < sieve.length; i++){
        if(sieve)
prime++;
if (prime == n)
return i;
if (prime < n && i == sieve.length -1)
checkAndResize(2*sieve.length);
return -1;
public int getNumberPrimeNumbers(int n){
int primes = 0;
for (int i =0 ; i < sieve.length ; i ++){
if (n > sieve.length){
checkAndResize(n);
if(sieve[i])
primes++;
else if (sieve[i])
primes++;
return primes;
public int getSieveSize ()
return sieve.length;
public boolean isPrime (int n) {
if (n > sieve.length){
checkAndResize(n);
//initializeSieve(n);
return sieve[n];
// prints out the prime numbers inside sieve
public void printPrimeNumbers() {
int n = 0;
boolean first = true;
System.out.print("[");
for(int i = 0; i < sieve.length - 1; i++){
n++;
if(sieve[i] == true && n != sieve.length - 1) {
if(first) first = false;
else System.out.print(" ");
System.out.print(i);
System.out.println("]");
// checks length of sieve with N and then resizes sieve if nessecary.
private void checkAndResize (int n){
if ((n + 1) >= sieve.length){
initializeSieve(2*n);
private void setMultiples (int k) {
for (int i = 2*k; i < sieve.length; i += k)
sieve [i] = false;
private void initializeSieve (int upper){
if ( upper < 2)
sieve = new boolean [2];
else
sieve = new boolean [upper + 1];
sieve[0] = false;
sieve[1] = false;
for (int i =2 ; i< sieve.length; i ++ )
sieve[i] = true;
int bound = (int) Math.ceil(Math.sqrt(sieve.length));
for (int i = 2 ; i < bound ; i ++)
if (sieve[i])
setMultiples (i);
private String booleanToString (boolean value)
if (value)
return "T";
else
return "F";
public String toString (){
StringBuffer buf = new StringBuffer("[");
for (int i = 0; i < sieve.length -1 ; i ++)
buf.append(booleanToString (sieve[i]) + " " );
buf.append(booleanToString (sieve[sieve.length -1]) + "]");
return buf.toString();
here is the client code
        PrimeNumbers test = new PrimeNumbers(16);
        System.out.println(test);
        System.out.println("There are " + test.getNumberPrimeNumbers(16) +
                " prime nummbers in the sieve from 1 to 15. \n");
        System.out.println("There are " + test.getNumberPrimeNumbers(26) +
                "  prime numbers in the resized sieve from 1 to 25.");
        System.out.println("\nThe first 25 prime numbers are:");// makes sense why it doesnt work
        test.printPrimeNumbers();
        System.out.println("\nThe 13th prime number is: " + test.getNthPrime(13));
        System.out.println();
        System.out.println("The number 3001 is prime:  " + test.isPrime(3001));do you see how my methods resized it?
here is the output:
[F F T T F T F T F F F T F T F F F]
There are 6 prime nummbers in the sieve from 1 to 15.
There are 15 prime numbers in the resized sieve from 1 to 25.
The first 25 prime numbers are:
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]// this is only the first 15 elements
The 13th prime number is: 41
The number 3001 is prime: true
thanks for taking your time to look at this

What's the problem?
You say that there are 15 prime numbers in the range 1-25. Your method printPrimeNumbers() prints the last calculated primes, and that is 15. So the program works.

Similar Messages

  • I am having trouble with Itunes registering the Mp4 video that has been converted.  Help!!

    I am having trouble with Itunes registering the MP4 video that has been converted.  Help!

    What do you mean by iTunes registering the mp4?
    Which iTunes, on iPad, iTunes is a Store and it won't take your .mp4.
    On pc, check if it supports the mp4 (there are a number of different mp4 format) you have converted.
    Can you import it to computer iTunes?

  • Printing the percentage of the file that has been loaded

    Hello everyone!
    I'm using a FileInputStream and a DataInputStream to read my .DAT file, but I want to print out the amount of percentages of the file that has been loaded, in a System,out.print(""); statement, please tell me how I can do this. :)
                fileInStream = new FileInputStream("C:/Documents and Settings/Benjamin Dahse/Dokumenter" +
                        "/NetBeansProjects/Online Multiplayer Game Client/build/classes/net/client/game/io/Cache/" +
                        "main_file_cache.DAT");
                dataInStream = new DataInputStream(fileInStream);This by the way, is how I get the path to the file, I want my DataInputStream to read.

    Thank you for your comment, but out of this code:
                int percentage = 0;
                byte[] buffer = new byte[15000000];
                while(true){
                    percentage += dataInStream.read(buffer, percentage, Math.min(buffer.length,
                            fileInStream.available() - percentage));
                    System.out.print(percentage / fileInStream.available() + "\n");
                    if(percentage >= fileInStream.available()){
                        break;
                }The only output I get is 3?

  • I am also having trouble with my wifi connection (ipod 4g). it has been three or five days since the last time i connected my ipod with wifi. it always says: "Safari cannot open the page because it is not connected to the Internet"), even though the Wifi

    Help me out

    Do other devices successfully connect to the network?
    Does the iPod successfully connect to other networks?
    - Reset the iPod. Nothing will be lost
    Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Power off and then back on the router.
    - Reset networks settings: Settings>General>Reset>Reset Network Settings
    - iOS: Troubleshooting Wi-Fi networks and connections
    - iOS: Recommended settings for Wi-Fi routers and access points

  • A user is having trouble printing. Print jobs take FOREVER when they used to speed through

    A user is having trouble printing. Print jobs take FOREVER when they used to speed through (despite linked images, etc)
    These are just some notes from our troubleshooting:
    User was trying to print out InDesign document however during the flattening phase, the computer would stall and would not print out the document.
    The document does not print out using double sided or single sided.
    The first time I went out, all I did was a system restore and uninstalled mcafee security plus because we're on a university network and sometimes the firewall blocks network printing, and that worked. She tried to print again later and it went back to taking 30 minutes for a print job of about 19 pages. These print jobs used to take about 5 minutes at the most to print entirely, but they queued up immediately, versus now, it takes about 5 minutes just to get to the printer.
    Further troubleshooting that I did on a separate day:
    Printer was printing fine in November - problem started at some point this month (December)
    Printing is fine elsewhere, including in Adobe Acrobat
    I was able to print one page just fine - it had no images (tried to print the same page later, took forever)
    Some of the images are very large (100MB+) photoshop files (again, they were there before and it printed fine) - these images are linked (with no link errors)
    She prints to a network printer, but there's no firewall on (again, printing is fine elsewhere)
    She's essentially printing with the default settings, she just centers her print, instead of the default layout.
    Now even printing with the default print set (not changing any print settings at all) doesn't work. Jobs take 30 minutes. I can't figure out if it's file specific or where the problem is. Since she has no custom settings, my next option is to just do an uninstall and reinstall, but I'm worried that the problem will still be there. I was starting to think that maybe the file is just so corrupted from all the force quits that that's causing the problem, but... I'm running out of solutions.
    Any help? 

    I have only worked doing layout for one business in my past. In that job our department would sometimes experience slowdown in printing from time to time. However, we began to see extreme slowdowns over a month period. It was finally tracked down to two things. One was something about the RAID array going bonkers. The other was some of the CAT cabling going bad. It only affected large print jobs and copying large files through the network. Even so, it was not 100% of the time. Sometimes things went along smoothly.
    Point is, have your IT people been invited in to experience the issue and see if anything in the network is causing at least latency issues?
    Take care, Mike

  • I am having trouble printing to an Epson XP600.  It is a new printer and it has been replaced because we thought it was the printer.  Basically, when I print from Excel, only color will print.

    I am having trouble printing to an Epson XP600.  We have replaced the printer and we are still having the same problem.  When I try to print from Excel, only the color prints.  Any suggestions?

    Has anyone else had issues not being able to print black (such as a document)?  My photos print great, but when I try to print a simple black-ink document, it won't work. Any suggestions?

  • Having trouble printing over a home network to a Canon S9000 from iMac

    Hello there,
    I recently bought a iMac and am having trouble printing to my Canon S9000 over my home network. If I hook up the printer to the iMac I can print to it, but the PC that is also hooked up to the network can't. If the printer is hooked up to the PC, the PC can print to it, but the iMac can not.
    I downloaded the S9000 driver from Canon and installed it on the iMac. Then brought up the printer in system preferences. I found my printer OK, but I can't seem to find the S9000 driver in the list.
    When the printer is hooked up to the PC and I try to print to it from the iMac this is the error I get: "Error Number : 311 Printer is in use or an error has occurred. If an error has occurred, eliminate the cause of the error."
    Any help is greatly appreciated!
    Thanks!

    Your problem is due to the way that Canon wrote the drivers for that printer. It can be shared to Windows machines if it is on a Windows machine, or to Macs if it is on a Mac. It can't be shared cross-platform. It is not supported by GutenPrint, so there is no CUPS system driver available; if there was, you could put it on the Windows machine and share it, and use the CUPS driver to print on your Mac.
    I'm sorry, but that printer will not ever be able to be shared cross-platform unless and until Canon rewrites the drivers. I'd not recommend holding your breath while waiting.
    The best solution to cross-platform printing is to get a network printer. Many vendors, including Brother, Canon, Epson, and HP, make them, some of them at fairly low (under $100) prices.

  • I am having trouble printing a PDF document. Whenever I click print nothing happens but when I am printing documents from Word, Excel etc they print without a problem. I've tried printing as image but that did not work. I uninstalled Adobe Reader XI and d

    I am having trouble printing a PDF document. Whenever I click print nothing happens but when I am printing documents from Word, Excel etc they print without a problem. I've tried printing as image but that did not work. I uninstalled Adobe Reader XI and downloaded Adobe Reader X instead and that still doesn't work. I have Windows 7.

    Hello,
    Please follow the link to troubleshoot the printing issues with Adobe Reader:- Troubleshoot PDF printing | Acrobat, Reader.
    Regards,
    Nakul

  • I am having trouble printing  and am only getting part of the document which i have no margins set any help??

    i am having trouble printing  and am only getting part of the document  i have no margins set any help??

    What version of Pages?
    What Printer?
    If it is Pages '09, turn Comments off.
    Peter

  • After updating to Mountain lion I am having trouble printing to my wifi Epson printer

    After updating to Mountain Lion I am having trouble printing on my wifi Epson Workforce 645. I keep getting an error message and my computer is set in the system prefrences to that printer

    What is the error message?
    Try resetting the printer queues:
    http://support.apple.com/kb/PH11143
    This update also clears your "computer name" in Syst Prefs > Sharing, and entering a computer name solves network print issues for many.

  • I am having trouble printing photos of my iPad can you advise me how to set this up as my printer keeps saying can't find computer

    I am having trouble printing off photos from my iPad can you please advise me the best way to do this, thank you

    Does your printer support AirPrint - the models that support it, and some troubleshooting for it, is on this page. If it's not an AirPrint printer then does the printer manufacturer have their own app in the store that you can use, or there are third-party printer apps such as Print Central For iPad and Print n Share which might work.

  • I'm having trouble printing Mail messages

    I have just started having trouble printing my Mail messages. When I print through the Mail program, only the header (to/ from emails, date received, subject, etc) print out. This seems to be happening only with Gmail messages, but I can't be sure. I think it's new, and I haven't noticed any Mail software updates - then again, I usually just let all of the updates come through so it's possible something changed without me really noticing it.
    Any suggestions other than copy and paste into a different application to print? It's kind of frustrating...

    This appears to be a Mac OS X 10.4.11 issue caused by pressing <Enter> or <Return> after doing File > Print, but before the Print dialog sheet has been fully displayed. Waiting a couple of seconds to let the Print dialog be fully displayed seems to “fix” the problem.

  • I,m having trouble printing jewel cases since the last upgrade. The song titles all bunch up together and are unreadable.

    I'm having trouble printing jewel cases since the last upgrade. The song titles all bunch up together and are unreadable. Can you help?

    I can’t figure out how to import music.
    Import it into iTunes first, then use the media browser in iMovie '08 to locate it.

  • I am having trouble printing from my computer. The printer keeps giving me an error message.

    I have an hp officejet 6500A printer, i am having trouble printing from my computer which is an hp windows 7 64-bit  to my computer. I keep recieving an error message on the printer stating that the paper size or type does not match. What do I need to do to get my computer to print to my printer?

    Hi - A couple of things to try.  First, in whatever app you're in, go into File, Print and Printer Properties and set the paper size appropriately.  After that, go into Page Setup in whatever app you're in and set the appropriate size in there as well.  In many cases, this problem is caused by different sizes being set within the printer driver and the app.  If that doesn't work, go back into Printer Properties and try choosing a different paper type (Plain Paper).  I've also had that resolve the problem before.
    Hope that helps.
    Say Thanks by clicking the Kudos thumbs up. Please mark the post that solves your problem as an Accepted Solution so other forum users can utilize the solution.
    I am an HP employee.

  • I am having trouble printing I have a connection to my printer wirelessly but does not print out the correct page I want.When I do print I get a bunch of pages more than is needed and also get a code and symbols please help I am jammed at work

    I am having trouble printing I have a connection to my printer wirelessly but does not print out the correct page I want.When I do print I get a bunch of pages more than is needed and also get a code and symbols please help I am jammed at work

    This can be the result of selecting the wrong driver. An older, unsupported laser printer will sometimes work with the generic Postscript driver.

Maybe you are looking for

  • Cannot deploy vs2010 dacpac via sqlpackage.exe

    Hi Forum, I've got a project that must use Visual Studio 2010. I am using a data tier application project for my SQL database. Deployment of the dacpac via Visual Studio 2010 works without any problems, even pre/post scripts works correct. I can also

  • How to handle TDS for Contractor under Section 94C in SAP B1 9.0

    Hi Expert If the credit or the payment (One time Payment) in pursuance of the contract does not exceed Rs.30,000/-, no deduction has to be made at source. However , if the aggregate of all amounts paid/credited or likely to be paid/credited exceeds R

  • How to maintain change history for a custom field

    Hi All, I have added a new custom field in standard table FKKVKP by appending a new structure to FKKVKP. Now this custom field will appear in transaction CAA2. My requirement is I have to maintain change history for my custom field. In transaction SC

  • IDVD Does not Burn 2 or more Copies

    For a few weeks not I have been having an issue with iDVD where it will burn a DVD (dual layer), eject it through the DVD burner and the diaglog is stuck on burning but says done. There is no prompt to insert another DVD. I do insert another one and

  • Problem with ALV gird

    hi i have an ALV grid in my screen in that one of my field (quantity field )is editable when user changes that quantity value and say he will press enter button i need my control of the program in my PAI. but this is not happening, if  any one knows