System.in Java Method Example

System.in Java Method Example

In my previous post about simple hello world java program, I introduced System.in method.Today am going to show you how to use it to get input from a user.Pass the variable to another method that calculates the sum of the tentered bythe user.

How to use System.in Java method 

Execute below code
[
public class InputMethodExample {


    public static int sum;
    /**     * @param args the command line arguments     */  
          public static void main(String[] args) {
        // TODO code application logic here        
          Scanner scanner=new Scanner(System.in);
        System.out.println("Enter number 1");
        int num1=scanner.nextInt();
        System.out.println("Enter number 2");
        int num2=scanner.nextInt();

        doSum(num1,num2);
    }

    public static void doSum(int a,int b)
    {
        int number1=a;
        int number2=b;
        sum=number1+number2;

        System.out.println("The sum of the two numbers is "+sum);

    }

}

]

Few things to note:

1.Non static method cannot be accessed in another static method that the reason why doSum() method is static.

2.Non static variable cannot be accessed in a static method and that the reason why int sum is a static variable.

See also:

System.exit() Java Method uses


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: System.in Java Method Example
System.in Java Method Example
System.in Java Method Example
Java Codes
http://www.download-all.net/2017/02/systemin-java-method-example.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/systemin-java-method-example.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy