大家好, 人太笨了, 又遇到个问题. 一个很简单的程序, 把命令行输入的int-string, 以"-" 分开
public static void main(String[] args)
{

/* Parse the first argument from command line */
String keyType = args[0].split("-")[0];
String valueType = args[0].split("-")[1];
                System.out.println(keyType);
System.out.println(valueType);
                if( keyType.equals("int") && valueType.equals("string") )
{
System.out.println("Right");
}
                else
                        System.out.println("Wrong");
        }
程序的结果是
int
string
Wrong
在调试窗口里面看
name             value
keyType          "int"(id=18)对应的下拉菜单下的value
又是
[0] i
[1] n
[2] t
[3] -
[4] s
[5] t
[6] r
....这到底是怎么回事阿? 谢谢大家阿

解决方案 »

  1.   

    你试试这个public static void main(String[] args)
        {        
            
            /* Parse the first argument from command line */
            String[] keyType = args[0].split("-");
                    System.out.println(keyType[0]);
            System.out.println(valueType[1]);
                    if( keyType[0].equals("int") && keyType[1].equals("string") )
            {
                System.out.println("Right");
            }
                    else
                            System.out.println("Wrong");
            }
    如果这个代码可行的话,那你的问题估计就是keyType是个地址,而不是数值了.
      

  2.   


    public static void main(String[] args)
        {        
            
            /* Parse the first argument from command line */
            String[] keyType = args[0].split("-");
                    System.out.println(keyType[0]);
            System.out.println(keyType[1]);//刚才的代码这里忘记改了.
                    if( keyType[0].equals("int") && keyType[1].equals("string") )
            {
                System.out.println("Right");
            }
                    else
                            System.out.println("Wrong");
            }
      

  3.   

    我觉得没有问题,我机子上执行的结果!
    int
    string
    Right
      

  4.   


    多谢各位的回复了, 我复制你的代码, 结果是正确的, 但为什么我这样改下就不对了呢? 我怎么打死都看不出来有什么差别呢? @@
    public static void main(String[] args)
        {        
    String[] keyType = args[0].split("-"); /* Parse the second argument from command line */

        int genericTypeParam=0;
    if( keyType[0].equals("int") && keyType[1].equals("string") )
    {
    genericTypeParam = 1;
    //testListArrayInt = new MyListTest<Integer,String>(arraySize);
    }

    if( keyType[0].equals("string") && keyType[1].equals("int") )
    {
    genericTypeParam = 2;
    //testListArrayStr = new MyListTest<String,Integer>(arraySize);
    }
    else
    {
    System.out.println("Generic Type of AList can not handle, please use int-string or string-int combination!");
    System.exit(1);
    }
      

  5.   

    你的代码 我执行后是这个问题public class test {
    public static void main(String[] args)
        {        
            
            /* Parse the first argument from command line */
            String keyType = args[0].split("-")[0];//这里报错了。
            String valueType = args[0].split("-")[1];
                    System.out.println(keyType);
            System.out.println(valueType);
                    if( keyType.equals("int") && valueType.equals("string") )
            {
                System.out.println("Right");
            }
                    else
                            System.out.println("Wrong");
            }
    }
    报错提示:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at test.main(test.java:7)
      

  6.   


    你需要在运行的参数里面加一段话, 模拟是给程序传命令, 比如 int-string 3 deijjdia
      

  7.   

    我要疯了, 这个是完整的问题代码:
    public class QuickTest {
    public static void main(String[] args)
    {
    for (String s: args) {
                System.out.print(s + " ");
            }
    System.out.println();
    // Check if the command line has the right form 
    if( args.length != 3 )
    {
    // Or System.erro?
    System.out.println("Error: three arguments should be given!");
    System.exit(1);
    }

    // Parse the first argument from command line 
    //String keyType = args[0].split("-")[0];
    //String valueType = args[0].split("-")[1];
    //System.out.println(keyType);
    //System.out.println(valueType);
    String[] keyType = args[0].split("-");

        int genericTypeParam = 0;
        
    //if( keyType.equals("int") && valueType.equals("string") )
        if( keyType[0].equals("int") && keyType[1].equals("string") )
    {
    genericTypeParam = 1;

    }

    //if( keyType.equals("string") && valueType.equals("int") )
        if( keyType[0].equals("string") && keyType[1].equals("int") ) {
    genericTypeParam = 2;
    }
    else
    {
                            System.out.println(genericTypeParam);
    System.out.println("Generic Type Wrong!");
    System.exit(1);
    }

    }}
    我发现问题是这样:
    不管是用注释掉的
    keyType = args[0].split("-")[0];
    还是没注释掉的
    String[] keyType = args[0].split("-");输出结果都是:int-string 3 abc.txt 
    1
    Generic Type Wrong!然后程序退出怎么会执行else里面的语句呢??