Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java user input troubles using the scanner class

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java user input troubles 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:

     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();
           }
     
     
        }


    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
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java user input troubles using the scanner class

    The nextInt method is for ints not floats.

    In the calculation method you use the variable sticker, discount and tax. Where in your code do you assign values to those variables?

    --- Update ---

    Help with user input using the scanner class

    Duplicate post
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java user input troubles using the scanner class

    I apologize for the duplicate post, i thought these 2 forums were completely separate from each other. Thank you for the help though!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java user input troubles using the scanner class

    Quote Originally Posted by Lach View Post
    I apologize for the duplicate post, i thought these 2 forums were completely separate from each other. Thank you for the help though!
    They are separate, and therein lies the problem. I do suggest reading:
    http://www.javaprogrammingforums.com...s-posting.html

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java user input troubles using the scanner class

    Alright thanks for the heads-up, i never looked at it in such a way. I will refrain from crossposting in the future. Thanks!

Similar Threads

  1. How to repaint() a class in a JFrame, with a JTextfield user input
    By DANGEROUSSCION in forum Object Oriented Programming
    Replies: 1
    Last Post: March 23rd, 2013, 03:06 PM
  2. Java User-Input bug (not continuing); NOVICE
    By Shzylo in forum Java Theory & Questions
    Replies: 11
    Last Post: December 14th, 2012, 07:54 AM
  3. Replies: 4
    Last Post: December 4th, 2012, 06:11 PM
  4. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM

Tags for this Thread