user2808951
user2808951

Reputation: 25

Else without if

I'm trying to write a code for my computer programming class for a project due Monday, and I'm pretty new to Java, but I'm trying to write a program that will first determine if a number the user inputs is even or odd and then determine if the number is prime or not. I'm not sure if I did the algorithm right or not, so if anyone has any corrections on the program to my algorithm or anything else please say so, but my real issue is that the program is refusing to compile. Every time I try, it says it's having an else without if problem. Here's a link to my command box:

http://s1341.photobucket.com/user/Emi_Nightshade/media/Capture_zps45f9a2ea.png.html

enter image description here Here's my code:

import java.io.*;
import java.util.*;

public class Lesson9p1_ThuotteEmily
{
    public static void main(String args[])
    {
        Scanner kbReader0=new Scanner(System.in);
        System.out.print("\n\nPlease enter an integer. An integer is whole number, and it can be either negative or positive. Please enter your number: ");
        long num=kbReader0.nextLong();

        if(num%2==0)                                     //if and else with braces
        {
           System.out.println("Your integer " + num + " is even.");
        }
        else
        {
            System.out.println("Your integer " + num + " is odd.");
        }

        Scanner kbReader1=new Scanner(System.in);
        System.out.print("\n\nWould you like to know if your number is prime? Please enter yes or no: ");
        String yn=kbReader1.nextLine();

        if(yn.equals.IgnoreCase("Yes"))
        {
            System.out.println("Okay. Give me a moment.");

            {
                if(num%2==0)
                {
                    System.out.println("Your number isn't prime.");
                }
                else if(num==2)
                {
                    System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
                }
                for(int i=3;i*i<=n;i+=2)
                {
                    if(n%1==0)
                    {
                        System.out.println("Your number isn't prime.");
                    }
                }
                else
                {
                    System.out.println("Your number is prime!");
                }
            }
        }
        if(yn.equals.IgnoreCase("No"))
        {
            System.out.println("Okay.");
        }
    }
}

If anyone could help me out with this and also any problems I may have made elsewhere in the program, I'd be very grateful! Thanks.

Upvotes: 1

Views: 37082

Answers (5)

live-love
live-love

Reputation: 52534

This doesn't work in Java (2 elses without if)

    if (1 == 1){

    }
    else
    {

    }
    else //syntax error here - else without if
    {

    }

This works:

                if (1 == 1){

                }
                else
                {
                    if (2 == 2){

                    }
                    else //this works
                    {

                    }
                }

Upvotes: 0

you have an else statement after a for loop

for(int i=3;i*i<=n;i+=2)
{
    if(n%1==0)
    {
        System.out.println("Your number isn't prime.");
    }
}
else
{
    System.out.println("Your number is prime!");
}

you probably have to make a Boolean variable to do this one. there are various ways to do it, but here's one that I would probably use

boolean isPrime = true;
for(int i=3;i*i<=n;i+=2)
{
    if(n%1==0)
    {
        isPrime = false;
    }
}
if(isPrime)
{
    System.out.println("Your number is prime!");
}
else
{
    System.out.println("Your number isn't prime.");
}

Upvotes: 7

FoggyDew
FoggyDew

Reputation: 211

import java.io.*;
import java.util.*;

public class Lesson9p1_ThuotteEmily {
    public static void main(String args[]) {
        Scanner kbReader0 = new Scanner(System.in);
        System.out.print("\n\nPlease enter an integer. An integer is whole number, and it can be either negative or positive. Please enter your number: ");
        long num = kbReader0.nextLong();

        if (num % 2 == 0) // if and else with braces
        {
            System.out.println("Your integer " + num + " is even.");
        } else {
            System.out.println("Your integer " + num + " is odd.");
        }

        Scanner kbReader1 = new Scanner(System.in);
        System.out.print("\n\nWould you like to know if your number is prime? Please enter yes or no: ");
        String yn = kbReader1.nextLine();

        if (yn.equalsIgnoreCase("Yes")) {
            System.out.println("Okay. Give me a moment.");

            {
                if (num % 2 == 0) {
                    System.out.println("Your number isn't prime.");
                } else if (num == 2) {
                    System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
                } else
                    for (int i = 3; i * i <= num; i += 2) {
                        if (num % i == 0) {
                            System.out.println("Your number isn't prime.");
                        } else {
                            System.out.println("Your number is prime!");
                        }
                    }
            }
        }
        if (yn.equalsIgnoreCase("No")) {
            System.out.println("Okay.");
        }
    }
}

all corrections included.

Upvotes: -1

libik
libik

Reputation: 23049

Here is the problem

            else
            {
                System.out.println("Your number is prime!");
            }

It is after for cycle, not the if statement, you have to switch it. :

           if(num%2==0)
            {
                System.out.println("Your number isn't prime.");
            }
            else if(num==2)
            {
                System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
            }
            else
            {
                System.out.println("Your number is prime!");
            }
            for(int i=3;i*i<=n;i+=2)
            {
                if(n%1==0)
                {
                    System.out.println("Your number isn't prime.");
                }
            }

Which is functional but not right alghoritm, you probably want this :

           if(num%2==0)
            {
                System.out.println("Your number isn't prime.");
            }
            else if(num==2)
            {
                System.out.println("Your number is 2, which is the only even prime number in existence. Cool, right?");
            }

            boolean isPrime = true;
            for(int i=3;i*i<=n;i+=2)
            {
                if(n%i==0)
                {
                    System.out.println("Your number isn't prime.");
                    isPrime = false;
                }
            }
            if (isPrime){
                System.out.println("Your number is prime!");
            }   

Upvotes: 3

Don Branson
Don Branson

Reputation: 13707

The block

else
{
    System.out.println("Your number is prime!");
}

has no corresponding 'if' before it.

Upvotes: 0

Related Questions