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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: I am new

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am new

    Hi guys! I'm very new to Java and doing basic java coding. I was wondering if any of you could help me whats wrong with my code. I'm just trying to do an if statement in which IF a the value of my equation is greater than or equal to 18 its good. But the ELSE function isnt working for me. I'm trying to see what mistake I'm making. Thank you.

    import java.util.Scanner;

    class apples{
    public static void main(String args[]){
    Scanner Alex = new Scanner(System.in);
    int fnum, snum, answer;
    System.out.println("Enter your age:");
    fnum = Alex.nextInt();
    System.out.println("Enter age of your girl:");
    snum = Alex.nextInt();
    answer = fnum / 2 + 7;
    if (answer >= 18);{
    System.out.println("Shes good for you");

    }else
    {
    System.out.println("She's not good for you");
    }
    }

    }

  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: I am new

    1. When you get errors, post the exact error text or trace (no paraphrasing) and indicate whether it was a compile time or run time error. Don't expect forum members to read your mind.

    2. Read about the syntax of the if and if-else statements here:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Then check your semicolons.

    db

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I am new

    import java.util.Scanner;
     
    class apples{
    	public static void main(String args[]){
    		Scanner Alex = new Scanner(System.in);
    		int fnum, snum, answer;
    		System.out.println("Enter your age:");
    		fnum = Alex.nextInt();
    		System.out.println("Enter age of your girl:");
    		snum = Alex.nextInt();
    		answer = fnum / 2 + 7;
           // what if frum % 2 !=0?
    // I mean, what if frum is not divisible by 2?
    // 7 /2 + 7 = 3 + 7 = 10
     
     
     
    // also, lose the ; after your if statement.  That is certainly causing an error.  
    		if (answer >= 18);{
    			System.out.println("Shes good for you");
     
    		}else
    		{
    			System.out.println("She's not good for you");
    		}
    	}
     
    }

    Get rid of that ; after the if. That's causing an error.