首页 > 编程语言 >Java: User Input (Scanner)

Java: User Input (Scanner)

时间:2022-11-27 07:22:06浏览次数:48  
标签:Java Scanner System value Reads user Input class User

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

import java.util.Scanner;  // Import the Scanner class

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}
MethodDescription
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user

 

 

 

 

 

 

 

 

 

Example:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);

    System.out.println("Enter name, age and salary:");

    // String input
    String name = myObj.nextLine();

// Numerical input
    int age = myObj.nextInt();
    double salary = myObj.nextDouble();

    // Output input by user
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);
  }
}

 

标签:Java,Scanner,System,value,Reads,user,Input,class,User
From: https://www.cnblogs.com/ShengLiu/p/16928939.html

相关文章

  • Java: Enums
    An enum isaspecial"class"thatrepresentsagroupof constants (unchangeablevariables,like final variables).enumLevel{LOW,MEDIUM,HIGH}......
  • Java: Interface
    Anotherwaytoachieve abstraction inJava,iswithinterfaces.An interface isacompletely"abstractclass"thatisusedtogrouprelatedmethodswithem......
  • Java: Inner Classes
    InJava,itisalsopossibletonestclasses(aclasswithinaclass).Thepurposeofnestedclassesistogroupclassesthatbelongtogether,whichmakesyour......
  • Java: Abstraction
    Abstractioncanbeachievedwitheither abstractclasses or interfaces.The abstract keywordisanon-accessmodifier,usedforclassesandmethods:Abst......
  • Java: Classes
    WhatareClassesandObjects?Aclassisatemplateforobjects,andanobjectisaninstanceofaclass.   Staticvs.Publicwecreateda static met......
  • Collectors groupingBy() method in Java with Examples
    https://www.geeksforgeeks.org/collectors-groupingby-method-in-java-with-examples/The groupingBy() methodofCollectorsclassinJavaareusedforgroupingo......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • Stream In Java
    https://www.geeksforgeeks.org/stream-in-java/ IntroducedinJava8,theStreamAPIisusedtoprocesscollectionsofobjects.Astreamisasequenceofobje......
  • Comparable vs Comparator in Java
    https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ Javaprovidestwointerfacestosortobjectsusingdatamembersoftheclass:  Comparabl......
  • equals() and hashCode() methods in Java
    https://www.geeksforgeeks.org/equals-hashcode-methods-java/ Java.lang.objecthastwoveryimportantmethodsdefined:publicbooleanequals(Objectobj)andp......