Install problem or run time problem?

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
laosland
Posts: 7
Joined: Thu Aug 28, 2003 2:02 pm

Install problem or run time problem?

Post by laosland »

I've just written a simple problem to find prime numbers and when I run it, it finished wihtout sending out any output other than the blank DOS shell telling me to Press any key to continue. Here's the code:
:?:
public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1, primeTrigger=1;
for (a=1;a>300;a++)
{

for (b=1;b>300;b++)
{
if (a%b>0)
{
primeTrigger=0;
}
}


if (primeTrigger==1)
{
System.out.println (a);
myPrime[x]=a;
x++;
}


}

}

}
/************************************************
Any ideas?
Thanks,
Andy
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

Code: Select all

 for (a=1;a>300;a++)
{
this loop is never entered: a is set to 1, the loop will run as long as a is bigger than 300.
As 1 is NOT bigger than 300 it will not run at all.

same for the loop with b.

Therefore (as all your outputs are within that for (a=... loop which will not be used, nothing is written to output.

So: neither install nor runtime problem - faulty programming...
laosland
Posts: 7
Joined: Thu Aug 28, 2003 2:02 pm

Thanks...

Post by laosland »

:oops: I guess I had a brain fart, as I was thinking that the second argument was *Do untill this is true*. Doh! Anyways, I rewrote it and the problem still happens:
/***************************************************
public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1;
boolean primeTrigger=true;
for (a=1;a<=300;a++)
{

for (b=1;b<=300;b++)
{
if (a%b>0)
{
primeTrigger=false;
break;
}
}


if (primeTrigger==true)
{
System.out.println (a);
myPrime[x]=a;
x++;
}


}

}

}
/***************************************************
User avatar
s_reynisson
Posts: 939
Joined: Tue May 06, 2003 1:59 pm

Post by s_reynisson »

Code: Select all

public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1;
for (a=1;a<=300;a++)
{
	boolean primeTrigger=true;  // reset for each number tested
	for (b=2;b<=a-1;b++)        // don't test for one and self
	{
		if (a%b==0)              // is div and thus flunks as prime
		{
		primeTrigger=false;
		break;
		}
	}
	if (primeTrigger)
	{
	System.out.println (a);
	myPrime[x]=a;
	x++;
	}
}
}
}
laosland
Posts: 7
Joined: Thu Aug 28, 2003 2:02 pm

Post by laosland »

:D I really, really appreciate the help. Now I really feel like a dunce, dividing by 1..doh!!!
Thanks for your help guys.
Andy
Post Reply