boolean counting short program
Hi. I'm on day 2 of Java - and day 2 to programing in general-so please bear with me. I'm trying to write a short program using the IsPrime method to count primes from 1-500. Below is what I've written. The class is listed below that and then the error message that results. Any help would be appreciated.
public class Prime {
public static void main (String[] arguments) {
final int LIMIT = 500
int count = 0
do
System.out.println (count);
count++
while (count <LIMIT)
int number = input.nextInt();
Prime p = new Prime();
boolean isPrime = isPrime(number);
if (isPrime == true)
System.out.println("%d is Prime...500", number);
else
System.out.println("%d is not Prime...500", number);
}
}
class Prime
{
public boolean isPrime( int number ) {
for (int factor = 2; factor ><= Math.sqrt(number); factor++)
if (number % factor == 0)
return false;
return true;
}
}
C:\J21work>javac 2assignment07.java
2assignment07.java:5: ';' expected
int count = 0
^
2assignment07.java:10: ';' expected
while (count <LIMIT)
^
2 errors>
[1254 byte] By [
yadaya777a] at [2007-11-27 6:06:55]

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to read.
1) You need to put ; on the ends of all your lines (that are not blocks of code).
2) You need { } with the lines that are blocks of code.
So
final int LIMIT = 500
int count = 0
Should be
final int LIMIT = 500;
int count = 0;
And you need { } around the do while block.
Hii ,
I think this will help you
public class Test14
{
public static void main (String[] arguments)
{
final int LIMIT = 500 ;
Prime p = new Prime();
int number = 0;
int count = 0 ;
boolean isPrime = true ;
do
{
isPrime = p.isPrime(count);
if (isPrime == true)
{
System.out.println(count+" Is prime ");
}
else
{
System.out.println(count+" Is not prime");
}
count++;
}while (count <LIMIT);
}
}
class Prime
{
public boolean isPrime( int number )
{
for (int factor = 2; factor ><= Math.sqrt(number); factor++)
{
if (number % factor == 0)
return false;
}
return true;
}
}
int count = 0count++Put semicolon(;) at the end of each above statements in u r code after
> Hii ,
>I think this will help you
>>
> s Test14
> {
>public static void main (String[] arguments)
> {
>
> final int LIMIT = 500 ;
> Prime p = new Prime();
> int number = 0;
> int count = 0 ;
> boolean isPrime = true ;
> do
> {
>isPrime = p.isPrime(count);
> if (isPrime == true)
>{
> System.out.println(count+" Is prime ");
>
> else
> {
> System.out.println(count+" Is not
> prime");
>
> }
>
> count++;
> }while (count <LIMIT);
>
>
>}
> class Prime
> {
>public boolean isPrime( int number )
> {
>
> for (int factor = 2; factor <= Math.sqrt(number);
> factor++)
> {
>
> if (number % factor == 0)
>return false;
>}
>return true;
> }
> }
>
>
>
Hi and thank you very much for the direction. I am still having difficulty with the if else loop; specifically the statement enclosed in paranthesis. I really have spent some time trying to figure out the unclosed string literal error message I'm getting for line 15 and 19.
Hi,
I found some of the errors mentioned above, and I believe I found some others as well.
1) Your first posting of code shows you declaring the class Prime twice, was this just an oversight when you posted? If not, move the isPrime() method into the first definition of class Prime(), this is the easiest way to achieve what you want (i.e. not having to instantiate a class, etc).
2) You had what appears to be a call to the scanner class in
input.nextInt();
but I didn't see where you created an object of Scanner. Maybe I am missing something, but I don't think that will compile, will it?
3) As noted above, you need ';' terminating your lines. Also, not required, but I find it extremely helpful to place all code inside of if blocks within {}. This is only required when there are multiple lines of code within the block, but having them makes debugging so much easier.
I touched up hyour code a bit, I'm no expert, but it works.... let me know if you have any questions.
import java.util.Scanner; //In order to access the Scanner class you need to import it first
public class Prime {
public static void main (String[] arguments) {
final int LIMIT = 10;
int count = 0;
Scanner keyboard = new Scanner(System.in);
do{
System.out.println (count);
count++;
}while (count <LIMIT);
System.out.println("Enter a prime number:"); //When requring console input, alwasy prompt the user
int number = keyboard.nextInt();
if (isPrime(number)) { //Curly braces are your friend....
System.out.printf("%d is Prime...500", number);
}
else {
System.out.printf("%d is not Prime...500", number);
}
}
public static boolean isPrime( int number )
{
for (int factor = 2; factor ><= Math.sqrt(number); factor++) //Not sure about ><= you had here....
{
if (number % factor == 0)
{
return false;
}
return true;
}
return false;
}
}
> public static boolean isPrime( int number )
> {
> for (int factor = 2; factor ><= Math.sqrt(number); factor++) //Not sure about ><= you had here....
> {
> if (number % factor == 0)
> {
> return false;
> }
> return true;
> }
> return false;
> }
Why are you returning true inside of the for loop? And the >< thing is a forum bug.
Because I'm an idiot! I didn't look close;y enough at that method... how is this?
public static boolean isPrime( int number )
{
for (int factor = 2; factor <= Math.sqrt(number); factor++) {
if (number % factor == 0)
{
return false;
}
}
return true;
}
Hi. Thank you for the advice. All of the advice is a huge help and gives me more to study and read over and over. Scanner class--I have been looking at so many examples I don't know where I picked that up from. input.nextInt();
When I read that code it is not clear the implications and functions and didn't realize it was part of the Scanner class. I'm not sure what the seamingly popular Scanner class does for that matter.This also applies to why I put a returning true inside the for loop. It's just that I'm trying to learn and figure out programming and this language altogether.
Hi,
The Scanner class is just a way to read input from the console, files, etc. etc. Instantiating an object of the Scanner class as done below, allows your program to take input from the console, which is normally from the keyboard. That's why I call the object 'keyboard', but you could call it anything you want.
Scanner keyboard = new Scanner(System.in);
With this object you can then do the following, and much more:
keyboard.nextInt(); //Retrieves the net int entered into the console
keyboard.nextDouble(); //Gets the next double
keyboard.next(); //Gets the next word
keyboard.nextLine(); //Gets the next complete line.
Scanner is a pretty cool class when you begin programming with Java. Though, Java 6 has a new way to get input from the console, but the class name escapes me now.
Hope this helped,
Kevin
> Scanner is a pretty cool class when you begin
> programming with Java. Though, Java 6 has a new way
> to get input from the console, but the class name
> escapes me now.
[url=http://java.sun.com/javase/6/docs/api/java/io/Console.html]java.io.Console[/url], for the curious.
~