Java Nepali tutorial part 3: String and user input

0

String and taking user input

In this tutorial, we will learn about String data type and how to take input of various data types.

We can use already defined classes from Java library.  With the help of constructors, we create objects from these classes.
We can organize those classes in packages. To import Scanner class, we do
import java.util.Scanner;
After import statement, we write our program as usual
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
// statements
}
}

Scanner constructor needs input stream object. We create the object to Scanner constructor in the following way.

Scanner s = new Scanner ( System.in );

For various types of input, we use different Scanner method listed as below.

Method To accept
nextByte()  A byte
nextShort() A short
nextInt() An int
nextLong() A long
next() A single word
nextLine() A line of String
nextBoolean() A boolean
nextFloat() A float
nextDouble() A double

 

About Author

Abhisekh is. Something. You can read his blogs at http://abhishekacharya.com.np/