Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6093055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:30:58+00:00 2026-05-23T12:30:58+00:00

In my program I’m reading integers form console many times. Every time, I need

  • 0

In my program I’m reading integers form console many times. Every time, I need to type this line.

new Scanner(System.in).nextInt(); 

I’m used to C/C++ and I’m wondering if I can define something like

#define READINT Scanner(System.in).nextInt(); 

and then, in every place in my java program I can read form console as

int a = new READINT;

But I read form books Java does not support macros.

Someone please explain me why is it so, and can I do this in any other way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:30:59+00:00Added an answer on May 23, 2026 at 12:30 pm

    You can but you shouldn’t.

    The shouldn’t part:

    You shouldn’t because using the pre-processor in that way is considered bad practice to start with, and there are better and more Java-idiomatic ways to solve this use case.

    The can part: (*)

    Java itself doesn’t support macros. On the other hand, you could pipe the source code through the C pre processor (CPP for short) just like the C/C++ compile chain does.

    Here’s a demo:

    src/Test.java:

    #define READINT (new java.util.Scanner(System.in).nextInt())
    
    class Test {
        public static void main(String[] args) {
            int i = READINT;
        }
    }
    

    cpp command:

    $ cpp -P src/Test.java preprocessed/Test.java
    

    Result:

    class Test {
        public static void main(String[] args) {
            int i = (new java.util.Scanner(System.in).nextInt());
        }
    }
    

    Compile:

    $ javac preprocessed/Test.java
    

    A better workaround:

    You can write your own utility class with a static method instead:

    import java.util.Scanner;
    class StdinUtil {
        public final static Scanner STDIN = new Scanner(System.in);
        public static int readInt() {
            return STDIN.nextInt();
        }
    }
    

    And when you want to use it, you can statically import the readInt method:

    import static StdinUtil.readInt; 
    
    class Test {
        public static void main(String[] args) {
            int i = readInt();
        }
    }
    

    (or do static import StdinUtil.STDIN; and use STDIN.nextInt().)

    And finally, an anecdote

    I myself used the CPP preprocessing approach on Java code once! I was creating a programming assignment for a course. I wanted to be able to easily extract a code skeleton out of the reference solution. So I just used a few #ifdefs to filter out the “secret” parts of the solution. That way I could maintain the reference solution, and easily regenerate the code skeleton.


    This post has been rewritten as an article here.


    (*) Since I hate answering questions with “you shouldn’t”. Besides, some future reader may have good reasons for wanting to use the cpp in conjunction with Java sources!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program so far has one purpose, take in two integers(the size of the
A program receives a list of Messages (base type). Each message in the list
Strange program hang, what does this mean in debug? After attaching windbg I found
program s; type info = record name, surname: string; min, sec: integer; end; arrays
Program: program s; type info = record name, surname: string; min, sek: integer; end;
my program makes a random name that could have a-z this code makes a
Program I'm making has a simple configuration file looking something like this. @overlays =
class Program { static void Main(string[] args) { Console.WriteLine(Fib 1: ); Console.ReadLine(); } long
This program require 'em-synchrony' ## v1.0.0 require 'em-hiredis' ## v0.1.0 module EventMachine module Hiredis
This program is supposed to calculate the number of degrees below 60 on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.