1

(amateur highs chool Java coder here). Iv'e been working on this project, and Iv'e reached a problem I can't figure out. Here's what I need help with: (I'm trying to write a program that will determine if the driver is speeding, and if so which one of 3 tickets is suitable, and, how much money to deduct from the account) 1. How do I use the reduc variable in the if statement? I get an error when i Include it in the last if statement like this....

//variables
    int sbanka = 400;
    int speed = 60;
    int speedlimit = 60;
    int ticket1 = 100;

    //logic
    //Ticket 1
    if(speed > speedlimit + 10) {
        System.out.println("You will recieve the level 1 ticket");
        boolean reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
        }
            //bank account calculation for ticket 1
            if(reduc == true && sbanka >-100) {
                sbanka -= 100;
                System.out.println("we will deduct 100 dollars from your checking account");

Here is the finished program. Thanks for the help.

package labs;

public class SpeedingTicket {

    public static void main(String[] args) {

    //variables   
    int speed = 50;
    int speedlimit = 60;
    String text = ("Your account balance is now ");
    boolean reduc = false;        

    //tickets
    int t1 = 100;
    int t2 = 150;
    int t3 = 200;

    //Bank account
    int sbanka = (400);


    //logic       
    if(speed > speedlimit + 10) {           
        System.out.println("You are getting a level one ticket");
        reduc = true;
    }    
        else {
            System.out.println("Your speed was fine.");
            reduc = false;                
        }       
            if(reduc == true) {
                System.out.println(text + (sbanka - t1));
            }


    if(speed > speedlimit + 20) {
        System.out.println("you are getting a level two ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
            reduc = false;
        }        
            if(reduc == true) {
                System.out.println(text + (sbanka - t2));
            }


    if(speed > speedlimit + 30) {
        System.out.println("This is a serious offence, you are getting a level 3 ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
            reduc = false;
        }         
            if(reduc == true) {
                System.out.println(text + (sbanka - t3));
            }

}

}

H. Woolverton
  • 31
  • 1
  • 7
  • Read on variable scope. And if you see an error post the error statement with your question – Hovercraft Full Of Eels Sep 26 '17 at 01:32
  • You've declared the variable within the `if {...}` scope, meaning you can't use it outside of the `if {...}` block. Move it's deceleration to above the `if` statement and provide it a default value (of `false` I assume) – MadProgrammer Sep 26 '17 at 01:32

1 Answers1

0

You declared boolean reduc = true; under if stament if(speed > speedlimit + 10){} => local variable. So you cannot use it outside that if(){}.
Good to read http://www.geeksforgeeks.org/variable-scope-in-java/
Refactor like below:

//variables
    int sbanka = 400;
    int speed = 60;
    int speedlimit = 60;
    int ticket1 = 100;
    boolean reduc = false; // <= Fix your logic here by yourself

    //logic
    //Ticket 1
    if(speed > speedlimit + 10) {
        System.out.println("You will recieve the level 1 ticket");
        reduc = true;
    }
        else {
            System.out.println("Your speed was fine.");
        }
            //bank account calculation for ticket 1
            if(reduc == true && sbanka >-100) {
                sbanka -= 100;
                System.out.println("we will deduct 100 dollars from your checking account");
  • 1
    1. It's doesn't "have" to be the object "version" of `boolean` and 2. You've not supplied a default value, which cause a new compiler error – MadProgrammer Sep 26 '17 at 01:34
  • @MadProgrammer, Thanks. I just updated. –  Sep 26 '17 at 01:36
  • the object Boolean is never assigned! you can not access a variable before you assign a value in a method. and if it is at a class level it will be initialized to null by default. you might end up having NullPointerException when unboxing kicks in! – LeTex Sep 26 '17 at 01:36
  • The main problem of this question is about `Variable Scope`. Im just try to give an example. Anw, I fixed! Thanks. –  Sep 26 '17 at 01:39