cris22tian
cris22tian

Reputation: 41

Storing value in another class

I have two classes created and I am trying to get the value from the user in 'UserInterface' class, but I want it to be stored in my second class called 'Calculator'.

import java.util.Scanner;

public class UserInterface {

    public static void main (String Args[]) {
        Calculator calculator = new Calculator();
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your first value: \t");
        input.nextInt(firstValue);
        System.out.println("Enter your second value: \t");
        input.nextInt(secondValue);
    }
}

I would like input.nextInt(firstValue); to pass the value to firstValue which is in the 'calculator' class shown below.

public class Calculator {

    public int firstValue;
    public int secondValue;

    public Calculator(int firstValue, int secondValue) {
        this.firstValue = firstValue;
        this.secondValue = secondValue;         
    }
}

Thanks in advance.

Upvotes: 0

Views: 101

Answers (4)

beingmanish
beingmanish

Reputation: 1120

nextInt() doesn't take any argument !

Simple just create the getter and setter for the fields in calculator,and set them while reading through the scanner ;

OR

Another approach would be take two local variables while reading by the scanner and store both the inputs in those local variables,then finally call parameterized constructor of calculator passing the local variables as arguments.

Upvotes: 0

Ihor Dobrovolskyi
Ihor Dobrovolskyi

Reputation: 1241

You can use the code like this:

public static void main (String Args[]) {
    Calculator calculator = new Calculator();
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your first value: \t");
    calculator.firstValue = input.nextInt();
    System.out.println("Enter your second value: \t");
    calculator.secondValue = input.nextInt();
}

or the code like this:

public static void main (String Args[]) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your first value: \t");
    int firstValue = input.nextInt();
    System.out.println("Enter your second value: \t");
    int secondValue = input.nextInt();
    Calculator calculator = new Calculator(firstValue, secondValue);
}

At the first example, you are setting the values after a calculator instance was created.

At the second one, you are creating the calculator instance with values you want.

Upvotes: 3

Kamil Banaszczyk
Kamil Banaszczyk

Reputation: 1153

You should read more about object oriented programming, this is very trivial question. You can do this in many way for example:

System.out.println("Enter your first value: \t");
int value = input.nextInt();
calculator.firstValue = value;

or

Scanner input = new Scanner(System.in);
System.out.println("Enter your first value: \t");
int firstValue = input.nextInt();
System.out.println("Enter your second value: \t");
int secondValue = input.nextInt();
Calculator calculator = new Calculator(firstValue, secondValue);

or you can use setters to set values and make fields private. But as I said before, you should learn more about OOP

Upvotes: 2

David
David

Reputation: 219096

Scanner.nextInt() returns the value, you don't pass it a value. Something like this:

int firstValue = input.nextInt();

Do this for both of your inputs, then after you've defined the values you can pass them to the constructor for your class:

Calculator calculator = new Calculator(firstValue, secondValue);

Additionally, you should make the fields on the Calculator class private instead of public. Public fields are poor form, and there's lots of literature which explains it better than I can in a simple answer here. But the idea boils down to an object should exclusively own its members and provide access to those members (via getters/setters in Java usually) only when needed.

Upvotes: 5

Related Questions