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 2 of 2

Thread: defining a constructor with a scanner

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

    Default defining a constructor with a scanner

    Hi java beginner here.

    Im pretty sure there will be a simple solution to this. I am trying to


    this is the task


    2.1 Stone
    Define a constructor for class Stone that reads input from the user and initialises the name and
    weight fields according to the following I/O (bold text indicates sample user input):
    Page 3
    Programming Fundamentals (48023) Autumn 2013 Assignment 1
    Enter stone name: Diamond
    Enter stone weight: 12

    here is my code

    import java.util.Scanner;
    public class Stone
    {
    String name;
    int weight;

    Stone()
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter stone name: ");
    String name = Global.keyboard.nextLine();
    System.out.print("Enter stone weight: ");
    int weight = Global.keyboard.nextInt();
    //Global.keyboard.nextInt();


    }
    }

    this is what i get when submitting to online marking system



    % java TestMethods +Stone Diamond 12 @name @weight +Stone Gem 7 @name @weight
    == Benchmark program's output == | == Your program's output ==

    rand_seed = 24069 rand_seed = 24069
    $ stone = new Stone() $ stone = new Stone()
    Enter stone name: Diamond Enter stone name: Diamond
    Enter stone weight: 12 Enter stone weight: 12
    $ stone.name -> "Diamond" | $ stone.name -> null
    $ stone.weight -> 12 | $ stone.weight -> 0
    $ stone = new Stone() $ stone = new Stone()
    Enter stone name: Gem | Enter stone name: Enter stone weight: Gem
    Enter stone weight: 7 | java.util.InputMismatchException
    $ stone.name -> "Gem" | at java.util.Scanner.throwFor(Scanner.java:840)
    $ stone.weight -> 7 | at java.util.Scanner.next(Scanner.java:1461)
    > at java.util.Scanner.nextInt(Scanner.java:2091)
    > at java.util.Scanner.nextInt(Scanner.java:2050)
    > at Scanner.nextInt(Scanner.java:57)
    > at Stone.<init>(Stone.java:13)
    > at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Na
    > at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Nat
    > at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance
    > at java.lang.reflect.Constructor.newInstance(Construc tor.jav
    > at Invoker.create(Invoker.java:23)
    > at TestMethods.main(TestMethods.java:53)

    The left column shows the correct output. The right column shows your own
    program's output. Examine the sequence of steps line by line to see where
    your program went wrong.

    Between the two columns, you may find a marker symbol:

    * The '|' symbol identifies lines that are different.
    * The '<' symbol points to lines in the left column that are not in the right column.
    * The '>' symbol points to lines in the right column that are not in the left column.

    You can click on any error message highlighted in blue, and this will open up
    a web page explaining the error message.




    any ideas what i am doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: defining a constructor with a scanner

    java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    > at java.util.Scanner.nextInt(Scanner.java:2091)
    > at java.util.Scanner.nextInt(Scanner.java:2050)
    > at Scanner.nextInt(Scanner.java:57)
    > at Stone.<init>(Stone.java:13)
    At line 13 the code called nextInt() which read invalid data from the console. The nextInt() method wants numeric input. Make sure the user enters valid input.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2013, 09:30 AM
  2. help with defining a changing variable within a loop
    By uswhovian in forum Loops & Control Statements
    Replies: 3
    Last Post: March 9th, 2013, 09:30 AM
  3. help with defining a method
    By mrjavajava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2013, 06:06 AM
  4. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  5. [SOLVED] What do I need to do to use a bean in Netbeans 7 apart from defining it?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 31st, 2011, 12:29 AM