我正在开发一个程序,该程序从文件中读取输入并给出有多少字符串是互补的输出。这是代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class QuickSorting {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String data;
Boolean status=false;
int counter=0;
int cases,case_item;
String[]inputs;
String to_cmp;
String with_cmp;
// TODO Auto-generated method stub
File file = new File("input.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
//writer to write in file
data=br.readLine();
cases=Integer.parseInt(data);
//check total cases
for(int i=1;i<=cases;i++)
{
data=br.readLine();
case_item=Integer.parseInt(data.trim());
inputs=new String[case_item];
//check entries in each case
for(int c_i=0;c_i<case_item;c_i++)
{
data=br.readLine();
inputs[c_i]=data;
}
for(int i1=0;i1<(inputs.length-1);i1++)
{
for(int j=0;j<(inputs.length-1);i1++)
{
if(i1!=j)
{ to_cmp=inputs[i1].toString();
with_cmp=inputs[j];
status=compare_entry(to_cmp,with_cmp);
if (status)
{counter++;}
}
}
}
System.out.println("The number of complementary strings are "+counter);
}
}
public static boolean compare_entry(String to_cmp,String with_cmp)
{Boolean stat=false;
for(int i=0;i<(to_cmp.length()-2);i++)
{
if(to_cmp.charAt(i)!=with_cmp.charAt(i))
{stat=true;
}
else
{
break;
}
}
return stat;}
}
但是我在这一行中的第 58 行中将 Array 排除在键错误 to_cmp=inputs[i1].toString()中;