In earlier post How to get input from console, I mentioned how to get numeric data as input from console using Scanner class methods nextInt(0 and nextDouble(). You can also get characters and Strings as input.
NOTE: I thought we should know more about getting input before learning other program flow control statements and loops, as we will all kind of data input in examples.
Input Characters from the Console / Keyboard
Like getting input for numeric data of different data types, we have different methods - nextInt(0, nextDouble(), nextFloat(). So, to get character, string or whole line as an input, there are three separate ways.
To get character as an input, we use read() method of in (InputStream in System class) class. read() method reads one character and it returns an integer value of the character. Before assigning the value to the char data type variable, we need to convert integer value back to the char data type. For converting integer value to char data type we use (char) and that is called type conversion / casting.
char ch = (char) System.in.read();
Here is the example :
NOTE: I thought we should know more about getting input before learning other program flow control statements and loops, as we will all kind of data input in examples.
Input Characters from the Console / Keyboard
Like getting input for numeric data of different data types, we have different methods - nextInt(0, nextDouble(), nextFloat(). So, to get character, string or whole line as an input, there are three separate ways.
To get character as an input, we use read() method of in (InputStream in System class) class. read() method reads one character and it returns an integer value of the character. Before assigning the value to the char data type variable, we need to convert integer value back to the char data type. For converting integer value to char data type we use (char) and that is called type conversion / casting.
char ch = (char) System.in.read();
Here is the example :
Class CharInput (CharInput.java)
NOTE: We will talk about type conversion / casting in coming posts. And also we will learn about exceptions.
To get String as an input, we use Scanner class like we use to get numeric data types. And then we use instance method next() of Scanner class. next() method will read only one word.
public class CharInput { public static void main(String args[]) throws java.io.IOException { char ch = (char) System.in.read(); System.out.println(ch); } }You can see in the example, we added a throws clause to main method. To handle input errors we must specify throws java.io.IOException to main method.
NOTE: We will talk about type conversion / casting in coming posts. And also we will learn about exceptions.
To get String as an input, we use Scanner class like we use to get numeric data types. And then we use instance method next() of Scanner class. next() method will read only one word.
Scanner sc = new Scanner(System.in); String s = sc.next();To get whole line as an input, we use instance method nextLine() of Scanner class. nextLine() will read whole line you entered -
Scanner sc = new Scanner(System.in); String s = sc.nextLine();Here is the example to get the String as word and line -
Class StringInput (StringInput.java)
import java.util.Scanner; public class StringInput { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the line:"); String line = sc.nextLine(); System.out.println("Line is " + line); System.out.println("Enter the word:"); String word = sc.next(); System.out.println("Word is " + word); } }We have created an object of Scanner class to get the strings. Here, it os noticeable that we don't need to throw IOException as Scanner class takes care of all input errors.
No comments:
Post a Comment