• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java Encapsulation and Inheritance

 
Greenhorn
Posts: 5
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a code for Inheritance and Encapsulation. It throws error for two testcases and it produces answer for another two. Kindly help me.


The error i get is.....

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at Main.main(Main.java:63)
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch! please use code tag to post your code from next time. You can refer this, click here --->Use Code Tags to know how to post code in code tag.

Monica Vedham wrote:Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at Main.main(Main.java:63)


InputMismatchException error is thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
For an example in main method of Main class you are taking input from user in form of int i.e. sc.nextInt() for choice of account type here on line no 58-59.
If you either enter value out of range of int type Or non- int value like floating point number Or String Or char where It was expecting int then It gives that error.
Can you please show us how you are entering values?

Edit:
Other changes I think you need to make are:
  • Don't write that much code in main method, better write a separate method getUserInput() Or something you think more descriptive. Worth reading Main Is A Pain
  • Following code is redunandant code on line no 63-68, 77-82 and 94-99 same code for all three types of account so you can write It once and store their values in three variables and in switch case you can use setter methods of Account class to set those values.

  • In following code on line no 5, 6 and 7 you can declare these variables with private access modifiers. As all of them have getter and setter method.


  • Same you you can declare below variables as private.Create getter and setter methods for them and access them using those methods.
  •  
    Sheriff
    Posts: 9012
    655
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch. There is one four things which is are out of scope of your main problem.

    1. Make your instance variables private. You already have written so called setters and getters to assign or retrieve instance variables values, so use them.
    2. Where is your constructor? Are you happy to open an account which doesn't belong to no one? Don't you want to let customer to open an account with certain amount of money in it?
    3. How do you know that certain account number doesn't exist before you set one? Maybe such number needs to be auto increment?
    4. Since you are familiar with keyword this, you can let parameter and instance variables have the same names.
     
    Liutauras Vilda
    Sheriff
    Posts: 9012
    655
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also, try not to use wild card imports (lines 1, 2). Campbell Ritchie told me once these are called on-demand imports, not wild card.

    Instead, specify importing classes explicitly:
     
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Not sure which line is line 63 that is causing the issue, but if it is around here then what name is being entered?

    Can you show the output from the app (not just the stack trace)?

    I ask because if the name is something like 'John Smith' then Scanner's next() method doesn't do what you expect.
     
    Liutauras Vilda
    Sheriff
    Posts: 9012
    655
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:Not sure which line is line 63..

    Dave, zoom browser up to 110%, this is what I do sometimes, when having problems like you do.

    Dave Tolls wrote:...what name is being entered?

    'John Smith' shouldn't cause that kind of issue. It just would make him become 'John'.

     
    Monica Vedham
    Greenhorn
    Posts: 5
    Eclipse IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [Moderation comment] Removed a huge amount of unnecessary quote

    The expected result is...
    1)Checking Account
    2)Saving Account
    3)Fixed Account
    Enter Account Type
    Enter The Account Number
    Enter The Customer Name
    Enter The Amount
    Enter The InterestRate
    Hi Jimesh Sharma, currently you are using Saving Account.
    Your account number is ACC456753
    Your are currently holding amount Rs.65846.0
    Rate of interest is 3.0%
     
    Monica Vedham
    Greenhorn
    Posts: 5
    Eclipse IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:

    Not sure which line is line 63 that is causing the issue, but if it is around here then what name is being entered?

    Can you show the output from the app (not just the stack trace)?

    I ask because if the name is something like 'John Smith' then Scanner's next() method doesn't do what you expect.





    May i know which function of Scanner can help the input like "John Smith"
     
    Liutauras Vilda
    Sheriff
    Posts: 9012
    655
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Monica Vedham wrote:May i know which function of Scanner can help the input like "John Smith"


    Look for the method called nextXXXX() in the Java API here.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Dave Tolls wrote:...what name is being entered?

    'John Smith' shouldn't cause that kind of issue. It just would make him become 'John'.





    Run that and enter John Smith for the name.

    What happens is John is picked up as the name, and Smith is still in the buffer.
    nextInt() then attempts to read an int but find only 'Smith' and throws an exception:

    Enter name:  
    John Smith
    Enter int:  
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at stuff.TestStuff.main(TestStuff.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
     
    Liutauras Vilda
    Sheriff
    Posts: 9012
    655
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:What happens is John is picked up as the name, and Smith is still in the buffer.

    Ah, oldest Scanner's trick. I didn't look into it as a sequence of steps. You're absolutely right. Have a cow.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I also didn't notice that, great job Dave  
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:
    Look for the method called nextXXXX() in the Java API here.



    And then be prepared to handle leftover line feeds...
    ;)
     
    Marshal
    Posts: 80665
    478
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The number of times I have had that Exception. . . . I have stopped looking for it in the code; I have seen it so often that I simply guess it has happened (‍). You need to beware of mixing nextLine and nextAnythingElse with a Scanner. I believe the correct solution is to create a utility class, maybe called KeyboardInputs, which you use for all your input and that class hides a Scanner from you. There is an explanation of a similar problem mixing nextInt and nextLine (and a possible solution) here. Please tell us which book you are using and what it says Scanner#nextLine does. Then compare that with what the API documentation says.
     
    Campbell Ritchie
    Marshal
    Posts: 80665
    478
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Aaaaaaaaaaaaaaaaaaaaaaaaargh! I have guessed wrong!

    No, it is a different error from what I thought. If you want a name after an int, try
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic