java 拆分扫描仪字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30341021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:57:14  来源:igfitidea点击:

Splitting a scanner string

javasplitjava.util.scanner

提问by CheeseMaster123

import java.io.IOException;
import java.util.Scanner;


public class web_practice {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();

        int l = input.indexOf(' ');
        String cmd = input.substring(0, l);
        String end = input.substring(l);

        if (cmd.equals("define"));
        java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://dictionary.reference.com/browse/" + end));
    }
}

I was trying to make a code to find the definition of a word by connecting it to dictionary.com and checking if they say the word "define" as the first word?

我试图通过将一个词连接到 dictionary.com 并检查他们是否说“定义”这个词作为第一个词来制作一个代码来找到一个词的定义?

The splitting is not working.

拆分不起作用。

回答by Soumitri Pattnaik

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();

        int words[] = input.split(' ');

        if (words[0].equalsIgnoreCase("define")) {
            java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://dictionary.reference.com/browse/" + words[0]));
        }
    }

回答by Sumit Singh

From Class Scanner

来自类扫描器

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

Scanner 使用分隔符模式将其输入分解为标记,默认情况下与空格匹配。然后可以使用各种 next 方法将结果令牌转换为不同类型的值。

So you wont get ' 'using scanner.next();method. Rather you can directly use scanner.next();like:

所以你不会得到' '使用scanner.next();方法。相反,您可以直接使用scanner.next();像:

Scanner scanner = new Scanner(System.in);
String input = scanner.next();

if ("define".equals(input )){
     String end = scanner.next();// Next token
     java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://dictionary.reference.com/browse/" + end));
}

See the result: output

查看结果:输出

回答by silentprogrammer

The problem I see in your code is using scanner.next();the next()method will read the next token without space for ex for console input define blablathe value of inputvariable would be defineie without space so in your case there is no space character so input.indexOf(' ');will return -1giving exception for substring()a quickfix would be change the line scanner.next();to scanner.nextLine();which would read the whole line rather than token.

我在您的代码中看到的问题是使用scanner.next();next()方法将读取下一个没有空格的令牌用于控制台输入变量define blabla的值input将是defineie 没有空格,因此在您的情况下没有空格字符,因此input.indexOf(' ');将返回-1为快速substring()修复提供异常被改线scanner.next();scanner.nextLine();其内容的整条生产线,而不是令牌。

回答by Sonu Gupta

    Scanner scanner = new Scanner(System.in);
    String input = scanner.next();;
    String[] inputs = input.split(" ");
    if (inputs[0].equalsIgnoreCase("define")){  java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://dictionary.reference.com/browse/" ));}

回答by Alaa Abuzaghleh

this code will work for you

这段代码对你有用

    String[] strArr = input.split(" ") ; 

    if(strArr[0].equals("define"){
    }

回答by Tagir Valeev

Your problem is that you are using Scannerwhich default behavior is to split the InputStreamby whitespace. Thus when you call next(), your inputstring contains the command only, not the whole line. Use Scanner.nextLine()method instead.

您的问题是您使用的Scanner默认行为是InputStream按空格拆分。因此,当您调用 时next(),您的input字符串仅包含命令,而不是整行。改用Scanner.nextLine()方法。

Also take a note that String end = input.substring(l);will add a space into the end string. You probably want to use String end = input.substring(l+1);. Here's the fixed mainmethod:

还要注意,String end = input.substring(l);将在结束字符串中添加一个空格。您可能想要使用String end = input.substring(l+1);. 这是固定main方法:

public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();

    int l = input.indexOf(' ');
    if(l >= 0) {
        String cmd = input.substring(0, l);
        String end = input.substring(l+1);

        if (cmd.equals("define"));
            java.awt.Desktop.getDesktop().browse(
                java.net.URI.create("http://dictionary.reference.com/browse/" + end));
    }
}