Search code examples
javaclassobjectinputdouble

how do i make the user input a double?


can somebody correct me what i did wrong here? my goal was for the user to input a number on the parameter System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1)); double upgradeAccessories= sc.nextDouble(); but i can't seem to see the issue

public static void main(String[] args) 
{
    DecimalFormat df = new DecimalFormat("#####");
    Scanner sc = new Scanner(System.in);
    
    double availAmount;
    
    Car owner = new Car();
    
    owner.owner("Marcus Laurence", 2014);
    
    double availAmount1=owner.upgradeAccessories(availAmount);
    double remainAmount=owner.upgradeAccessories(availAmount);
    
    System.out.println("Owner:" + owner.name);
    System.out.println("Model:" + owner.model);
    System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
    double upgradeAccessories= sc.nextDouble();
    System.out.println("Installed AC:" + owner.hasAC);
    System.out.println("Installed Leather Seats:" + owner.hasLeatherSeats);
    System.out.println("Installed Back Wipers:"+ owner.hasBackWipers);
    System.out.println("Installed Fog Lights:" + owner.hasFogLights);
    System.out.println("Amount Remaining After Upgrade:" + df.format(remainAmount));
}

public double upgradeAccessories(double availAmount) 
{          

        if(availAmount == 25000)
        {
            availAmount -= 21500;
            hasAC=true;
        }
        
        else if(availAmount == 40000) 
        {
            availAmount -= 21500;
            availAmount -= 14400;
            hasAC=true;
            hasLeatherSeats=true;
        }
        else if(availAmount == 50500) 
        {
            availAmount -=21500;
            availAmount -=14400;
            availAmount -=6250;
            availAmount -=3300;
            hasAC=true;
            hasLeatherSeats=true;
            hasBackWipers=true;
            hasFogLights=true;
        }
        return availAmount;
    }   

Solution

  • how do i make the user input a double?

    You did this part fine - scanner.nextDouble() is all it takes. Here's a really simple standalone example, including output:

    System.out.print("enter a double: " );
    Scanner sc = new Scanner(System.in);
    double d = sc.nextDouble();
    System.out.println("d: " + d);
    
    enter a double: 123.456
    d: 123.456
    

    So reading a double is not the issue. However, this line is suspicious:

    double upgradeAccessories = sc.nextDouble();
    

    Why? A few reasons..

    • It defines a new variable "upgradeAccessories" which is not used anywhere else – so it's reading a value (from sc.nextDouble()) and then doing nothing with it. This is something that an IDE (such as IntelliJ or Eclipse) will help you see – it will draw attention to unused code, like declaring a value that is never used.
    • The name of the variable – "upgradeAccessories" – is the same as the method name defined elsewhere in your code, namely:
      public double upgradeAccessories(double availAmount) 
      

    So to fix your code, it seems that you probably want to replace this:

    double upgradeAccessories = sc.nextDouble();
    

    with something like this:

    double next = sc.nextDouble();
    owner.upgradeAccessories(next);
    

    Also, the way you're comparing doubles is dangerous. Generally, fractional numbers are never exactly equal. Instead, of doing comparisons like this:

    if (availAmount == 25000) { ... }
    

    It's better to do something like greater-than-or-equal:

    if (availAmount >= 25000) { ... }
    

    Or if you're using only whole-number values, use integer instead of double, and then direct x == y comparisons will work fine.