1

I wrote this piece of code but when I try to compile it says:

 error: class, interface, or enum expected
 }
 ^
1 error

How can I fix that? I don't understand. Is something wrong with the parentheses? What does enum mean? I have checked the parenthesis and everything seems in order.

import java.util.Scanner;

public class Horoscope {
  public static void main(String[] args) {

    System.out.println("Please enter month of Birth: ");
    Scanner month = new Scanner(System.in);
    System.out.println("Please enter day of Birth: ");
    Scanner day = new Scanner(System.in);
    System.out.println("Your horoscope is ");

    if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
        System.out.print("Aries");
     }
   if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
        System.out.print("Taurus");
     }
   if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {
        System.out.print("Gemini");
     }
   if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
       System.out.print("Cancer");
     }
  if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
        System.out.print("Leo");
     }
  if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
        System.out.print("Virgo");
     }
  if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {
        System.out.print("Libra");
     }
  if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) {
        System.out.print("Scorpio");
     }
  if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
        System.out.print("Sagittarius");
     }
  if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
        System.out.print("Capricorn");
     }
  if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {
        System.out.print("Aquarius");
     }
  if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {
        System.out.print("Pisces");
    }
}
}
}
}

Thanks

Saadat
  • 163
  • 1
  • 2
  • 12
  • What do you mean "You checked"? There are clearly superfluous braces there. – RealSkeptic Jul 12 '15 at 13:14
  • @RealSkeptic What do you mean by braces? { or ( ? Can you please help? I am really new to this :) – Saadat Jul 12 '15 at 13:15
  • You have two extra `}`. But they just mask several other errors. – Marvin Jul 12 '15 at 13:16
  • 1
    remove two `}` from last part of the code, and i believe you need to read from the user the month and the day, by using `Scanner month = new Scanner(System.in)` you are just tell the compiler to use the in stream only . you have to say `int mnth = month.nextInt()` – Ali Helmy Jul 12 '15 at 13:16
  • 1
    "Braces" in English mean `{}` (full name: "curly braces"). Brackets mean `[]` (full name: square brackets). And parentheses mean `()`. – RealSkeptic Jul 12 '15 at 13:16
  • @Marwin yeah, when I try removing those, everything else fails. What is the problem? I don't seem to understand. I used the correct if statements didn't I? it says I can't compare scanner to int. How should I fix that? – Saadat Jul 12 '15 at 13:17
  • It's not that everything else fails. It's that when you have such a serious error as unbalanced braces, the compiler does not go into the details of the program itself and quits immediately. Once you fix the serious error, it starts complaining about the content of the actual code. – RealSkeptic Jul 12 '15 at 13:18
  • And as for the actual content errors - look at your code. When you compare `month == 3`, what type is `month`? Does that make sense? Look at the example code you got at school. Did you do the correct thing when you declare `month` and `day` like that? – RealSkeptic Jul 12 '15 at 13:22
  • @RealSkeptic Thanks :) Ali Helmy is right. I need to make it next int = month.nextInt() – Saadat Jul 12 '15 at 13:24
  • @AliHelmy Shukran :) – Saadat Jul 12 '15 at 13:24
  • 1
    Welcome bro :) , please accept an answer to close the question . – Ali Helmy Jul 12 '15 at 13:27

2 Answers2

1

By using Scanner month = new Scanner(System.in) you are just tell the compiler to use the in stream only . you have to say int mnth = month.nextInt() Change this part

System.out.println("Please enter month of Birth: ");
Scanner month = new Scanner(System.in);
System.out.println("Please enter day of Birth: ");
Scanner day = new Scanner(System.in);
System.out.println("Your horoscope is ");

with

int month=0 , day=0;
Scanner scanner= new Scanner(System.in);
System.out.println("Please enter month of Birth: ");
month = scanner.nextInt();
System.out.println("Please enter day of Birth: ");
day = scanner.nextInt();
System.out.println("Your horoscope is ");
Ali Helmy
  • 784
  • 6
  • 18
1

Few things you should study before you start to write the code.

This is your correct code.

import java.util.Scanner;

public class Horoscope {

    public static void main(String[] args) {

        System.out.println("Please enter month of Birth: ");
        Scanner sc = new Scanner(System.in);
        int month = sc.nextInt();
        System.out.println("Please enter day of Birth: ");
        int day = sc.nextInt();
        System.out.println("Your horoscope is ");

        if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
            System.out.print("Aries");
        }
        if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
            System.out.print("Taurus");
        }
        if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {
            System.out.print("Gemini");
        }
        if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
            System.out.print("Cancer");
        }
        if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
            System.out.print("Leo");
        }
        if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
            System.out.print("Virgo");
        }
        if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {
            System.out.print("Libra");
        }
        if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) {
            System.out.print("Scorpio");
        }
        if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
            System.out.print("Sagittarius");
        }
        if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
            System.out.print("Capricorn");
        }
        if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {
            System.out.print("Aquarius");
        }
        if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {
            System.out.print("Pisces");
        }
    }
}
Community
  • 1
  • 1
Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
  • but you only put one scanner? – Saadat Jul 12 '15 at 13:26
  • 1
    One `Scanner` is enough for the entire program. `Scanner` defines the input stream. And with `.nextInt()`, you take the next integer input. You can take any number of inputs using a single `Scanner`. Refer the links in the answer. That will help you understand better. – Shrinivas Shukla Jul 12 '15 at 13:29