如何使用Scanner
类从控制台读取输入?像这样:
System.out.println("Enter your username: ");
Scanner = input(); // Or something like this, I don't know the code
基本上,我想让扫描仪读取用户名的输入,并将输入分配给String
变量。
答案 0 :(得分:316)
说明java.util.Scanner
如何工作的一个简单示例是从System.in
读取单个整数。这真的很简单。
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
要检索用户名,我可能会使用sc.nextLine()
。
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
如果您想要更多地控制输入,或者只是验证username
变量,您也可以使用next(String pattern)
。
答案 1 :(得分:32)
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();
答案 2 :(得分:21)
从控制台读取数据
BufferedReader
已同步,因此可以从多个线程安全地完成对BufferedReader的读取操作。可以指定缓冲区大小,或者可以使用默认大小(8192)。对于大多数用途,默认值足够大。
readLine() « 只是从流或来源中逐行读取数据。一条线被认为是由以下任何一条终止:\ n,\ r(或)\ r \ n
Scanner
使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格(\ s),并由Character.isWhitespace
识别。
« 在用户输入数据之前,扫描操作可能会阻塞,等待输入。 « 如果要从流中解析特定类型的令牌,请使用扫描程序(BUFFER_SIZE = 1024)。 « 扫描程序不是线程安全的。它必须在外部同步。
next()«查找并返回此扫描仪的下一个完整令牌。 nextInt()«将输入的下一个标记扫描为int。
<强>代码强>
String name = null;
int number;
java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine(); // If the user has not entered anything, assume the default value.
number = Integer.parseInt(in.readLine()); // It reads only String,and we need to parse it.
System.out.println("Name " + name + "\t number " + number);
java.util.Scanner sc = new Scanner(System.in).useDelimiter("\\s");
name = sc.next(); // It will not leave until the user enters data.
number = sc.nextInt(); // We can read specific data.
System.out.println("Name " + name + "\t number " + number);
// The Console class is not working in the IDE as expected.
java.io.Console cnsl = System.console();
if (cnsl != null) {
// Read a line from the user input. The cursor blinks after the specified input.
name = cnsl.readLine("Name: ");
System.out.println("Name entered: " + name);
}
Reader Input: Output:
Yash 777 Line1 = Yash 777
7 Line1 = 7
Scanner Input: Output:
Yash 777 token1 = Yash
token2 = 777
答案 3 :(得分:11)
input.nextInt()方法存在问题 - 它只读取int值。
因此,当使用input.nextLine()读取下一行时,您会收到“\ n”,即 Enter 键。所以要跳过这个,你必须添加input.nextLine()。
尝试这样:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (it consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
答案 4 :(得分:10)
有几种方法可以从用户那里获得输入。在这个程序中,我们将使用Scanner类来完成任务。此Scanner类位于java.util
下,因此该程序的第一行是 import java.util.Scanner; ,它允许用户在Java中读取各种类型的值。 import语句行必须在java程序的第一行,我们继续进行代码。
in.nextInt(); // It just reads the numbers
in.nextLine(); // It get the String which user enters
要访问Scanner类中的方法,请在&#34;中创建一个新的扫描仪对象作为&#34;。现在我们使用其中一种方法,即&#34; next&#34;。 &#34;下一个&#34; method获取用户在键盘上输入的文本字符串。
我在这里使用in.nextLine();
来获取用户输入的字符串。
import java.util.Scanner;
class GetInputFromUser {
public static void main(String args[]) {
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string " + s);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer " + a);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
答案 5 :(得分:9)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] arguments){
Scanner input = new Scanner(System.in);
String username;
double age;
String gender;
String marital_status;
int telephone_number;
// Allows a person to enter his/her name
Scanner one = new Scanner(System.in);
System.out.println("Enter Name:" );
username = one.next();
System.out.println("Name accepted " + username);
// Allows a person to enter his/her age
Scanner two = new Scanner(System.in);
System.out.println("Enter Age:" );
age = two.nextDouble();
System.out.println("Age accepted " + age);
// Allows a person to enter his/her gender
Scanner three = new Scanner(System.in);
System.out.println("Enter Gender:" );
gender = three.next();
System.out.println("Gender accepted " + gender);
// Allows a person to enter his/her marital status
Scanner four = new Scanner(System.in);
System.out.println("Enter Marital status:" );
marital_status = four.next();
System.out.println("Marital status accepted " + marital_status);
// Allows a person to enter his/her telephone number
Scanner five = new Scanner(System.in);
System.out.println("Enter Telephone number:" );
telephone_number = five.nextInt();
System.out.println("Telephone number accepted " + telephone_number);
}
}
答案 6 :(得分:6)
您可以创建一个简单的程序来询问用户的姓名,并打印回复使用的输入。
或者要求用户输入两个数字,您可以添加,乘法,减去或除以这些数字,并打印用户输入的答案,就像计算器的行为一样。
所以你需要Scanner类。您必须import java.util.Scanner;
,并且需要使用以下代码:
Scanner input = new Scanner(System.in);
input
是变量名。
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name: ");
s = input.next(); // Getting a String value
System.out.println("Please enter your age: ");
i = input.nextInt(); // Getting an integer
System.out.println("Please enter your salary: ");
d = input.nextDouble(); // Getting a double
了解这种情况有何不同:input.next();
,i = input.nextInt();
,d = input.nextDouble();
根据String,int和double对其余部分的改变方式相同。不要忘记代码顶部的import语句。
答案 7 :(得分:4)
一个简单的例子:
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int number1, number2, sum;
Scanner input = new Scanner(System.in);
System.out.println("Enter First multiple");
number1 = input.nextInt();
System.out.println("Enter second multiple");
number2 = input.nextInt();
sum = number1 * number2;
System.out.printf("The product of both number is %d", sum);
}
}
答案 8 :(得分:3)
当用户输入他/她username
时,也要检查有效输入。
java.util.Scanner input = new java.util.Scanner(System.in);
String userName;
final int validLength = 6; // This is the valid length of an user name
System.out.print("Please enter the username: ");
userName = input.nextLine();
while(userName.length() < validLength) {
// If the user enters less than validLength characters
// ask for entering again
System.out.println(
"\nUsername needs to be " + validLength + " character long");
System.out.print("\nPlease enter the username again: ");
userName = input.nextLine();
}
System.out.println("Username is: " + userName);
答案 9 :(得分:2)
import java.util.*;
class Ss
{
int id, salary;
String name;
void Ss(int id, int salary, String name)
{
this.id = id;
this.salary = salary;
this.name = name;
}
void display()
{
System.out.println("The id of employee:" + id);
System.out.println("The name of employye:" + name);
System.out.println("The salary of employee:" + salary);
}
}
class employee
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Ss s = new Ss(sc.nextInt(), sc.nextInt(), sc.nextLine());
s.display();
}
}
答案 10 :(得分:2)
以下是执行所需操作的完整类:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int valid = 6;
Scanner one = new Scanner(System.in);
System.out.println("Enter your username: ");
String s = one.nextLine();
if (s.length() < valid) {
System.out.println("Enter a valid username");
System.out.println(
"User name must contain " + valid + " characters");
System.out.println("Enter again: ");
s = one.nextLine();
}
System.out.println("Username accepted: " + s);
Scanner two = new Scanner(System.in);
System.out.println("Enter your age: ");
int a = two.nextInt();
System.out.println("Age accepted: " + a);
Scanner three = new Scanner(System.in);
System.out.println("Enter your sex: ");
String sex = three.nextLine();
System.out.println("Sex accepted: " + sex);
}
}
答案 11 :(得分:2)
阅读输入:
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
在使用某些参数/参数调用方法时读取输入:
if (args.length != 2) {
System.err.println("Utilizare: java Grep <fisier> <cuvant>");
System.exit(1);
}
try {
grep(args[0], args[1]);
} catch (IOException e) {
System.out.println(e.getMessage());
}
答案 12 :(得分:1)
您可以传输此代码:
Scanner obj= new Scanner(System.in);
String s = obj.nextLine();
答案 13 :(得分:0)
您可以使用Java中的Scanner类
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println("String: " + s);
答案 14 :(得分:0)
有一种从控制台读取的简单方法。
请找到以下代码:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Reading of Integer
int number = sc.nextInt();
// Reading of String
String str = sc.next();
}
}
有关详细说明,请参阅以下文件。
现在让我们谈谈对Scanner课程的详细了解:
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
这是用于创建Scanner实例的构造函数。
这里我们传递的InputStream
引用只是System.In
。这里打开InputStream
管道输入控制台。
public InputStreamReader(InputStream in) {
super(in);
try {
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## Check lock object
}
catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
}
通过传递System.in,此代码将打开套接字以便从控制台读取。