0

I am writing to find out whether the user is Teen ager or not? I am getting error while executing the program.

public class TeenNumberChecker
{
    public static void main (String[] args)
    {
        System.out.println(hasteen(22,28,32));  
    }



public static boolean  hasteen(int age1,int age2,int age3) {
        if ((age1>=13&&age1<=19)||(age2>=13&&age2<=19)||(age2>=13&&age2<=19))
            return true;
}
else {
           return false;
}
}

Error i am getting is "Exception in thread "main" java.lang.Error: Unresolved compilation problem: "

5
  • 2
    You have a typo - you missing a { after if ((age1>=13&&age1<=19)||(age2>=13&&age2<=19)||(age2>=13&&age2<=19)) Commented Feb 16, 2020 at 9:45
  • no error message to help find out what is wrong? at least the indentation, or missing of it, is not helping at all Commented Feb 16, 2020 at 9:46
  • Syntax error near else part Commented Feb 16, 2020 at 9:50
  • Void method cannot have return values Commented Feb 16, 2020 at 9:51
  • 1
    Nice idea Paul Rooney Commented Feb 16, 2020 at 10:10

2 Answers 2

1

Your code does not compile.

  1. There are brackets missing and
  2. you repeated the comparison of age2 where you possibly wanted to use age3 instead.

You should quickly learn to indent your source code properly to assist your own eyes in seeing errors. The indentation style is surely a matter of taste, so find out yourself what works best for you or follow my example below.

class Main
{
    public static void main(String[] args)
    {
        System.out.println(hasteen(22, 28, 32));
    }

    public static boolean hasteen(int age1, int age2, int age3)
    {
        if (
                (age1 >= 13 && age1 <= 19) ||
                (age2 >= 13 && age2 <= 19) ||
                (age3 >= 13 && age3 <= 19)
           )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Using an IDE (like IntelliJ or NetBeans) which supports automatic code formatting and highlighting of errors would be helpful here.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure how my below solutions work:

public class TeenNumberChecker
{
    public static void main (String[] args)
    {
        System.out.println(hasteen(00,88,32));  
    }



public static boolean hasteen(int age1, int age2, int age3)
{
    if (
            (age1 >= 13 && age1 <= 19) ||
            (age2 >= 13 && age2 <= 19) || 
            (age3 >= 13 && age3 <= 19)
    )            
        return true;

    return false;
}

Can someone explain it?

1 Comment

There is not much to explain. Format it like I showed you, then you will see why it works. If the condition is true then the method returns true. The return false will not be executed because the method returned already. But if the condition is not true, then the return true will not be executed. So the next command is return false. Better use always brackets like I showed you, then the program flow is more obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.