public class parameterDivider {
static String[] pAndf;//用来存放得到的字符串
static int bIndex;//得到字符串在输入字符串中的开始位置
static int eIndex;//得到字符串在输入字符串中的终止位置
static Vector v=new Vector();//用来存放得到的字符串
static boolean comeout=false;//判断是否从头开始循环
static int another=0;//下一个查找的位置
static String [] str;//函数的返回值
static Index[] obj={};//存放得到的index对象
final static String quotation="\'";
final static String dash="-";
final static String space=" ";
final static String comma=",";
String test1="-cfs jjj -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test2="-cfs 'jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test3="-cfs 'jjj','jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test4="'jjj','jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test5="'jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test6="-cfs -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test7="-cfs jjj";
String test8="-cfs 'jjj'";
String test9="-cfs 'jjj','jjj'";
String test10="'jjj','jjj'";
String test11="'jjj'";
String test12="-cfs";
String test13="-c 'jkjk','jkjk'";
String test14="-c jjj";
String test15="-c 'jjj'";
String test16="-c";
String []test={test1,test2,test3,test4,test5,test6,test7,test8,test9,test10,test11,test12,test13,test14,test15,test16};
/** Creates a new instance of parameterDivider */
public parameterDivider() {
//pAndf=new String[obj.length];
//pAndf=dividerParameter(test[1]);
String [][]print=new String[16][];
for(int i=0;i<16;i++) {
pAndf=new String[dividerParameter(test[i]).length];
pAndf=dividerParameter(test[i]);
for(int j=0;j<pAndf.length;j++) {
System.out.print(pAndf[j]);
System.out.println("*");
}
System.out.println("/");
}
/*for(int i=0;i<2;i++) {
pAndf=dividerParameter(test[i]);
for(int j=0;j<pAndf.length;j++) {
System.out.print(pAndf[j]);
System.out.print("*");
}
System.out.println();
}*/
}
public String[] dividerParameter(String s) {
前面省略
obj=(Index[])v.toArray(obj);
str=new String[obj.length];
for(int i=0;i<obj.length;i++) {
str[i]=s.substring(obj[i].getStart(),obj[i].getEnd()+1);出现异常
}
return str;
}
public static void main(String args[]) {
new parameterDivider();
/*for(int i=0;i<pAndf.length;i++) {
System.out.println(pAndf[i]);
}*/
}
}程序运行出先异常,java.lang.StringIndexOutOfBoundsException: String index out of range: 55
。
谢谢
static String[] pAndf;//用来存放得到的字符串
static int bIndex;//得到字符串在输入字符串中的开始位置
static int eIndex;//得到字符串在输入字符串中的终止位置
static Vector v=new Vector();//用来存放得到的字符串
static boolean comeout=false;//判断是否从头开始循环
static int another=0;//下一个查找的位置
static String [] str;//函数的返回值
static Index[] obj={};//存放得到的index对象
final static String quotation="\'";
final static String dash="-";
final static String space=" ";
final static String comma=",";
String test1="-cfs jjj -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test2="-cfs 'jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test3="-cfs 'jjj','jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test4="'jjj','jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test5="'jjj' -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test6="-cfs -c 'jkjk','jkjk' -c jjj -c -c 'jjj'";
String test7="-cfs jjj";
String test8="-cfs 'jjj'";
String test9="-cfs 'jjj','jjj'";
String test10="'jjj','jjj'";
String test11="'jjj'";
String test12="-cfs";
String test13="-c 'jkjk','jkjk'";
String test14="-c jjj";
String test15="-c 'jjj'";
String test16="-c";
String []test={test1,test2,test3,test4,test5,test6,test7,test8,test9,test10,test11,test12,test13,test14,test15,test16};
/** Creates a new instance of parameterDivider */
public parameterDivider() {
//pAndf=new String[obj.length];
//pAndf=dividerParameter(test[1]);
String [][]print=new String[16][];
for(int i=0;i<16;i++) {
pAndf=new String[dividerParameter(test[i]).length];
pAndf=dividerParameter(test[i]);
for(int j=0;j<pAndf.length;j++) {
System.out.print(pAndf[j]);
System.out.println("*");
}
System.out.println("/");
}
/*for(int i=0;i<2;i++) {
pAndf=dividerParameter(test[i]);
for(int j=0;j<pAndf.length;j++) {
System.out.print(pAndf[j]);
System.out.print("*");
}
System.out.println();
}*/
}
public String[] dividerParameter(String s) {
前面省略
obj=(Index[])v.toArray(obj);
str=new String[obj.length];
for(int i=0;i<obj.length;i++) {
str[i]=s.substring(obj[i].getStart(),obj[i].getEnd()+1);出现异常
}
return str;
}
public static void main(String args[]) {
new parameterDivider();
/*for(int i=0;i<pAndf.length;i++) {
System.out.println(pAndf[i]);
}*/
}
}程序运行出先异常,java.lang.StringIndexOutOfBoundsException: String index out of range: 55
。
谢谢
pAndf=dividerParameter(test[0]);
for(int j=0;j<pAndf.length;j++) {
System.out.print(pAndf[j]);
System.out.println("*");
}
System.out.println("/");
就没事啊
我感觉是pAndf的长度不对,它的值好象是每次test字符串的加和。
for(int i=0;i<s.length();i++) {
comeout=false;//开始下面的查找
int m=i;//用于下面没有flag的判断
if(s.substring(i,i+1).equals(dash)) {//有flag的情况
bIndex=i;//保留开始的位置
for(i=i+1;i<s.length();i++) {
if(s.substring(i,i+1).equals(space)) {//找到flag后面的第一个空格
int j=i+1;
int k=j;//用于下面另两种情况的判断
if(s.substring(j,j+1).equals(quotation)) {//判断它下一个字符是否以单引号开头
for(j=j+1;j<s.length();j++) {
if(s.substring(j,j+1).equals(quotation)) {//找到下一个单引号
if(j!=s.length()-1) {//排除已经到字符串尾部的情况
if(!s.substring(j+1,j+2).equals(comma)) {//判断是否为最后一个单引号
eIndex=j;//截取字符串
v.add(new Index(bIndex,eIndex));//添加index对象
another=j+1;//跳过空格
i=another;
comeout=true;//跳出循环
break;
}
else {
j=j+2;//如果遇到逗号向后移动两个位置
}
}
else {//flag后面接一个带引号的文件
eIndex=s.length()-1;
v.add(new Index(bIndex,eIndex));
i=s.length();
comeout=true;
break;
}
}
}
}
//flag后面没有东西的情况
if(!comeout) {
j=k;
for(;j<s.length();j++) {
if(s.substring(j,j+1).equals(space)) {//排除可能是flag后面接不带单引号的字符串的情况
break;
}
if(s.substring(j,j+1).equals(quotation)||s.substring(j,j+1).equals(dash)){//排除所有中间的空格
eIndex=j-2;
v.add(new Index(bIndex,eIndex));
another=j-1;
i=another;
comeout=true;
break;
}
}
}
//flag后面接不带单引号的字符串的情况
if(!comeout) {
for(j=k+1;j<s.length();j++) {
if(s.substring(j,j+1).equals(space)) {//找到字符串的结束
eIndex=j-1;
v.add(new Index(bIndex,eIndex));
another=j;
i=another;
comeout=true;
break;
}
}
}
}
else {//只有flag的情况可能有多个,也可能有一个
if(i==s.length()-1) {
eIndex=s.length()-1;
v.add(new Index(bIndex,eIndex));
i=s.length();
comeout=true;
}
}
if(comeout) {//跳出循环
break;
}
}
if(comeout) {//成功截取字符串
continue;
}
}
else if(s.substring(i,i+1).equals(quotation)){//没有flag的情况
i=m;
bIndex=i;
for(int j=i+1;j<s.length();j++) {
if(s.substring(j,j+1).equals(quotation)) {//寻找下一个单引号
if(j!=s.length()-1) {//排除已经到字符串的尾部的情况
if(!s.substring(j+1,j+2).equals(comma)) {//找到最后一个单引号
eIndex=j;
v.add(new Index(bIndex,eIndex));
another=j+1;
i=another;
comeout=true;
break;
}
else {//遇到逗号向后移一个位置
j=j+2;
}
}
else {//一个带单引号的文件
eIndex=s.length()-1;
v.add(new Index(bIndex,eIndex));
i=s.length();
comeout=true;
break;
}
} }
}
}