拆分扫描仪字符串

时间:2015-05-20 05:32:05

标签: java split java.util.scanner

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));
    }
}

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

分裂不起作用。

7 个答案:

答案 0 :(得分:1)

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]));
        }
    }

答案 1 :(得分:1)

import java.io.*;
public class Sol {
    public static void main(String[] args) throws IOException 
    { 
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
        String[] input = new String[2];     
        input = in.readLine().split(" "); 
        int a; 
        int b; 
        a = Integer.parseInt(input[0]); 
        b = Integer.parseInt(input[1]); 

        System.out.println("You input: " + a + " and " + b); 
    } 
}

答案 2 :(得分:0)

此代码适用于您

name: myApp
version: x.y.z

答案 3 :(得分:0)

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

另请注意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));
    }
}

答案 4 :(得分:0)

来自Class Scanner

  

扫描程序使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格。然后可以使用各种下一种方法将得到的标记转换为不同类型的值。

所以你不会使用MainFrame方法获得EditText product_quantity = (EditText) convertView .findViewById(R.id.editTextQuantity); product_quantity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.input_dialog); dialog.setTitle("What about Today!"); //initialize custom dialog items.** EditText mood = (EditText) dialog.findViewById(R.id.editTextyourMode); Button btnSaveButton = (Button) dialog .findViewById(R.id.btnSaveMyMoods); Button btnClear = (Button) dialog .findViewById(R.id.btnClear); dialog.show(); } }); 。相反,您可以直接使用' ',如:

scanner.next();

查看结果:output

答案 5 :(得分:0)

我在您的代码中看到的问题是使用$ ./bin/char_array dat/char_array.dat array[0][0] = 2 array[0][1] = 1 array[0][2] = 1 array[0][3] = 1 array[1][0] = 6 array[1][1] = 2 array[1][2] = 1 array[1][3] = -7 array[2][0] = -2 array[2][1] = 2 array[2][2] = 1 array[2][3] = 7 scanner.next();方法将读取下一个不带空格的标记,用于控制台输入next() define blabla变量的值将是input,即没有空格所以在你的情况下没有空格字符,因此define将返回input.indexOf(' ');-1 {例外}一个例外,一个quickfix会更改行{{1}到substring()这将读取整行而不是令牌。

答案 6 :(得分:0)

    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/" ));}