read values in java code example
Example 1: how to read input in java
//For continues reading a line
import java.util.Scanner;
Scanner in = new Scanner(System.in);
while(in.hasNextLine()) {
String line = in.nextLine();
System.out.println("Next line is is: " + line);
}
Example 2: read int from keyboard java
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
//For string
String text= scan.nextLine();
System.out.println(text);
//for int
int num= scan.nextInt();
System.out.println(num);
}
/*Is better to create another instance of Scanner if you have to use both nextline
and nextInt because they can conflict each other
*/
}