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???
Code java:
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!");
}
}
}
}
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.
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?
Re: My code will not run like I want it to. What is wrong???
Quote:
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.
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:
Code java:
System.out.print("How old are you?");
int a=k.nextInt();
if (a<16) {
// ....
add a single line to your code where I indicate:
Code java:
System.out.print("How old are you?");
int a=k.nextInt();
k.nextLine(); // **** add this to handle the EOL token
if (a<16) {
// ....
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."
Re: My code will not run like I want it to. What is wrong???
Quote:
Originally Posted by
koolestkid20
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?
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.
Re: My code will not run like I want it to. What is wrong???