1
import java.util.*;

class Player {
    public static void main (String [] args) {
        String number = Text.nextLine
    }
}

I want the user input from this class and bring into another class and use the number variable for a If statement

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52

4 Answers4

1

I want the user input from this class and bring into another class and use the number variable for a If statement.

It is simple take a look at below example(make sure to add both classes in one package different java files as Player.java and ExampleClass.java),

This is the class that Scanner has:

import java.util.*;

public class Player{
    public static void main (String [] args){

        Scanner getInput = new Scanner(System.in);
        System.out.print("Input a number");
        //you can take input as integer if you want integer value by nextInt()
        String number = getInput.nextLine();

        ExampleClass obj = new ExampleClass(number);
        obj.checkMethod();
    }
}

This is the class that check number:

public class ExampleClass{
    int number;
    public ExampleClass(String number){
        try{
            //If you want to convert into int
            this.number = Integer.parseInt(number);
        }catch(NumberFormatException e){
            System.out.println("Wrong input");
        }
    }

    public void checkMethod(){
        if(number > 5){
            System.out.println("Number is greater.");
        }else{
            System.out.println("Number is lesser.");
        }
    }
}

Few thing to mention:

Your example code contains syntax errors, fix those first.

  1. If you want integer you can use getInput.nextInt() rather than getInput.nextLine().
  2. You can create getter and setters to set vaues and get values. In my example I just only set value through the constructor.
  3. Use proper naming convention.
  4. In my example I convert the String into integer inside the constructor and wrap with try-catch block to prevent from NumberFormatException(If you input character or something you can see wrong input will print). Sometimes in variaus situation it is not good to use try-catch in constructor. To learn more about this, please read Try / Catch in Constructor - Recommended Practice.
Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

Where you normally make an import on the other class, just import the Player class and it should work

cMcNerlin
  • 112
  • 8
0

I'm not sure if I got it, I suppose you're using a scanner. This is the way I would do that:

Scanner class:

public class ScannerTest { 

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("Insert a decimal:");
            String inputValue = scanner.nextLine();
            if(!new ScannerCalc().isNumeric(inputValue)){
                System.out.println("it's not a number...");
                break;
            } 
            else
                new ScannerCalc().checkNumber(inputValue);
        }
    }
}

ScannerCalc class:

public class ScannerCalc {

    public boolean isNumeric(String s) {  
        return s != null && s.matches("[-+]?\\d*\\.?\\d+");  
    }  

    public void checkNumber(String number){

        if(Integer.parseInt(number)%2==0)
            System.out.println("it' even");
        else
            System.out.println("it's odd");

    }
}

Pay attention on instantiation of classes to reuse methods.

Black.Jack
  • 1,878
  • 2
  • 22
  • 39
0

If you want to make use of a local variable in another entity, it is better to pass it as an argument to a method of the other entity. For example

OtherClass.operation(scanner.nextLine());   // In case method is static

new OtherClass().operation(scanner.nextLine());   // In case method is not static
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52