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?