【问题标题】:Java useDelimiter and nextLineJava useDelimiter 和 nextLine
【发布时间】:2020-02-15 13:56:30
【问题描述】:

我正在尝试创建一个使用分隔符分隔字符串输入(Windows 路径)的程序。但是我的程序似乎忽略了分隔符。

我期待的结果:

Skriv in sökvägen: C://Windows/System/

C

Windows

System

我得到的结果:

Skriv in sökvägen: C://Windows/System/

C://Windows/System/

我在下面的代码中遗漏了什么?

import java.util.Scanner; 

public class Sokvagen 
{

   public static void main(String[] args) 

   {

      //String representing pathway
      String sokvag;

      //Creating scanner object for reading from input stream 
      Scanner userInput = new Scanner(System.in);

      // Set delimiter to ':' or '/' or whitespace
      userInput.useDelimiter("[:/\\s]+"); 

      // Instructions to the user to type a windows patway ex: C://Windows/System/
      System.out.print("Skriv in sökvägen: ");

      //Input
      sokvag = userInput.nextLine();

      //Print the result
      System.out.println(sokvag);

      userInput.close();  
   }   
}

【问题讨论】:

  • 文档 - nextLine(): "使扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括任何 行分隔符 最后。”; next(): "从这个扫描器中查找并返回下一个完整的标记。一个完整的标记前后是匹配分隔符模式的输入。"
  • 考虑检查Path(和Paths)类......
  • 当使用 next () 我得到 4 而不是 C C C C
  • 我得到"C""Windows""System"ideone.com/Q8n2jW(显然使用循环做得更好,参见Stefan's answer)((也不知道为什么\s,又名空白为分隔符))

标签: java string java.util.scanner delimiter


【解决方案1】:

userInput.nextLine() 总是返回整行,而userInput.next() 使用你的分隔符返回一个标记。但是随后您需要逐个令牌读取循环令牌中的输入,直到...

import java.util.Arrays;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        String sokvag;

        //Creating scanner object for reading from input stream
        Scanner userInput = new Scanner(System.in);

        // Set delimiter to ':' or '/' or whitespace
        userInput.useDelimiter("[:/\\s]+");

        // Instructions to the user to type a windows patway ex: C://Windows/System/
        System.out.print("Skriv in sökvägen: ");

        do
        {
            //Input
            sokvag = userInput.next();

            //Print the result
            System.out.println(sokvag);
        }
        while (????);

        userInput.close();
    }
}

问题是,您不知道用户何时输入了最后一个令牌(路径的最后一部分)。

所以最好将整个输入作为一行读取,然后将其分成几部分。例如:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        String sokvag;

        //Creating scanner object for reading from input stream
        Scanner userInput = new Scanner(System.in);

        // Instructions to the user to type a windows patway ex: C://Windows/System/
        System.out.print("Skriv in sökvägen: ");

        //Input
        sokvag = userInput.nextLine();
        String[] parts = sokvag.split("[:/\\s]+");

        //Print the result
        System.out.println(Arrays.toString(parts));

        userInput.close();
    }
}

当然你也可以遍历parts-array来逐行输出内容。

【讨论】:

    【解决方案2】:

    我发现我应该使用 3 个字符串并使用 next 并且一切正常。

      //String representing pathway
      String sokvag1, sokvag2, sokvag3;
    
      //Creating scanner object for reading from input stream 
      Scanner userInput = new Scanner(System.in);
    
      // Set delimiter to ':' or '/' or whitespace
      userInput.useDelimiter("[:/\\s]+"); 
    
      // Instructions to the user to type a windows patway ex: C://Windows/System/
      System.out.print("Skriv in sökvägen: ");
    
      //Input
      sokvag1 = userInput.next();
      sokvag2 = userInput.next();
      sokvag2 = userInput.next();
    
      //Print the result
      System.out.println(sokvag1);
      System.out.println(sokvag2);
      System.out.println(sokvag3);
    
      userInput.close();  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      相关资源
      最近更新 更多