3 Replies - 251 Views - Last Post: 07 October 2012 - 04:37 PM Rate Topic: -----

#1 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Not understanding these errors

Posted 07 October 2012 - 03:07 PM

I keep getting these errors:
PrimeNumber.java:15: '.class' expected
		if (int factor % num == 0)
		        ^
PrimeNumber.java:15: illegal start of expression
		if (int factor % num == 0)
		               ^
PrimeNumber.java:15: ';' expected
		if (int factor % num == 0)
		                ^
PrimeNumber.java:15: illegal start of expression
		if (int factor % num == 0)
		                     ^
PrimeNumber.java:15: ';' expected
		if (int factor % num == 0)
		                       ^
PrimeNumber.java:15: illegal start of expression
		if (int factor % num == 0)
		                         ^
PrimeNumber.java:24: 'else' without 'if'
			else if (prime == true)
			^
7 errors



This is my code:
import java.util.Scanner;

public class PrimeNumber
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number and I will");
	System.out.println("determine if it is prime.");
	int num = scan.nextInt();
	boolean isPrime = true;
	int factor;
	for (factor=2; factor<num; factor++)
		{
		if (int factor % num == 0)
			prime = false;
		}
		
		{
		if (prime == false)
		System.out.println(num + " is not prime");
		}
			{
			else if (prime == true)
			System.out.println(num + " is prime");
			}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not understanding these errors

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Not understanding these errors

Posted 07 October 2012 - 03:16 PM

It looks like you collected code from a number of sources and forgot to rename/redefine the various pieces as a part of the new program.

A Scanner keyboard is created, but a scan is used.
A boolean isPrime is created, but prime is tested.
The int factor is declared twice in the same scope, so that's an error.
Braces around if/else clauses cause else to be displaced from its if (I think - weird bracing).
Style: testing if ( prime == false ) is unnecessary. if ( prime ) is sufficient.

Try cleaning up those problems and post the updated code if you need more help.

This post has been edited by GregBrannon: 07 October 2012 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Re: Not understanding these errors

Posted 07 October 2012 - 04:23 PM

I got it to compile. Now when I enter numbers that are known to be not prime numbers, the program is saying that they are....here is the updated code:
import java.util.Scanner;

public class PrimeNumber
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Enter a number and I will");
	System.out.println("determine if it is prime.");
	int num = keyboard.nextInt();
	boolean isPrime = true;
	
	int factor;
	
	for (factor=2; factor<num; factor++)
		{
		if (factor % num == 0)
			isPrime = false;
		
		if (isPrime == false)
		System.out.println(num + " is not prime");
		
			else if (isPrime == true)
			System.out.println(num + " is prime");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Not understanding these errors

Posted 07 October 2012 - 04:37 PM

Well first of all your Ouputs are in your for loop so they are going to keep printing out. You only need to do the print AFTER the for loop.

Also you are getting the remainder o factor / num.
This means if I entered 50 as a number it will be doing 2 / 50, 3 / 50, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1