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 9 of 9

Thread: My code will not run like I want it to. What is wrong???

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

    Default My code will not run like I want it to. What is wrong???

    When I run my code and say that I am 17 years old it asks if I have my license but then it wont let me type???

     
     
    import java.util.*;
     
    public class test {
     
     
    	public static void main(String[] args) {
     
     
    		Scanner k=new Scanner(System.in);
     
     
    		System.out.print("How old are you?");
    		int a=k.nextInt();
     
    		if (a<16) {
    			System.out.print("It's too bad you can't drive.");
    		}
     
    		if (a==16) {
     
    				System.out.print("Do you have your permit yet?");
    				String p=k.nextLine();
     
     
     
    				 if (p.equals("yes")) {
     
    					 System.out.print("Cool!");
    						String c=k.nextLine();
     
    				 }
    				 }
     
    				if (a>16) {
     
    					System.out.print("So.....you have your liscence right....right?");
    					String d=k.nextLine();
     
     
    					if (d.equals("no")) {
    						System.out.print("Now, that is just embarassing!");
     
    					}
    				}
     
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    Please explain what the problem is. Post the program's output and add some comments showing what you want the output to look like.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    I did what you said. Now can you tell me why it wont let me type my answer?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    I did what you said. Now can you tell me why it wont let me type my answer?
    Can you explain what you did and what the "it" is that won't let you type your answer?

    Can you copy the contents of the command prompt window from when you execute the program to show what you are talking about?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    I need to blog this issue.

    The problem is with your use of the Scanner object. Scanners read in tokens, and there is a special token, the end of line (EOL) token that can't be ignored. If you call nextLine() on your Scanner, you'll swallow the remaining tokens on that line up to and including the EOL token, but if you call nextInt() and most all other similar methods, you won't handle EOL until nextLine() is called. So when you type 17<return> k.nextInt() will handle the 17 OK, but the EOL token will be left dangling. The next time you call k.nextLine() it gobbles the EOL token but ignores anything else you've typed in after it. The solution is to call k.nextLine() immediately after calling k.nextInt() so that you explicitly handle the EOL token. So instead of this:

    System.out.print("How old are you?");
    int a=k.nextInt();
     
    if (a<16) {
      // ....

    add a single line to your code where I indicate:

    System.out.print("How old are you?");
    int a=k.nextInt();
    k.nextLine();  // **** add this to handle the EOL token
     
    if (a<16) {
      // ....
    Last edited by Norm; November 24th, 2012 at 06:31 PM. Reason: nextInt() changed to nextLine()

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    How old are you? 17
    So.....you have your liscence right....right?

    "Then I try to type and I can't."

    --- Update ---

    How old are you? 17
    So.....you have your liscence right....right?

    "Then I try to type and I can't."

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    Quote Originally Posted by koolestkid20 View Post
    How old are you? 17
    So.....you have your liscence right....right?

    "Then I try to type and I can't."

    --- Update ---

    How old are you? 17
    So.....you have your liscence right....right?

    "Then I try to type and I can't."
    Please clarify this post as it makes absolutely no sense to me. Who are you addressing this to, and what are you trying to say? How does it relate to my post above?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    The OP was posting the contents of the command prompt window that I asked for.
    He left off the header/description:
    Here is the contents of the command prompt window when I run the program.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My code will not run like I want it to. What is wrong???

    Ah thanks .... d'oh!!!

Similar Threads

  1. How to run a C++ code using java?
    By progfool in forum Java Native Interface
    Replies: 5
    Last Post: August 2nd, 2013, 06:46 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  4. Replies: 6
    Last Post: July 21st, 2011, 07:45 AM
  5. How to run this code?
    By jackdear44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 11:37 PM