Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: If then

  1. #1
    Junior Member
    Join Date
    May 2013
    Thanked 0 Times in 0 Posts

    Smile If then

    Hi there all, sorry but I'm completely new to Java. I started learning some Java on my own but I hit a little roadbump with if then statements. I am trying to be able to type in a number so that I can come out with one of the statements listed below (yea I know, they're funny but I'm just trying them out), but Java gives me a "operator is undefined" error.

    //public static void main(String args[]){
    		Scanner x = new Scanner(System.in);
    		double ans;
    		System.out.println("Type in a random number from 1-10.");
    		ans = x.nextDouble();
    		if (x > 5){
    			System.out.println("You are a boy.");
    		}else{System.out.println("You are a girl.");

    The only problem according to eclipse is coming from the "if" line. "The operator > is undefined for the argument type(s) Scanner, int." What am I doing wrong?

  2. #2
    Join Date
    Apr 2012
    Thanked 27 Times in 27 Posts

    Default Re: If then

    Your variable x is a Scanner object. I think what you're looking to do is compare the "ans" variable instead.

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    Vol Brisingr (May 15th, 2013)

Tags for this Thread