【问题标题】:reading more than one word from user从用户那里读取多个单词
【发布时间】:2015-05-28 13:10:32
【问题描述】:

主类:

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);
    }
}

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

【问题讨论】:

  • nextLine() 应该可以工作,但它总是返回字符串 - 也许您试图将字符串保存在其他变量中?
  • 使用 next() ,我得到:java.util.InputMismatchException !
  • 它适用于不包含空格的字符串!但是当我尝试使用多字串时,它会抛出一个异常,无论我接下来使用什么方法!

标签: java


【解决方案1】:

请添加这个:

    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}+

【讨论】:

  • 它就像一个魅力^^,但你能付出额外的努力并解释刚刚发生的事情吗?! ,
  • @Moawia307 这意味着return 或换行符将不计入您的input.next()
【解决方案2】:

问题是input.nextInt() 只读取一个整数。因此,当您在号码后按回车键时,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

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多