简体   繁体   English

扫描仪 class (system.in) 在 android 工作室中不起作用,并且在 build.gradle 中发生了变化

[英]Scanner class (system.in) does not work in android studio and with changes in build.gradle

I have problem with Scanner Class in the android studio.我对 android 工作室中的扫描仪 Class 有疑问。 I create the New Module in android studio for coding pure java in this.我在 android 工作室中创建了新模块,用于在此编码纯 java。 After that I want use the Scanner Class for inputting some element from user but the program generate the following Error.之后,我想使用扫描仪 Class 从用户输入一些元素,但程序会生成以下错误。

package org.meicode.lib;
import java.util.*;
public class MyClass {
    public static void main(String []args){
        Scanner In= new Scanner(System.in);
        System.out.println("Enter Your Name Plz: ");
        String Name= In.next();
    }
}

The Main Class which I used the Scanner and System.in我使用扫描仪和 System.in 的主要 Class

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at org.meicode.lib.MyClass.main(MyClass.java:7)

FAILURE: Build failed with an exception.

The Error After I run the program运行程序后的错误

I search in stackoverflow and I see some solution for this problem like adding the我在stackoverflow中搜索,我看到了一些解决这个问题的方法,比如添加

run { standardInput = System.in }

but it does not work too, however the build.gradle run with "Task execution finished".但它也不起作用,但是 build.gradle 以“任务执行完成”运行。 you can see my build.gradle before and after the changes.你可以看到我的 build.gradle 在更改前后。 The Lib and the MyClass is the name of my module and the class respectively. Lib 和 MyClass 分别是我的模块和 class 的名称。

apply plugin: 'java-library'
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

Build.gradle before changes Build.gradle 更改前

apply plugin: 'java-library'
apply plugin:'application'
mainClassName = 'MyClass'

run { standardInput = System.in }
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

Build.gradle after changes更改后的 Build.gradle

I really hope you help me to overcome this problem.我真的希望你能帮助我克服这个问题。

I had the same issue with system.in not working.我在 system.in 不工作时遇到了同样的问题。 Restarting IDE worked for me.重新启动 IDE 对我有用。

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

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