Results 1 to 3 of 3
  1. #1
    Lach is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Help with user input using the scanner class

    So I have this program that is supposed to take user input like car names and calculate prices and payment per month right here:

    Java Code:
    import java.lang.*;
        import java.io.*;
        import java.util.*;
        
        public class NewCar
        {
        
        private int year;
        private String make;
        private String model;
        private double sticker,discount,tax,finalP,final2,final3;
        private int month,monPayt;
        private int abbrev1;
        private String abbrev2;
        private String abbrev3;
        
           public NewCar(int year, String make, String model)
           {
              this.year= year;
              this.make= make;
              this.model = model;
           }
           
           public int getYear()
           {
              return year;
           }
           
           public String getMake()
           {
              return make;
           }
           
           public String getModel()
           {
              return model;
           }
           
           public String carDesc()
           {
              return year + " " + make + " "  + model;
           }
           
           public String carAbbrev()
           {
              abbrev1 = year % 100;
              String abbrev2 = make.substring(0,1);
              String abbrev3 = model.substring(0,1);
              return abbrev1 + abbrev2 + abbrev3;
           }
          
           
           public double calcFinalPrice()
           {
              final2 = (sticker - discount);
              final3 = final2 * tax;
              finalP = final2 + final3;
              return finalP;
           }
           
           public int calcZeroPctMonPayt()
           {
              monPayt = (int)finalP / month;
              return monPayt;
           }
           
              
           public String toString()
           {
              return "You want to purchase a " + carDesc() + ". Abbreviation: " + carAbbrev();
           }
           
           
        }

    Java Code:
    And i test it with this tester class:
    
        import java.lang.*;
        import java.io.*;
        import java.util.*;
        
        public class NewCarTester
        {
        
        
           
           
           
           public static void main(String[] args)
           {
              Scanner scan = new Scanner(System.in);
              
              System.out.println("Enter the car's Year, Make and Model: ");
              int year = scan.nextInt();
              String make = scan.next();
              String model = scan.next();
              
              System.out.println("Enter the Sticker Price: ");
              int sticker = scan.nextInt();
              
              System.out.println("Enter the discount: ");
              int discount = scan.nextInt();
              
              System.out.println("Enter the Sales Tax Rate: ");
              int tax = scan.nextInt();
              
              System.out.println("Enter the number of Months at Zero Percent Interest: ");
              int month = scan.nextInt();
              
              NewCar c = new NewCar(year, make, model);
              
              System.out.println(c.toString());
              System.out.println("Final Price : " + c.calcFinalPrice());
              System.out.println(c.calcZeroPctMonPayt());
             
              
             
              
              
            }
        }

    Now everything works fine accept the calcualtion methods, Whenever i call one of the calculation methods i get 0.0 everytime. This leads me to believe that the inputs the user is putting in are not correctly getting stored into the variables which they are supposed to go into. Can anyone identify the problem and help me fix it? Thanks! Also, one more problem im having is that if a user enters the sales tax with a decimal in it, the program gets an error and stops running, anyone have any insight on that?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with user input using the scanner class


  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Help with user input using the scanner class

    You have not yet responded to your first two posts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Compare Abstract Class Values to User input?
    By FadedAura in forum New To Java
    Replies: 16
    Last Post: 11-25-2011, 05:49 PM
  2. Replies: 5
    Last Post: 11-27-2010, 04:49 AM
  3. how to reverse some input from scanner
    By moncur in forum New To Java
    Replies: 24
    Last Post: 11-09-2010, 03:59 PM
  4. Replies: 1
    Last Post: 09-30-2010, 03:36 AM
  5. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 03:44 AM

Tags for this Thread

Posting Permissions

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