Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree1Likes

Thread: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

  1. #1
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Question 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. )


    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.. 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!

    Java 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.");
    			
    					
    		
    
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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.
    Last edited by Tolls; 02-28-2012 at 05:39 PM.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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.

  4. #4
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by Tolls View Post
    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! ;)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by spande View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by JosAH View Post
    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!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Stick it right after your nextInt() and nextDouble() calls.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    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.

  9. #9
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by Tolls View Post
    Stick it right after your nextInt() and nextDouble() calls.

    Any chance you could show me, in my
    Java Code:
     readLine()
    If it won' be taking too much of your time, ofcourse!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by Tolls View Post
    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()
    Last edited by spande; 02-28-2012 at 06:46 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Can you post your code with the nextLine() call

  14. #14
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by spande View Post

    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

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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???

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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...:)
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by Tolls View Post
    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()...'


    Java Code:
    int stoppet; 
    		stoppet = tastatur.nextInt(); tastatur.nextLine();
    And..

    Java 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..'


    Again guys, thanks for actually helping me! :)

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    What happened when you compiled and executed the program?

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    spande is offline Member
    Join Date
    Feb 2012
    Location
    Vordingborg, Denmark
    Posts
    14
    Rep Power
    0

    Default Re: What's wrong with my code? (Danish java-programmer would be greatly appriciated!)

    Quote Originally Posted by Norm View Post
    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)

Page 1 of 2 12 LastLast

Similar Threads

  1. What is wrong in this code of java
    By kank635 in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 10:41 PM
  2. Replies: 5
    Last Post: 12-08-2011, 08:54 PM
  3. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 10:30 PM
  4. Please look at my java code and tell me what's wrong
    By xcointoss in forum Advanced Java
    Replies: 3
    Last Post: 10-22-2010, 05:15 AM
  5. what wrong with my java code
    By MAXZZXAM in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 10:38 AM

Posting Permissions

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