简体   繁体   English

将Scanner对象连接到System.in对象并使用Scanner方法

[英]Connecting a Scanner object to a System.in object and using Scanner methods

Here is my problem: 这是我的问题:

I am trying to use Scanner and System.in to take the input from a keyboard and assign it to an int variable. 我正在尝试使用Scanner和System.in从键盘获取输入并将其分配给int变量。

This is what I have (the full program is included below): 这就是我所拥有的(完整的程序包含在下面):

// this program will use scanner objects and system inputs
public class Scanner
{
    public static void main(String[] args)
    {
        int number;  
             // declares integer "number"

        Scanner keyboard = new Scanner(System.in); 
  //-------------------------------------------------------------------
  // declares referance variable (Scanner class) "keyboard", creates       
  // Scanner object that 
  // reads input from System.in, then assigns address of Scanner object 
  // to the reference variable "keyboard"
  //-------------------------------------------------------------------     
        System.out.println("Enter an integer value: ");
             // displays text 
        number = keyboard.nextInt();
            // assigns keyboard input to "number" as integer value

        System.out.println("You entered the integer " + number);
           // displays "number" to see if the program worked
    }
}

When I try to compile the program in the windows command prompt using javac, I get this error message: 当我尝试使用javac在Windows命令提示符下编译程序时,出现以下错误消息:

error: constructor Scanner in class Scanner
cannot be applied to given types;

Scanner keyboard = new Scanner(System.in);

required: no arguments
found: InputStream
reason: actual and formal argument lists differ in length

error: cannot find symbol
            number = keyboard.nextInt();
                             ^
symbol:   method nextInt()
location: variable keyboard of type Scanner
2 errors

What am I doing wrong? 我究竟做错了什么?

There is a naming clash . 有一个命名冲突 You have to make your Scanner class unique from Java language perspective, there are 2 options to do that: 从Java语言的角度来看,您必须使Scanner类具有唯一性,可以通过以下两种方法进行:

  1. Rename the class 重命名课程
    • to some other name so that there till be no more naming clash 改成其他名称,以便不再有命名冲突
  2. Make the Scanner class unambiguous 使Scanner类明确

    • by using Fully Qualifed Names 通过使用全限定名

       public static void main(String[] args) { int number; java.util.Scanner keyboard = new java.util.Scanner(System.in); System.out.println("Enter an integer value: "); number = keyboard.nextInt(); System.out.println("You entered the integer " + number); } 

您应该重命名类Scanner-错误是,您使用Java标准库中名称为Scanner的类。

import java.util.Scanner;  

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

        System.out.println("Enter an integer value: ");

        number = keyboard.nextInt();
        System.out.println("You entered the integer " + number);

    }
}

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

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