kp

public class Solver {
 int n, capacity, answer;
 int weight, value;
 int
D;
 public Solver(int n, int weight, int value, int capacity) {
 this.n = n; this.weight = weight; this.value = value; this.capacity = capacity;
 }
 int solve(){
 int D = new int [n + 1][capacity + 1];
 for(int s = 0; s <= n; s++){ D[s][0] = 0;}
 for(int t = 1; t <= capacity; t++){ D[0][t] = 0;}
 for(int i = 1; i <= n; i++){
  for(int j = 1; j <= capacity; j++){
   if(j < weight[i]) D[i][j] = D[i-1][j];
   else {
 int v1 = D[i-1][j - weight[i]] + value[i];
 int v2 = D[i-1][j];
   if (v1 > v2)
       D[i][j] = v1;
   else   D[i][j] = v2;
  }
 }
 }
 return D[n][capacity];
 }
 }
 
 
 
 
 
 
 
 
import java.util.Scanner;
import java.io.File;
public class KP {
 public static void main(String args) {
 int num =0;
 int capacity = 0;
 int answer = 0;
 int weight = {0};
 int
value  = {0};
 try{
 File file = new File("1.txt");
 Scanner scan = new Scanner(file);
 String Num = scan.nextLine();
 String T = Num.split(" ",2);
 num = Integer.parseInt(T[0]);
 capacity = Integer.parseInt(T[1]);
 weight = new int[num + 1];
 value = new int[num + 1];
 Scanner Scan = new Scanner(file);
 String Num2 = scan.nextLine();
 for (int i = 0; i <= num ; i++){
  if (!Scan.hasNextLine()){
   break;
  }
  String line = Scan.nextLine();
 String date = line.split(" ",2);
 weight[i] = Integer.parseInt(date[0]);
 value[i] = Integer.parseInt(date[1]); 
 System.out.println(weight[i]+" "+ value[i]); 
   }
   }catch(java.io.FileNotFoundException e){
  System.out.println(e);
  System.exit(0);
 }

 Solver s = new Solver(num, weight, value, capacity);
 answer = s.solve();
 System.out.println(answer);
 }
}