#move scanner into different class

24 messages · Page 1 of 1 (latest)

thin pilot
#

so i have this code

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int inputAsInteger = Integer.parseInt(input);

        Addition addition = new Addition(inputAsInteger);

        addition.print();

    }
}```

and this code in a different class

```public class Addition {
    private int numberOne;

    public Addition(int numberOne) {
        this.numberOne = numberOne;
    }

    public int getNumber() {
        return numberOne;
    }

    public void print() {
        System.out.println(numberOne);
    }
}```

i basically want to take what i put for the scanner in the main class and move it into a new class called subtraction for example
pulsar mirageBOT
#

This post has been reserved for your question.

Hey @thin pilot! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

thin pilot
#

any ideas to help

subtle wasp
#

The example isn't explanatory. How does a new class called Substraction relate to what you showed so far?

thin pilot
#

Ah I'm sorry if it's confusing I basically want to take the output of the scanner in main and move it to a different class I just used a random name based on the addition one

subtle wasp
#

You already did that here:

Addition addition = new Addition(inputAsInteger);
#

Do it again

thin pilot
#

i thought i tried that im guessing i need to add the Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int inputAsInteger = Integer.parseInt(input); part to?

subtle wasp
#

You already have a Scanner so you don't need to make a new one (and it may get in the way to make a new one)

#

As for the rest, sure, read yet the next line after the first. Just make sure not to try to re-create variables of the same names as variables you already created

thin pilot
#

how would i tranfer it to a different class then because i keep getting errors?

#

im sorry if im being confusing

subtle wasp
#

Don't try and think that moving to different classes will solve all problems

#

Identify what is your problem, and only move to a different class, what should move to a different class

thin pilot
#

im trying to get the output from the scanner thats in main and somehow transfer it to a different class but if i try and use Addition addition = new Addition(inputAsInteger); in a different class i get errors

subtle wasp
#

Why are you talking of a different class?

#

That line: Addition addition = new Addition(inputAsInteger);
it transfers the output of the Scanner to class Addition. But this line is not in class Addition

#

So here you have a transfer of the output of Scanner to another class

#

You can do that again

thin pilot
#

omg im so sorry i just realized what you meant

#

thank you so much for being so patient

pulsar mirageBOT
# thin pilot thank you so much for being so patient

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

thin pilot
#

and for all this help