Java equivalent of cin
Java equivalent of Cin. The Language is build based on “OOP”,
All inputs and output will need to be treated as an object of a class.In order to use this class you must import
- import java.util.*;
The Equivalent of cin in Java is ” Scanner”which reside inside the java.util package mentioned above.If you have forgot something you can always refer back here
Follow the Code Step by step below to learn how to create a scanner in JAVA
- First I created a package and import java.util.*;
- I have created 2 static global variable which i will need to pass on data later;
- Then i create a Void Method to take in values from the Scanner.
- In this method it will take in 2 parameter , an object name and a number, and then it will compare the number and print out the object name and grades
- I go back to my main function
- I create 2 new Scanner Object , the first one i called it “scanInput” the second one i called it”scanInput2″
- scanInput will assign the value to inputNumber , while scanInput2 will assign the value to inputString
- Then I pass the input parameter gather from the user assign to inputNumber and inputString into the method “returnRatio( )”
- I close the obeject scanner (scanInput scanInput2 )
package oop; import java.util.*; public class samplemethod { static int inputNumber; static String inputString; public static void main(String[] args) { } }
public static void returnRatio(int cinNumber, String objectName ){ if(cinNumber < 30){ System.out.println("The Object Name is " +""+ objectName+"" +"The Value that you have enter is "+ cinNumber+" and your Grade is E"); } else if(30 <cinNumber && cinNumber <=50) { System.out.println("The Object Name is" + objectName+"The Value that you have enter is "+ cinNumber+" and your Grade is D"); } else if(50 <cinNumber && cinNumber <=60) { System.out.println("The Object Name is" + objectName+"The Value that you have enter is "+ cinNumber+" and your Grade is C"); } else if(60 <cinNumber && cinNumber <=80) { System.out.println("The Object Name is" + objectName+"The Value that you have enter is "+ cinNumber+" and your Grade is B"); } else if(80 <cinNumber && cinNumber <=100) { System.out.println("The Object Name is" + objectName+"The Value that you have enter is "+ cinNumber+" and your Grade is A"); } else{ System.out.println("Enter a Valid Number"); } }
public static void main(String[] args) { Scanner scanInput = new Scanner(System.in); Scanner scanInput2 = new Scanner(System.in); System.out.println("Input the Value from 0<x <100"); inputNumber = scanInput.nextInt(); System.out.println("Input the Object Name "); inputString = scanInput2.nextLine(); returnRatio(inputNumber,inputString ); scanInput.close(); scanInput2.close(); }
Well this wrap up the article on Java equivalent of cin Hopefully this help you out on your learning of the JAVA programming language. Check out how to create Arrays in Java here
Leave a Reply