Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 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 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...