大家好, 人太笨了, 又遇到个问题. 一个很简单的程序, 把命令行输入的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
....这到底是怎么回事阿? 谢谢大家阿
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
....这到底是怎么回事阿? 谢谢大家阿
解决方案 »
- 关与一个按扭同时调用两个方法?
- java中Date类的使用出现一个Note!!
- 急求!!!急!!急!!!~高手做毕业设计!!!!!!!!!
- byte t = (r >> 24) & 0xFF;是什么意思呀?
- 弱问下,jsp和servlet难不难学?
- int[]转换成byte[]在线等??
- 请问如何修改List里item的间隔?
- (送分发发88)一个java应用程序的表单布置的小问题,希望大家帮忙?
- 请教:我想选个WEB APPLICATION SERVER,请问什么比较好?重谢
- 有考SCJD的吗?
- HashSet 中的 add 疑问
- ObjectInputStream readObject 连续读对象的问题
{
/* 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是个地址,而不是数值了.
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");
}
int
string
Right
多谢各位的回复了, 我复制你的代码, 结果是正确的, 但为什么我这样改下就不对了呢? 我怎么打死都看不出来有什么差别呢? @@
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);
}
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)
你需要在运行的参数里面加一段话, 模拟是给程序传命令, 比如 int-string 3 deijjdia
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里面的语句呢??