PDA

View Full Version : Delete "else" token???




soccersquirt82
Aug 26, 2008, 03:17 PM
I am making a method for prime numbers. The only error I have is "Syntax error on token "else", delete this token." Why do I need to delete the else token? I need the else token to tell the computer when to return false. Please help!

[code]
static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x)
if ((x% divisor) == 0) {
return prime = true;}
divisor = divisor + 1;
else {
return false; }
}



davedelong
Aug 26, 2008, 03:22 PM
you don't. you need to restructure your loop. The divisor = line can't be between brackets like that. Also, the line "return prime = true" is non-sensical and returns true. Perhaps you mean prime == true?

static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x) {
if ((x% divisor) == 0) {
return prime = true;
}
divisor = divisor + 1;
else {
return false;
}
}

Cromulent
Aug 26, 2008, 03:24 PM
I am making a method for prime numbers. The only error I have is "Syntax error on token "else", delete this token." Why do I need to delete the else token? I need the else token to tell the computer when to return false. Please help!


static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x)
if ((x% divisor) == 0) {
return prime = true;}
divisor = divisor + 1;
else {
return false; }
}

You need to have if / else statements one after the other like so:

if(somestuff == tosomeotherstuff)
{

}
else
{

}

you can't have statements in between them so remove the extra line in your code or move it out of the way.

P.S Please use the code tags correctly.

soccersquirt82
Aug 26, 2008, 03:35 PM
I have another error now that I fixed the last one. It says, "This method must return a result of type boolean." Also, I tried doing the prime == true, but I got another error about that.


static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x)
if ((x% divisor) == 0) {
return true;}
else {
return false; }
divisor = divisor + 1;
}

jeremy.king
Aug 26, 2008, 03:49 PM
I have another error now that I fixed the last one. It says, "This method must return a result of type boolean." Also, I tried doing the prime == true, but I got another error about that.

What happens when x < 2? ;)

Cromulent
Aug 26, 2008, 03:53 PM
I have another error now that I fixed the last one. It says, "This method must return a result of type boolean." Also, I tried doing the prime == true, but I got another error about that.


static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x)
if ((x% divisor) == 0) {
return true;}
else {
return false; }
divisor = divisor + 1;
}


First off that while statement makes no sense in its current form, as soon as you hit the first if or else depending on what it evaluates too you'll just return true or false and drop out of the method.

Is this a homework thing by the way?

Mac Player
Aug 26, 2008, 03:56 PM
// removed

lee1210
Aug 26, 2008, 03:58 PM
Edit: To MacPlayer: Please remove your code. There is nothing wrong with this, but the OP is doing this for a class.

I am somewhat concerned that this is your 4th or 5th project, and your instructor hasn't showed you the syntax for if-then-else. In one of the previous threads I sent a link to the Sun document on how to use the if-then-else construct in Java. Here it is again:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

You should read it again, and/or any materials your instructor has provided on this.
if(<expression that evaluates to boolean>) {
<statements to execute if conditional is true>
<...>
<...>
} else {
<statements to execute if conditional is false>
<...>
}

Depending on the situation, some of that is optional. if you only need to execute one statement in the case that a condition is true instead of a whole block, you can do so:
if(<condtional>) <statement if true>

If there is nothing to do if the conditional is false, but you need a block of statements executed if it is true:
if(<conditional>) {
<statements if true>
<...>
}

This is a basic construct of the language, and you need to understand its structure before you continue.

-Lee

iShater
Aug 26, 2008, 03:59 PM
Is this a homework thing by the way?

What? you haven't seen the other threads yet? ;)

Cromulent
Aug 26, 2008, 04:13 PM
What? you haven't seen the other threads yet? ;)

Selective thread reading skills :).

soccersquirt82
Aug 26, 2008, 04:29 PM
Two errors: "Syntax error on token "=", delete this token" and "The left-hand side of an assignment must be a variable." I thought I needed the == to set it as an equality and not an assignment. I also don't know how or why to make the "x% divisor" a variable.


{
static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x){
(x% divisor) = = 0;}
return true;

divisor = divisor + 1;
return prime;
}

iShater
Aug 26, 2008, 04:44 PM
Two errors: "Syntax error on token "=", delete this token" and "The left-hand side of an assignment must be a variable." I thought I needed the == to set it as an equality and not an assignment. I also don't know how or why to make the "x% divisor" a variable.


{
static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while (divisor < x){
(x% divisor) = = 0;}
return true;

divisor = divisor + 1;
return prime;
}


I see an extra space in your "= =", it should be "=="

soccersquirt82
Aug 26, 2008, 04:50 PM
Hmmm. . . now it says, "Syntax error on token "==", invalid AssignmentOperator." The thing about the variable is still there.

iShater
Aug 26, 2008, 04:59 PM
Hmmm. . . now it says, "Syntax error on token "==", invalid AssignmentOperator." The thing about the variable is still there.

Well, you are doing a comparison but then doing nothing with it.

you are checking if the remainder is zero, but then what? what are you doing with that result?

I think the } you have at the end of that line is a problem. Are you trying to end the loop when the remainder is zero?

lee1210
Aug 26, 2008, 05:06 PM
Hmmm. . . now it says, "Syntax error on token "==", invalid AssignmentOperator." The thing about the variable is still there.

You need to rearrange some things.

First off, your while loop now is:
while (divisor < x){
(x% divisor) = = 0;
}

The second line, whether you have = = or == is not syntactically correct.

You also are not modifying divisor or x inside the while loop, so your while condition will always be true if x is greater than 2 when you enter the function.

You need to use an if construct here, and (x % divisor) == 0 should be the conditional. In the "then" part of the if construct, you should return something. But what does it mean if there is no remainder? It means that x is evenly divisible by divisor. So is x prime? If so, then you should return true. if not you should return false.

You also need to pull the increment of divisor into your while loop, so it needs to be before the } that matches the opening { of the while loop.

If the loop exits and has not returned, what does that mean about x? Is it prime? if so, return true, if not return false.

While this project may not require it, you should also ensure that x is positive. Otherwise, divisor will never be less than x and you will loop forever.

-Lee

iShater
Aug 26, 2008, 05:11 PM
It would be ironic if all of these questions are part of a test the OP is taking. :p

kalimba
Aug 26, 2008, 08:19 PM
Not trying to be a douche or anything, but I think you need to step away from your computer and first ask yourself these questions:

1) What is the problem I'm trying to solve?
2) Do I understand how the problem can be solved in a series of logical steps?
3) What constructs of the programming language might I use to implement the logical steps?

After you've answered these questions, THEN sit back down and start coding.

Pardon my being so blunt, but it just doesn't sound like you've put much thought into what you're supposed to be learning.

soccersquirt82
Aug 26, 2008, 08:30 PM
OK. I tried a whole new different thing. Tell me if this is close. Two errors: "Syntax error on token "<", invalid AssignmentOperator" and "Syntax error on token "int", invalid ArgumentList. I tried to put the divisor = divisor + 1 down in the main, and to do that I needed to call the divisor variable from the prime method. I also tried to add that x must be positive.


package prime;
import java.util.Scanner;
public class Prime
{
static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while ((x% divisor) == 0){
divisor < x;}
return false;
}

public static void main(String [] args) {

Scanner sc = new Scanner (System.in);

int x;
divisor = divisor(prime(int));

//user input
System.out.print("x = ");
x = sc.nextInt();

if (x < 0){
System.out.print("Number cannot be negative.");
}

divisor = divisor + 1;

if (prime) { System.out.print("You have found a prime number!");
}else{ System.out.print("I'm sorry. The number you have chosen is not prime.");
}
}
}

soccersquirt82
Aug 26, 2008, 08:37 PM
1) What is the problem I'm trying to solve?
2) Do I understand how the problem can be solved in a series of logical steps?
3) What constructs of the programming language might I use to implement the logical steps?


1. User inputs an x integer. I say if the integer is prime or not.
2. If the integer can be divided by any integer above two and come out evenly (no remainder), then it isn't prime.
3. A while statement dividing x by every integer above 2 and below x. If all of those have remainders, then it is prime. Boolean and integers.

lee1210
Aug 26, 2008, 09:00 PM
OK. I tried a whole new different thing. Tell me if this is close. Two errors: "Syntax error on token "<", invalid AssignmentOperator" and "Syntax error on token "int", invalid ArgumentList. I tried to put the divisor = divisor + 1 down in the main, and to do that I needed to call the divisor variable from the prime method. I also tried to add that x must be positive.


package prime;
import java.util.Scanner;
public class Prime
{
static boolean prime(int x){

int divisor = 2; //initialize loop variables
boolean prime = true;

//loop condition
while ((x% divisor) == 0){
divisor < x;}
return false;
}

public static void main(String [] args) {

Scanner sc = new Scanner (System.in);

int x;
divisor = divisor(prime(int));

//user input
System.out.print("x = ");
x = sc.nextInt();

if (x < 0){
System.out.print("Number cannot be negative.");
}

divisor = divisor + 1;

if (prime) { System.out.print("You have found a prime number!");
}else{ System.out.print("I'm sorry. The number you have chosen is not prime.");
}
}
}


OK, so syntax issues and control structure issues...
You check if x is less than 0. If it is, you print a message. Then you continue running your program. You should either return inside the block, or have an else with the rest of your code in it.

The line:
divisor = divisor(prime(int));

Is not syntactically correct or needed. Take it out.

I don't think you know how functions work, so I will try to explain briefly. The only way that a function interacts with the calling code is through it's parameter list and what it returns. Variables in the function you call prime have no bearing on prime and vice versa. What happens in the function is completely independent of what's in your main function. A function can be called as many times as you need. The compiler already knows the type of your functions, so you don't need to mention them in your main function at all until you want to use them. You do so by referencing their name, then an open parenthesis, then the variables you are passing as arguments separated by commas, then a close parenthesis. If the function returns a value, you can put a variable and an assignment operator, =, before the call. In the case of returning a boolean, you could just use the function directly in an if as it looks like you were trying to do. For prime, this would look like:
if(prime(x)) {
//Print that x is prime
} else {
//Print that x is not prime
}

or

boolean isPrime = prime(x);

Remove
divisor = divisor+1;
from main and move it into your prime function.

To call your prime function (we've been over this before) you need to pass it x. To do this, you would write:
prime(x)

You already have prime in a conditional, so the boolean returned from prime will be used how you expect.

That will get your main function right.

Your prime function is a bit off. You had the right loop control before with:
while(divisor < x)

Inside prime you need to check the condition:
(x % divisor) == 0

In an if. if that condition is true, you know something about x and you can return the proper value.

In your while loop you will also have to increment divisor so the behavior of the loop changes with each iteration. That means BEFORE the } ending the while loop you need a statement that increments divisor.

If the while loop finishes, and has not returned (so x % divisor never equalled 0) you know something about x, and you need to return appropriately.

-Lee

soccersquirt82
Aug 26, 2008, 09:27 PM
I got it! Thanks!

lee1210
Aug 26, 2008, 09:37 PM
I got it! Thanks!

Now to finish the alphabetizing project. =)

-Lee

iShater
Aug 26, 2008, 10:27 PM
Now to finish the alphabetizing project. =)

-Lee

You have the patience of a teacher. ;) :D

lee1210
Aug 26, 2008, 10:41 PM
You have the patience of a teacher. ;) :D

I feel like it's sort of like karma. If we help those that need it, and make them better programmers. The more programming knowledge we put out there, the greater the total programming knowledge of the world. I feel like this will somehow mean that in the future I will be working with better programmers, and won't have to do so much myself =).

-Lee

Cromulent
Aug 27, 2008, 02:41 AM
I feel like it's sort of like karma. If we help those that need it, and make them better programmers. The more programming knowledge we put out there, the greater the total programming knowledge of the world. I feel like this will somehow mean that in the future I will be working with better programmers, and won't have to do so much myself =).

-Lee

You should consider teaching, I know there are a lot of people out there who are prepared to pay for decent quality patient teachers. Get yourself a webcam and some screen sharing setup and you'll be laughing.

That is assuming you are not already a teacher in your spare time :p.

iShater
Aug 27, 2008, 09:30 AM
You should consider teaching, I know there are a lot of people out there who are prepared to pay for decent quality patient teachers. Get yourself a webcam and some screen sharing setup and you'll be laughing.

That is assuming you are not already a teacher in your spare time :p.

+1 that!

Even one in community college! I have taken a couple of classes there in the past as a refresher, and you have a 50/50 chance of getting a good teacher, so I am sure they need them. And extra $$ won't hurt :p

crees!
Aug 27, 2008, 10:06 AM
Not trying to be a douche or anything, but I think you need to step away from your computer and first ask yourself these questions:

1) What is the problem I'm trying to solve?
2) Do I understand how the problem can be solved in a series of logical steps?
3) What constructs of the programming language might I use to implement the logical steps?

After you've answered these questions, THEN sit back down and start coding.

Pardon my being so blunt, but it just doesn't sound like you've put much thought into what you're supposed to be learning.

Where's Reading Rainbow - Programmers Edition when you need it.