-
What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Hello,
WARNING, THE FOLLOWING IS WRITTEN BY A NOOBIE JAVA-PROGRAMMER, WHO IS NEW IN THE GAME!
DO NOT PROCEED READING IF YOU'RE ALLERGIC TO UTTER NOOBIENESS.
(Also this is my first post on this forum, and essentially I have no clue what I'm doing. :(clap): )
So, I'm writing this little program for my IT-class. And since I'm new, I don't have full control over the diffrent variables, I'm using in this code. Anyway, I'm having a problem with my code. Is there any chance someone could help me looking through it, and tell me what's wrong with it?
Essentially, I have to write a code that allows me to see how much "the users" alkohol level is (BAC if you will). And then ask the user how many times they have violatet the Danish DUI offence. Then the 'program' should tell me, what the punishment for violating that offence is.
Please help me. That is..:(wait): Ofcourse you have any idea what I'm trying to say!
For an example the code would say (English):
[CONSOLE]: How many times have you been stopped for driving under the influence?
[USER]: 1
[CONSOLE]: What is your alcohol-level?
[USER]: 1.25
(then the console SHOULD say, but doesn't)
[CONSOLE]: You're losing your drivers license if you violate this crime again
You're getting a fine of ??? Danish Crowns(DDK/KR)
You are to take these a course, which you are paying for you're self.
(skip following, if you're not Danish speaking/reading!)
Eksempel af hvad consollen burde spytte ud, når jeg kører min kode:
[KONSOL]: Hvor mange gange er du blevet stoppet?
[BRUGER]: 1
[KONSOL]: Hvad er din promille?
[BRUGER]: 1,25
[KONSOL]: Betinget frakendelse af kørekort
Bøde svarende til netto af din månedsløn
Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time
I'm going to attach my code, and write it here aswell. I'm using Eclipse Classic, if that makes any diffrence.
Don't be too harsh, guys! :(blush):
Code:
public class opg1_promille {
public static void main(String[] args) {
java.util.Scanner tastatur = new java.util.Scanner(System.in); // Tastatur: "keyboard" in Danish
System.out.println("Hvor mange gange er du blevet stoppet?"); //How many times have you been stopped for DUI?
int stoppet = tastatur.nextInt();
System.out.println("Hvad er din promille?"); //what is your alcohol level?
double promille;
promille = tastatur.nextDouble();
// FØRSTE GANG DU ER BLEVET STOPPET / FIRST TIME STOPPED FOR DUI
if (stoppet == 1)
if (promille > 0.00 && promille < 0.51)
System.out.println("Dette er ikke ulovligt.");
if (promille > 0.49 && promille < 1.21)
System.out.println("Betinget frakendelse af kørekort" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.19 && promille < 2.01)
System.out.println("Ubetinget frakendelse i min. 3 år" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.99)
System.out.println("Betinget fængsel i min. 20 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 3)
System.out.println("Seek doctor, IMHO.");
// ANDEN GANG DU ER BlEVET STOPPET / SECOND TIME STOPPED FOR DUI
if (stoppet == 2)
if (promille > 0.00 && promille < 0.51)
System.out.println("Dette er ikke ulovligt.");
if (promille > 0.49 && promille < 1.21)
System.out.println("Betinget fængsel min. 10 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.19 && promille < 2.01)
System.out.println("Betinget fængsel min. 10dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.99)
System.out.println("Betinget fængsel i min. 30 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 3)
System.out.println("Seek doctor, IMHO.");
// TREDJE GANG DU ER BLEVET STOPPET / THIRD TIME STOPPED FOR DUI
if (stoppet == 3)
if (promille > 0.00 && promille < 0.51)
System.out.println("Dette er ikke ulovligt.");
if (promille > 0.49 && promille < 1.21)
System.out.println("Ubetinget fængsel min. 20 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.19 && promille < 2.01)
System.out.println("Ubetinget fængsel min. 20 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 1.99)
System.out.println("Ubetinget fængsel min. 40 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time");
if (promille > 3)
System.out.println("Seek doctor, IMHO.");
// FJERDE GANG DU ER BLEVET STOPPET / FOURTH TIME STOPPED FOR DUI
if (stoppet == 4)
if (promille > 0.00 && promille < 0.51)
System.out.println("Dette er ikke ulovligt.");
if (promille > 0.49 && promille < 1.21)
System.out.println("Ubetinget fængsel min. 40 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time" +
"Du for også frakendt dit kørekort i 10 år. GLHF");
if (promille > 1.19 && promille < 2.01)
System.out.println("Ubetinget fængsel min. 40 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time" +
"Du for også frakendt dit kørekort i 10 år. GLHF");
if (promille > 1.99)
System.out.println("Ubetinget fængsel min. 50 dage" +
"bøde svarende til netto af din månedsløn" +
"Du skal gennemføre kurset Alkohol og Trafikkursus, som du selv skal betale. 2500kr á 4 x 2½ time" +
"Du for også frakendt dit kørekort i 10 år. GLHF");
if (promille > 3)
System.out.println("Seek doctor, IMHO.");
}
}
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
After the call to tastatur.nextInt(), call tastatur.nextLine().
The carriage return is probably stuck in the buffer.
Note, if that does not cure it you might want to stick some println() calls in there so you can see where it's going (eg print out the value of promille).
ETA: You've used code tags and have almost got the layout correct for your code, so you are doing better than almost all other first time posters.
Class names should be leading caps (Opg1Promille), variable names are correct though.
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
One comment on the list of if statements. If they are mutually exclusive, only one in the list should be true at a time and execute, then you should use if/else if statements.
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
Tolls
After the call to tastatur.nextInt(), call tastatur.nextLine().
The carriage return is probably stuck in the buffer.
How would this look like in the code?
Could I please steal some of your time - and make you explain this shortly? Would be a BIG help for me! ;)
Thanks for your reply and warm words! ;)
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
spande
How would this look like in the code?
Could I please steal some of your time - and make you explain this shortly? Would be a BIG help for me! ;)
Thanks for your reply and warm words! ;)
The nextInt() method only reads digit characters; as soon as it finds a non-digit character it stops reading and leaves the offending character in the input buffer. If you press [enter] (and you did and will do so) it generates an [enter] character in the input buffer; the readLine() method reads all characters up to an including this [enter] character, so it's gone afterwards.
kind regards,
Jos
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
JosAH
The nextInt() method only reads digit characters; as soon as it finds a non-digit character it stops reading and leaves the offending character in the input buffer. If you press [enter] (and you did and will do so) it generates an [enter] character in the input buffer; the readLine() method reads all characters up to an including this [enter] character, so it's gone afterwards.
kind regards,
Jos
So, where would it be appropriate to use the readLine() method in my code?
Also thanks for your reply!
BTW, so far I've only learned methods that read digits, shuch as Int, Double, Byte, Short etc - since those are the only ones I've been using so far! :)
So this is great infomation!
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Stick it right after your nextInt() and nextDouble() calls.
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
the readLine() method reads all characters up to an including this [enter] character
This should be the nextLine() method. Read the API doc for the Scanner class to see what methods it has.
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
Tolls
Stick it right after your nextInt() and nextDouble() calls.
Any chance you could show me, in my
If it won' be taking too much of your time, ofcourse! :(sweat):
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
See post#8.
nextLine() is a method in the Scanner class. Read the API doc
Java Platform SE 6
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Are you sure this is your code?
Because if you wrote most of the above and can't see where to put tastatur.nextLine() then I suspect it isn't...
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
Tolls
Are you sure this is your code? I suspect it isn't...
I am 100% sure this is my code. I wrote some of it with my labpartner at class, and finished it when I got home.
Then I realised it wasn't working -> posted a thread here, looking for help so I can finish it before tomorrow to impress my teacher TO GAIN GREAT SUCCES -> now.
I tried placing tastatur.nextLine() elsewhere, and after that I realised I had no idea what exactly I were doing, i went to seek help :)
edit: I ment tastatur.nextInt() (as in my code, line 8, havn't touched anything since post) NOT tastatur.nextLine()
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Can you post your code with the nextLine() call
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
spande
I tried placing tastatur.nextLine() elsewhere, and after that I realised I had no idea what exactly I were doing, i went to seek help :)
edit: I ment tastatur.nextInt() (as in my code, line 8, havn't touched anything since post) NOT tastatur.nextLine()
@Norm
edited post #12
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
We are waiting for you to do what the three of us recommended. What is the problem with making the change we recommend???
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quoting myself:
"After the call to tastatur.nextInt(), call tastatur.nextLine()."
and
"Stick it right after your nextInt() and nextDouble() calls."
I realise it's a second language, but you strike me as pretty fluent...:)
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
Tolls
Quoting myself:
"After the call to tastatur.nextInt(), call tastatur.nextLine()."
and
"Stick it right after your nextInt() and nextDouble() calls."
I'm having a hard time understanding what you mean by 'stick it right after nextDouble()...'
Code:
int stoppet;
stoppet = tastatur.nextInt(); tastatur.nextLine();
And..
Code:
double promille;
promille = tastatur.nextDouble(); tastatur.nextLine();
Like this??? I'm not sure that this is what you mean by 'sticking it in after..' :s:
Again guys, thanks for actually helping me! :)
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
What happened when you compiled and executed the program?
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Pretty much.
(Note you could put it on a new line).
Apologies for the use of slang.
The first sentence is less slangy.
-
Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
Quote:
Originally Posted by
Norm
What happened when you compiled and executed the program?
Hvor mange gange er du blevet stoppet? //How many times have you been stopped for DUI?
1
Hvad er din promille? //what is your alcohol level?
1.25
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at opg1_promille.main(opg1_promille.java:17)