I don't know if any of you have started the project yet, but I have come across a snag in my code. I'm trying to determine if a number is a whole number in an IF STATEMENT. My code takes a number in the array, divides it by 2, and then I want to basically say "if result is whole number then..." , but I'm not sure how or if I can do that. Any help would be appreciated.
You could use the mod operator and then just check the remainder. If there is a remainder, it didn't go in an even number of times (not a multiple). For example, x=88%2 would result in x being set to zero, while x-89%2 would result in it being set to 1. Since you're working with whole numbers anyway, the only fractions you would get would be quotients and you don't need those except to determine whether the number goes in an even number of times or not. If the result of the mod operation is anything other than zero, then the divisor didn't go into the dividend an equal number of times.
Okay, I got online and think I found enough information to write the correct statements. I apologize for the allignment. I just did a copy and paste. I'm getting 18 errors when I compile, and about 11 of them are somewhere in the following statements. One that stood out to me was that one error said " i < num.length " is not a statement and another error said a " > " was expected in the statement.
Does anyone see anything that stands out as just plain wrong? I've also got some errors with brackets and semi-colons, but I don't know why. I've looked through my code and I'm pretty sure that I have a matching close bracket " } " for every open bracket " { ".
It looks like a for loop whereas what you really seem to want is a while loop. A for loop requires 3 things, the condition, the counter, and the increment. Substitute a "while" for the "for" and see what happens.
Is there a good way to wait for all of the threads to finish running before printing the prime numbers? I create and start a thread each time i pass through my for loop, once i exit the loop it prints the prime numbers, but some of the threads are still running and my output looks like this:
22: 73 23: 79 Starting thread for 41. Starting thread for 43. Starting thread for 47. 24: 83 Ending thread for 41. Ending thread for 43. Ending thread for 47. 25: 89 26: 97
I know the join() method might work, but calling it within the loop causes the program to wait for each thread to finish before going through the loop again.
for now I have a static variable acting as a counter of all the threads running. Whenever a thread is started (within main) i increment the counter, and whenever a thread is finished (inside the run() method) i decrement it. Before my loop that prints the results, i just have an empty loop:
while(threadsRunning > 0) { }
so that main does not go on to execute the rest of the code until all threads have finished. I don't know if there is a more efficient way to solve this problem though.
Off the top of your head, is there anything that stands out that would cause a "reached end of file while parsing" error? I'm getting it at the last ' } ' of my code.
I don't know specifically, but it sounds like you are trying to read more data after reaching the end of a file. Since we aren't reading a file for Project 3, I'm not sure where the error would come from. Maybe you can stop in on Friday and bring your code and we can take a look. Or you can post a snippet of the code here to see if anyone has run into a similar problem.
I'm getting the same error also. I'm using netbeans and its always the last close bracket thats "wrong" as if i don't have enough brackets. Not sure how to fix it. Its a date, Prof Garrett, see you Friday 9AM.
I'm getting really weird error on the most basic setup of my project.. it won't let me make a loop unless I have this random set of brackets { } in my code.. it order for it to compile I had to put the opening after my array decloration and the } at the end of the program. I have to do the { at the end of the last line of code before the loop, and it does this with every type of loop I've tried. not sure what would cause this, haven't had it before and cut the program as basic as I can trying to figure out the problem. (this sounds like the same prob some of you are having, throwing the code out there with mine)
public class project3 { static String[] array = new String[100];{ for (int tmp = 1; tmp < 100; tmp ++){ array[tmp] = "True"; } }}
I'm having a really stupid problem with my if statement, I'm at the end of the program trying to just print the list of prime numbers and the if statement is in a loop checking 1 to 100 but it executes on every pass and I'm not sure why...
for (int tmp = 1; tmp <= 100; tmp ++){ if (array[tmp]=="True");{
I've tried using the .equals and a few other little systax changes to get it to work but it's always returning true on that check and I'm not sure why..
I used the empty loop/ with a static counter to wait for my threads also, but one thing you may want to add to the loop is a sleep(int) call in the loop. The reason i added the sleep in main is because once the system gets in the loop, it may sit their and do nothing for a while. The sleep method will let jvm move on to one of the other threads without (hopefully) wasting to much cpu time.
one downside is if you do add sleep, you have to put the loop in a try/catch for the InteruptedException because their is a chance main could be interupted.
The following snippet of code is how I begin to see if a number is prime or not by using a for loop. It keeps on setting everything from 2-50 true. Not sure why its doing that. Any thoughts???
for ( int i = 2; i < 51; i++){
if ( numbers[i] = true) { Multiple value = new Multiple (i); Thread one = new Thread (value); System.out.println ("Checking multiples of " + i); one.start();
So I've got most of my code written the way I want it to work, however I am running into a problem that I can't seem to find a solution for in my searching through numerous Java educational sources. My code basically does what I think most others do, which is I've created a loop and a thread is created and started each time through that loop. The problem is that I don't know how to get my thread objects to have unique names Code goes something like this: for (int j = 0; j < primes.length; j++){ if (primes[j] == true){ Thread ***** = new Primer(); *****.start(); } } Where the ***** represents the various code I need to put in to create these thread objects, but also make each one have their own name so that the compiler doesn't get pissed at me. (Note: Primer() is the name of my class that extends Thread and contains the run() method). Any thoughts on what simple or complicated solution I am missing would be appreciated.
michael i dont think you need each thread to have a different name. if you were to name it Thread thread1 = new primer, then each time through the loop it creates a new thread1 this method should work fine
for example my code for calling a new thread looks like while loop { Thread thread2 = new Thread(new CalcThread()); thread2.start(); } and this creates a new thread each time through the loop for the various values from 1-50 hope this helps as im not very good at explaning it kevin
I didn't have to import anything for this program, and to add the sleep(1); i used in my waiting loop in main all i had to do inclose the loop in a try and add a catch InterruptedException section (that is empty)