Answers for "java.util.scanner in java"

84

java scanner

import java.util.Scanner;

// import scanner 

Scanner myScanner = new Scanner(System.in); // Make scanner obj

String inputString = myScanner.nextLine(); // Take whole line

boolean inputBoolean = myScanner.nextBoolean(); //Boolean input

long inputLong = myScanner.nextLong(); //Interger,long ... input
Posted by: Guest on November-26-2019
38

scanner in java

import java.util.Scanner;		//Import Scanner in java

class classname{
  public void methodname(){
    Scanner s_name = new Scanner(System.in);	//Scanner declaration
    //Use Scanner object to take input
    int val1    =  s_name.nextInt();			//int
    float val2  =  s_name.nextFloat();			//float
    double val3 =  s_name.nextDouble();			//double
    string name =  s_name.nextLine();			//string
    char ch     =  s_name.nextLine().charAt(0);	//character
  }}
Posted by: Guest on May-25-2020
10

how to use scanners in java

import java.util.Scanner;

Public class Scanner {
	Public static void main(String[] args) {
    	// Scanner *scanner name here* = new Scanner(System.in);
      	Scanner scan = new Scanner(System.in);
      	System.out.println("Type anything and the scanner will take that input and print it");
      	String next = scan.next();
      	System.out.println(next);
    } 
}
Posted by: Guest on August-18-2020
1

how to provide a long string in Java Scanner class

If you use the nextLine() method immediately following the nextInt() method, 
nextInt() reads integer tokens; because of this, the last newline character for 
that line of integer input is still queued in the input buffer and the next 
nextLine() will be reading the remainder of the integer line (which is empty). 
So we read can read the empty space to another string might work. Check below 
code.
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();

        // Write your code here.
        double d = scan.nextDouble();
        String f = scan.nextLine();
        String s = scan.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
Posted by: Guest on October-19-2020
0

take input from user in java using scanner

import java.util.*;  
class UserInputDemo   
{  
public static void main(String[] args)  
{  
Scanner sc= new Scanner(System.in);    //System.in is a standard input stream  
System.out.print("Enter first number- ");  
int a= sc.nextInt();  
System.out.print("Enter second number- ");  
int b= sc.nextInt();  
System.out.print("Enter third number- ");  
int c= sc.nextInt();  
int d=a+b+c;  
System.out.println("Total= " +d);  
}  
} 
Posted by: Guest on July-25-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language