简体   繁体   English

使用 Scanner 类输入

[英]Input using Scanner Class

I am a new java programmer, In my below code when am trying to enter the employee name like john smith, only john is getting printed in output.我是一名新的 Java 程序员,在我下面的代码中,当我尝试输入像 john smith 这样的员工姓名时,只有 john 被打印在输出中。 And if I put如果我把

System.out.print("Enter Employee Name: ");   
String employeeName = s.next(); 

above以上

System.out.print("Enter SEX: ");       
char SEX = s.next().charAt(0);

this piece of code in output is not asking for my employeename value.输出中的这段代码不要求我的员工姓名值。 and directly printing the remaining values.并直接打印剩余的值。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
**package cs480;
import java.util.Scanner;
public class CS480D1_Richa_2_Week2 {
    public static void main(String [] args){
     Scanner s=new Scanner(System.in);  
     System.out.print("Ente Employee ID: ");
     int employeeId = s.nextInt();
     System.out.print("Ente SEX: ");       
     char SEX = s.next().charAt(0);
     System.out.print("Ente Employee Name: ");
     String employeeName = s.next();
     System.out.println("Employee ID is " +employeeId);
     System.out.println("Employee Name is " +employeeName);
     System.out.println("Employee gender is " +SEX);
    }        

}**

Try replacing your code尝试替换您的代码

String employeeName = scanner.nextLine();字符串员工姓名 = 扫描仪.nextLine();

instead相反

String employeeName = scanner.next();字符串员工姓名 = 扫描仪.next();

next() can read the input only till the space. next() 只能读取输入直到空格。 It can't read two words separated by space.它无法读取由空格分隔的两个单词。 Also, next() places the cursor in the same line after reading the input.此外,next() 在读取输入后将光标置于同一行。

nextLine() reads input including space between the words (that is, it reads till the end of line \\n). nextLine() 读取输入,包括单词之间的空格(即,它读取到行尾 \\n)。 Once the input is read, nextLine() positions the cursor in the next line.读取输入后,nextLine() 将光标定位在下一行。

Either you can use你可以使用

scanner.nextLine()

or override delimiter to use next()或覆盖分隔符以使用 next()

scanner.useDelimiter("\n");
scanner.next();

Please try with this:请试试这个:

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    System.out.print("Ente Employee ID: ");
    int employeeId = s.nextInt();
    System.out.print("Ente SEX: ");
    char SEX = s.next().charAt(0);
    System.out.print("Ente Employee Name: ");
    String employeeName = s.nextLine();
    employeeName = s.nextLine();// Try to add this line of code
    System.out.println("Employee ID is " + employeeId);
    System.out.println("Employee Name is " + employeeName);
    System.out.println("Employee gender is " + SEX);

}

Scanner.next() uses a white-space delimiter(takes the element before white-space). Scanner.next()使用空白分隔符(在空白之前获取元素)。

So either use所以要么使用

Scanner.nextLine() 

or reset the delimiter using或使用重置分隔符

Scanner s = new Scanner(input).useDelimiter("\\s*"your delimiter"\\s*");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM