2
インバージョンの数を見つけるためにmergesortの実装をしようとしています。 。配列は、ハードコードされた小さな番号のリストに対して正しい結果を返すようですが、ファイルから読み込むと不正な番号を返します。私は文字列の整数比較とは何かを推測しますが、正確に何が問題なのか理解できません。任意の洞察力はhelpful.Hereの(関連する)は、あなたが整数オーバーフローを取得しているファイルから読み込もうとしているときに逆転の数が見つからないようにするmergeSortの実装
public class ReadFile {
public static void main(String args[]){
int count=0;
int n[];
int i=0;
try{
n=OpenFile();
int num[] = new int[n.length];
for (i=0;i<n.length;i++){
num[i]=n[i];
// System.out.println("Num"+num[i]);
}
count=countInversions(num);
}
catch(IOException e){
e.printStackTrace();
}
System.out.println(" The number of inversions"+count);
}
public static int [] OpenFile()throws IOException{
FileReader fr=new FileReader("C:/IntegerArray.txt");// to put in file name.
BufferedReader textR= new BufferedReader(fr);
int nLines=readLines();
System.out.println("Number of lines"+nLines);
// Integer[] nData=new Integer[5000];
int[] nData=new int[nLines];
//int nData[]={1,3,5,2,4,6};
for (int i=0; i < nLines; i++) {
nData[ i ] = Integer.parseInt((textR.readLine()));// **Is this causing the problem?**
}
textR.close();
return nData;
}
public static int readLines() throws IOException{
FileReader fr=new FileReader("C:/IntegerArray.txt");
BufferedReader br=new BufferedReader(fr);
int numLines=0;
//String aLine;
while(br.readLine()!=null){
numLines++;
}
System.out.println("Number of lines readLines"+numLines);
return numLines;
}
public static int countInversions(int num[]){...
}
:トン!!魔法のように働いたおかげで私はすぐに質問からコメントを削除します! – KodeSeeker
と他の人は、メソッドは、stackoverflowでプライベートメッセージを送信する方法はありますか? – KodeSeeker