从用户那里读取多个单词

时间:2015-05-28 13:10:33

标签: java

主要班级:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Student std = new Student();

        System.out.println("plz , inter id :");
        std.setId(input.nextInt());

        System.out.println("plz , inter name :");
        std.setName(input.next());

        System.out.println("plz , inter Age :");
        std.setAge(input.nextInt());

        System.out.println("plz , inter department :");
        std.setDepartment(input.next());

        System.out.println("plz , inter GPA :");
        std.setGpa(input.nextFloat());

        std.printStudentInfo();

    }
}

学生班:

public class Student {

    private int id;
    private String name;
    private int age;
    private String department;
    private float gpa;

    public void setId(int Pid) {
        this.id = Pid;
    }

    public int getId() {
        return this.id;
    }

    public void setName(String Pname) {
        this.name = Pname;
    }

    public String getName() {
        return this.name;
    }

    public void setAge(int Page) {
        this.age = Page;
    }

    public int getAge() {
        return this.age;
    }

    public void setDepartment(String Pdepartment) {
        this.department = Pdepartment;
    }

    public String getDepartment() {
        return this.department;
    }

    public void setGpa(float Pgpa) {
        this.gpa = Pgpa;
    }

    public float getGpa() {
        return this.gpa;
    }

    public void printStudentInfo() {
        System.out.println("--------------  " + "[" + this.id + "]" + "  "
                + this.name.toUpperCase() + " -----------------");
        System.out.println("age : " + this.age);
        System.out.println("Department : " + this.department);
        System.out.println("Gpa : " + this.gpa);
    }
}

这是一个简单的应用程序,它从用户读取一些数据并将其打印出来,我想在我的两个字符串字段“name,department”中读取用户的多个单词,但是,当我在部门间名称为两个或更多的单词,如“计算机科学”,我收到错误,我也尝试使用nextline()而不是next(),类似的结果,我最终犯了另一个错误!

2 个答案:

答案 0 :(得分:2)

请添加此内容:

    input.useDelimiter("\r\n");

    Scanner input = new Scanner(System.in);

无需readLine()

来自javadoc:

public Scanner useDelimiter(Pattern pattern)

Sets this scanner's delimiting pattern to the specified pattern.

Parameters:
    pattern - A delimiting pattern
Returns:
    this scanner

这意味着当您调用next[...]()时,模式集将成为分隔符。它会根据这种模式分裂。 所以默认的显然是空格。实际上这是:\p{javaWhitespace}+

答案 1 :(得分:2)

问题是input.nextInt()只读取整数。因此,当您在号码后按Enter键时,input.next()将扫描该换行符而不是您键入的输入。因此,请尝试添加额外的input.nextLine()来过滤该换行符并扫描您的正确输入:

Scanner input = new Scanner(System.in);

Student std = new Student();

System.out.println("plz , inter id :");
std.setId(input.nextInt()); //scans the number until newline

input.nextLine(); //scans the newline from the previous input
System.out.println("plz , inter name :");
std.setName(input.nextLine());

System.out.println("plz , inter Age :");
std.setAge(input.nextInt());

input.nextLine();   
System.out.println("plz , inter department :");
std.setDepartment(input.nextLine());

System.out.println("plz , inter GPA :");
std.setGpa(input.nextFloat());

std.printStudentInfo();

注意:此代码为 tested