简体   繁体   中英

Java class to replace print statement and scanner input

Good Evening All,

I need help writing a class that enables to write a code like:

int number  = Utility.getInt("Enter an int", '>');

to replace something like:

System.out.println("Enter an int>");
Scanner scan = new Scanner(system.in);
int number = scan.nextInt();

Please find my solution below. I am not sure if I am understanding the prompt correctly, not sure if I need to use a Java pre-defined Utility class or if I just need to name the class Utility. Any help or tip would be greatly appreciated thanks!

public class Utility {
private int number; 
public Auto setInt (int number){
return "Enter an int" + number;
if (number>0)
   this.number = newNumber;
return this;
}
public int getInt(){
return newNumber;
}
}

Have you tried static methods like below ?

public class Utility {
    public static Integer getInt(String message){
        System.out.println(message);
        Scanner scan = new Scanner(System.in);
        Integer number = scan.nextInt();
        return number;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM