-1

I am very new to java, trying to learn from this book for a class and im stuck making a program. Keeps telling me else without if is an error and that it cant find the symbol labeled as guessdigit1 & 2.

It keeps saying guessdigit1 and 2 are their own class when i hover over the error dialog box next to symbol. any idea? Thank you!

package loterry;

import java.util.Scanner;

public class Loterry {
// This program creates two random numbers, and checks to see if your guess
    // makes the lottery win

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter in your guess for loterry, two digits please");
        int lottery = (int)(Math.random()*100 /50);
        int guess = input.nextInt();
        int lotterydigit1= lottery /10;
        int lotterydigit2= lottery %10;

        // Get digits from guess
        int guessdigit1 = guess / 10;
        int guessdigit2 = guess % 10;

        System.out.println("The lottery number is " + lottery);

        if (guess == lottery)
            System.out.println("Exact Match: you win 10,000");
        else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
            System.out.println("Match all digits: you win 3,000");
        else if (guessdigit1 == lotterydigit1 
                     || guessdigit1 == lotterydigit2
                     || guessdigit2 == lotterydigit2
                     || guessdigit2 == lotterydigit2)
                  System.out.println("match one digit: you win 1,000");
                else
                  System.out.println("sorry no match");




    }
}

6 Answers6

11

here is your problem:

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);

Remove the semicolon (;)

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)

Note: It is recommended to always write your if else statement with bracket { } to avoid any possible error in the future, and of course it was in the Java code conventions. (see this answer for clarification on braces issue)

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
     System.out.println("Match all digits: you win 3,000");
}
Community
  • 1
  • 1
Baby
  • 5,062
  • 3
  • 30
  • 52
3

remove the semicolon (;) to avoid the complier errors

use the curly braces "{ }" block in the if else statement to avoid the confusions

if (guess == lottery)
            System.out.println("Exact Match: you win 10,000");
        else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); //->here
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • To elaborate - with the semicolon, you are basically saying the else if block is done. Your next line is `System.out...` is a new statement, and the next else if `else if (guessdigit1 == lotterydigit1...` is the one the compiler is complaining about. – ansible Jan 23 '14 at 05:57
2

Replace
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);

with

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)

And you should use {} while writing if else. It makes it clean code and clean code is easy to debug.

Aman Arora
  • 1,232
  • 1
  • 10
  • 26
0

Simply remove the semicolon(;) which you have put at the end of your first elseif.

Only statements should have semicolon(;) at its end

Working Code:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter in your guess for loterry, two digits please");
        int lottery = (int)(Math.random()*100 /50);
        int guess = input.nextInt();
        int lotterydigit1= lottery /10;
        int lotterydigit2= lottery %10;

        // Get digits from guess
        int guessdigit1 = guess / 10;
        int guessdigit2 = guess % 10;

        System.out.println("The lottery number is " + lottery);

        if (guess == lottery)
            System.out.println("Exact Match: you win 10,000");
        else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
            System.out.println("Match all digits: you win 3,000");
        else if (guessdigit1 == lotterydigit1 
                     || guessdigit1 == lotterydigit2
                     || guessdigit2 == lotterydigit2
                     || guessdigit2 == lotterydigit2)
                  System.out.println("match one digit: you win 1,000");
                else
                  System.out.println("sorry no match");




    }
Rajaah
  • 51
  • 1
  • 2
  • 11
0

Yeah that semicolon after that else if statement is what is messing you up. But seriously though I would advise you to use brackets in the future... It makes the code look neater and thus makes it easier to keep track of things... Trust me it really helps when working on larger programs or working in a group.

0

Just remome ; after if condition and use {} each statement

See here docs

     if (guess == lottery){
//code here

}

        else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
//code here

}

        else if (guessdigit1 == lotterydigit1 
                     || guessdigit1 == lotterydigit2
                     || guessdigit2 == lotterydigit2
                     || guessdigit2 == lotterydigit2){
//code here

}

                else{
//code here

}
Sitansu
  • 3,225
  • 8
  • 34
  • 61