Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By AlexGraal

Thread: If statement help

  1. #1
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    50
    Rep Power
    0

    Question If statement help

    Hello! My name is Nitrodev and i have been familiar to java for just a week and i already have a problem i can't solve without any help. I decided to make a small game so nothing major just a text based one but to my problem: I can't seem to make this code
    Java Code:
    import java.util.Scanner;
    
    public class Character_Creation {
    	
    	public static void main(String[] args) {
    		
    		Scanner firstname = new Scanner(System.in);
    		
    	    System.out.println("So your name is " + firstname.nextLine());
    	    System.out.println("Now enter your age!");
    	    
    	    Scanner age1 = new Scanner(System.in);
    	    int age = age1.nextInt();
    	    
    	    if (age < 18);
    	    	System.out.println("Too Young!");
    	    	
    	    if (age >= 18);
    	    	System.out.println("Welcome!");
    	    
    
    	}
    }
    to work i i want to, here is the result:

    Nitrodev (user input)
    So your name is Nitrodev
    Now enter your age!
    18
    Too Young!
    Welcome!


    Any help is appreciated

    ps. sorry for spelling errors i'm Finn so my english isn't the best.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: If statement help

    Do you get any messages when you compile the program?
    You should use the javac -Xlint option to get compiler warnings.
    For example use this command to compile:
    javac.exe -cp . -Xlint Character_Creation.java
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    50
    Rep Power
    0

    Default Re: If statement help

    I really don't know how to compile it with Eclipse

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: If statement help

    You need to look at the settings for eclipse so that it gives you all the warnings like the javac compiler does with the -Xlint option. I don't use eclipse and can't help you.

    I just found this:
    go to Preferences > Java > Compiler > Errors/Warnings
    and turn off all the Ignore settings so you get all the warnings.
    Last edited by Norm; 02-08-2014 at 08:28 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    136
    Rep Power
    0

    Default Re: If statement help

    Here is your code:

    Java Code:
    import java.util.Scanner;
     
    public class Character_Creation {
         
        public static void main(String[] args) {
             
            Scanner firstname = new Scanner(System.in);
             
            System.out.println("So your name is " + firstname.nextLine());
            System.out.println("Now enter your age!");
             
            Scanner age1 = new Scanner(System.in);
            int age = age1.nextInt();
             
            if (age < 18);
                System.out.println("Too Young!");
                 
            if (age >= 18);
                System.out.println("Welcome!");
             
     
        }
    }
    There are two major things I would change - which may or may not solve your problem.

    I'll explain them, then insert them into your code with annotations.

    first of all, you should set up your scanners differently. You only need one scanner.
    First you need to create the scanner:
    Java Code:
    Scanner scan = new Scanner ( System.in );
    Then, if you want someone's name, you use nextLine() to take in a string, or if you want a number, use nextInt() or nextDouble().

    You should take the number/string in, and then regurgitate it - not all at once.

    AKA, you'd want:
    Java Code:
    Scanner scan = new Scanner ( System.in );
    System.out.println("Your question here");
    String name = scan.nextLine();
    //do something with the person's name
    
    System.out.println("Your other question here");
    int age = scan.nextInt();
    //do something with the person's age
    Second, you only want to let people in of a certain age, so you need an else in there.

    Annotated code:
    Java Code:
    import java.util.Scanner;
     
    public class Character_Creation {
         
        public static void main(String[] args) {
            Scanner scan = new Scanner( System.in ); //creating the scanner object
    
            System.out.println("Enter your name:"); //ask a question
            String name = scan.nextLine();   //get a String response and store it in a String called "name"
            System.out.println("So your name is " + name + "!");
    
            System.out.println("What is your age?"); //Ask another question
            int age = scan.nextInt();
    
            //now we need to check something
            if (age < 18) {                   //if statements DO NOT have semicolons at the end.
                System.out.println("Too Young!");
            } else {  //if the previous check returns false, this will be called.
                System.out.println("Welcome!");
            }
        }
    }
    NitroDev likes this.

  6. #6
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    50
    Rep Power
    0

    Default Re: If statement help

    I'm very thankful for your answer AlexGraal because that solved my problem.
    Now is there a way to run the whole project in Eclipse?
    Last edited by NitroDev; 02-08-2014 at 10:21 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: If statement help

    Having gotten the code without understanding what the problem was, will you be able to solve this kind of problem next time it happens?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    50
    Rep Power
    0

    Default Re: If statement help

    Quote Originally Posted by Norm View Post
    Having gotten the code without understanding what the problem was, will you be able to solve this kind of problem next time it happens?
    Yes i will be

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: If statement help

    Can you tell us what was wrong with your code then? Not to be a douche, but it is slightly glaring. It is also an incredibly important aspect of all of your future coding, so without this fundamental understanding you will struggle through your learning experience.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    136
    Rep Power
    0

    Default Re: If statement help

    Err he was asking for some minute stuff so I thought it'd be fine to give him the whole code. I annotated what was going on and everything - I don't see the big deal. If he wants to learn, looking at that example is a good way to figure out what happened. He won't be able to do any of the other stuff without figuring out what it is that I did.

    Nitro - yes there is. You really need to figure out how to use Eclipse for yourself. I'm sure there is a tutorial out there somewhere. Just google it. Also, you typically won't get a whole code on here - I only did it because it's so simple, it's easier just to show you and explain what's going on. You need to figure out what I did in the annotated version and why before you can do the rest of your project.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: If statement help

    Since this has gone past the original problem, here is what I see using the javac -Xlint option to compile the code:
    Java Code:
    Running: D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Character_Creation.java
    
    Character_Creation.java:16: warning: [empty] empty statement after if
            if (age < 18);
                         ^
    Character_Creation.java:19: warning: [empty] empty statement after if
            if (age >= 18); 
                          ^
    2 warnings
    
    5 error(s)
    Your IDE should give you warnings about the misplaced ; that causes the empty statement.
    The OP needs to know this so the next time he won't need to come here to solve the problem.

    In general you should always use {}s with if statements and loops. They will prevent bugs when the second line of code is added and indented to look like its in the block but isn't.
    Java Code:
      if (some > 2)
           do this
           and do that
    "and do that" is not part of the if statement. It is the statement that follows the if statement.
    Last edited by Norm; 02-09-2014 at 01:13 AM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    50
    Rep Power
    0

    Default Re: If statement help

    Quote Originally Posted by Dark View Post
    Can you tell us what was wrong with your code then? Not to be a douche, but it is slightly glaring. It is also an incredibly important aspect of all of your future coding, so without this fundamental understanding you will struggle through your learning experience.
    Well what is noticed is that i had the ";" after if statement so i think that was 1 of the problems and second one was that i could not do this:

    Java Code:
    if(x = 4)
    do this
    and do this // <--- not going to work

Similar Threads

  1. Statement and prepared statement
    By suhaas.mohandos@gmail.com in forum JDBC
    Replies: 10
    Last Post: 01-30-2014, 01:44 PM
  2. Replies: 3
    Last Post: 01-30-2014, 08:21 AM
  3. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  4. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •