Delete "else" token???

Discussion in 'Mac Programming' started by soccersquirt82, Aug 26, 2008.

1. soccersquirt82 macrumors 6502

Joined:
Mar 11, 2008
#1
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; }
}
```

2. davedelong macrumors member

Joined:
Sep 9, 2007
Location:
Right here.
#2
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?

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;
}
}```

3. Cromulent macrumors 603

Joined:
Oct 2, 2006
Location:
The Land of Hope and Glory
#3
You need to have if / else statements one after the other like so:

Code:
```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.

4. soccersquirt82 thread starter macrumors 6502

Joined:
Mar 11, 2008
#4
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.

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 true;}
else {
return false; }
divisor = divisor + 1;
}
```

5. jeremy.king macrumors 603

Joined:
Jul 23, 2002
Location:
Fuquay Varina, NC
#5
What happens when x < 2?

6. Cromulent macrumors 603

Joined:
Oct 2, 2006
Location:
The Land of Hope and Glory
#6
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?

Joined:
Jan 19, 2006
8. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#8
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.
Code:
```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:
Code:
`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:
Code:
```if(<conditional>) {
<statements if true>
<...>
}```
This is a basic construct of the language, and you need to understand its structure before you continue.

-Lee

9. iShater macrumors 604

Joined:
Aug 13, 2002
Location:
Chicagoland
#9
What? you haven't seen the other threads yet?

10. Cromulent macrumors 603

Joined:
Oct 2, 2006
Location:
The Land of Hope and Glory
#10

11. soccersquirt82 thread starter macrumors 6502

Joined:
Mar 11, 2008
#11
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.

Code:
```{
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;
}
```

12. iShater macrumors 604

Joined:
Aug 13, 2002
Location:
Chicagoland
#12
I see an extra space in your "= =", it should be "=="

13. soccersquirt82 thread starter macrumors 6502

Joined:
Mar 11, 2008
#13
Hmmm. . . now it says, "Syntax error on token "==", invalid AssignmentOperator." The thing about the variable is still there.

14. iShater macrumors 604

Joined:
Aug 13, 2002
Location:
Chicagoland
#14
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?

15. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#15
You need to rearrange some things.

First off, your while loop now is:
Code:
```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

16. iShater macrumors 604

Joined:
Aug 13, 2002
Location:
Chicagoland
#16
It would be ironic if all of these questions are part of a test the OP is taking.

17. kalimba macrumors regular

Joined:
Jun 10, 2008
#17
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.

18. soccersquirt82 thread starter macrumors 6502

Joined:
Mar 11, 2008
#18
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.

Code:
```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.");
}
}
}
```

19. soccersquirt82 thread starter macrumors 6502

Joined:
Mar 11, 2008
#19
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.

20. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#20
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:
Code:
`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:
Code:
```if(prime(x)) {
//Print that x is prime
} else {
//Print that x is not prime
}```
or

Code:
`boolean isPrime = prime(x);`
Remove
Code:
`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:
Code:
`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:
Code:
`while(divisor < x)`
Inside prime you need to check the condition:
Code:
`(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

Joined:
Mar 11, 2008
22. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#22
Now to finish the alphabetizing project. =)

-Lee

23. iShater macrumors 604

Joined:
Aug 13, 2002
Location:
Chicagoland
#23
You have the patience of a teacher.

24. lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#24
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

25. Cromulent macrumors 603

Joined:
Oct 2, 2006
Location:
The Land of Hope and Glory
#25
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 .