在http://penguin.ewu.edu/cscd501/Wint-2011/BranchAndBound/Knap01BnB.txt的main()函数中
inp = new Scanner ( new File(lineIn) );
maxWeight = inp.nextInt();
n = inp.nextInt();
System.out.printf ("Reading data from file %s, with %d items\n",
lineIn, n);
avail = new Item[n];
pack = new boolean[n];
for ( k = 0; k < n; k++ )
{ weight = inp.nextInt();
profit = inp.nextInt();
avail[k] = new Item(weight, profit);
}
使用上面的代码,我尝试运行 Bandp.txt,它没有问题。我在尝试使用十进制小数值运行 txt 文件时遇到问题:
Exception in thread "main" java.util.InputMismatchException at
java.util.Scanner.throwFor(Unknown Source) at
java.util.Scanner.next(Unknown Source) at
java.util.Scanner.nextInt(Unknown Source) at
java.util.Scanner.nextInt(Unknown Source) at
Knap01BnB.main(Knap01BnB.java:199)
我需要更改代码的哪一部分以便可以显示十进制值并且它们不会显示输入不匹配。
我的txt文件如下图:data.txt
100 20
12.64 8.43 4.21 8.45 17.56 8.31 13.85 12.39 19.32 14.29 4.03 17.14 8.24 1.24 0.79 5.59 6.6 12.18 12.24 1.67 6.45 19.43 9.88 8.75 18.37 15.64 8.24 5.55 3.6 6.4 6.69 18.44 3.07 0.71 11.28 10.25 14.26 12.14 0.71 2.37
这是 BandP.txt 中的值:
15 7 5 25 11 45 3 12 2 7 2 6 7 10 5 4