Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

java - I have an issue using tokens, scanning multiple inputs and executing them at the same time

I am having an issue, i need to scan different inputs that the user will insert on the screen at the same time. For example i have a sort of a menu that is printed on the screen and the user will select if he wants to load a file and type the name of file.I have done this by asking the user to type load and after that asked him what file to load by I need it to be done at the same time. The user just types "load S.txt" and it selects the loads option and opens the file at the same time.My professor told me I am supposed to use token but I'm stuck.(in java) Thanks .

          private static void inspecting (){
    System.out.println("inspect word or byte?");
    Scanner input3 = new Scanner( System.in );
    String insp = input3.next();
    if(insp.equals("word")){
        System.out.println("select the adrress you want to inspect the value");
        Scanner input4 = new Scanner( System.in );
        int addr = input4.nextInt();

             what i need is the user to type "ispect word at adrress x" withou doing it step by step.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand correctly your Scanner is breaking on spaces. You want it to break on new lines:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separator")); // this scans for lines
while (scanner.hasNext()) {
    String input = scanner.next();
    System.out.println(input); // take a look for yourself
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...