如何在Java中清除Scanner中输入的字符串?

huangapple go评论75阅读模式
英文:

how does on clear inputted strings from a Scanner in Java?

问题

以下是翻译好的部分:

package Printer;

import java.util.Scanner;

public class echo {

    public static void print(String x) {
        System.out.print(x);
    }

    public static String y;

    public static String get(String x) {
        Scanner inputx = new Scanner(System.in);
        System.out.println(x);
        y = inputx.nextLine();
        inputx.close();
        return y;
    }
}
package story;

import Printer.echo;
import java.util.concurrent.TimeUnit;

public class MainStory {

    public static void main(String[] args) throws InterruptedException {

        echo.print("hello user and welcome to my dungeon \n");

        echo.get("are you willing to go thru 100 floors each more deadlier "
                + "than the next");

        if(echo.y.equals("yes")) {
            echo.print("well what ya waiting for, lets hurry up and "
                    + "pick your first class \n");
            TimeUnit.MILLISECONDS.sleep(500);
            echo.print("wait I'm supposed to give a list, "
                    + "hold on give me like 2 seconds to fetch the list off my drive \n");
            TimeUnit.SECONDS.sleep(2);
            echo.print("ok here it is in its full glory");
            echo.print("List of classes in Project: Ordus");
            echo.print("Tank");
            echo.print("Rogue");
            echo.print("DPS");
            echo.print("each class has a sus set of skills that "
                    + "allow it to blossom into a very cool run");
            echo.get("what shall it be \n");
            String input = echo.y;
            if(input.equals("tank") && echo.y.equals("Tank")) {
                echo.print("you're a tank");
            }
            if(input.equals("rogue") && echo.y.equals("Rogue")) {
                echo.print("you're a Rogue");
            }
            if(input.equals("DPS") && echo.y.equals("dps")) {
                echo.print("you're a dps");
            }
            echo.print("this is the end of the story so far");
            echo.print("please wait for an update in the near future");
        }
        else {
            
        }
    }
}

请注意,这只是您提供的代码的翻译部分。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

so I'm trying to make a small text adventure for some practice and I want to simplify things a bit. so I made a class that handles my text outputting and hopefully inputs. it worked the first time I tried to input something, but when I try again the code throws a NoSuchElementException.

here is the printer class
'''

package Printer;
import java.util.Scanner;
public class echo {
public static void print(String x) {
System.out.print(x);
}
public static String y;
public static String get(String x) {
Scanner inputx = new Scanner(System.in);
System.out.println(x);
y = inputx.nextLine();
inputx.close();
return y;
}
}

the part I'm working on

package story;
import Printer.echo;
import java.util.concurrent.TimeUnit;
public class MainStory {
public static void main(String[] args) throws InterruptedException {
echo.print("hello user and welcome to my dungeon \n");
echo.get("are you willing to go thru 100 floors each more deadlier "
+ "than the next");
if(echo.y.equals("yes")) {
echo.print("well what ya waiting for, lets hurry up and "
+ "pick your first class \n");
TimeUnit.MILLISECONDS.sleep(500);
echo.print("wait I'm supposed to give a list, "
+ "hold on give me like 2 seconds to fetch the list off my drive \n");
TimeUnit.SECONDS.sleep(2);
echo.print("ok here it is in its full glory");
echo.print("List of classes in Project: Ordus");
echo.print("Tank");
echo.print("Rogue");
echo.print("DPS");
echo.print("each class has a sus set of skills that "
+ "allow it to blossom into a very cool run");
echo.get("what shall it be \n");
String input = echo.y;
if(input.equals("tank") && echo.y.equals("Tank")) {
echo.print("your a tank");
}
if(input.equals("rogue") && echo.y.equals("Rogue")) {
echo.print("your a Rogue");
}
if(input.equals("DPS") && echo.y.equals("dps")) {
echo.print("your a dps");
}
echo.print("this is the end of the story so far");
echo.print("plese wait for a update in the near future");
}
else {
}
}
}

I want to try to clear the string before the user inputs again so I can be rid of the error.

答案1

得分: -2

足够的只是不关闭扫描器,所以只需删除 inputx.close() 这一行。

英文:

It'd be enough to not close the scanner, so just delete the inputx.close() line.

huangapple
  • 本文由 发表于 2020年10月10日 17:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64291963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定