2016-04-21 2 views
0

Пожалуйста, игнорируйте неаккуратное кодирование. Я довольно новичок в java, и я пытаюсь сделать этот проект для школы. Я не могу найти причину этой ошибки дляЯ продолжаю получать только эти компиляционные ответы в 3-х методах. JAVA

DeCrossDiscipline.java:455: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     PrintWriter outputFile = new PrintWriter(filename); 
           ^
DeCrossDiscipline.java:533: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     PrintWriter outputFile = new PrintWriter(filename); 
           ^
DeCrossDiscipline.java:623: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     PrintWriter outputFile = new PrintWriter(filename); 
           ^
3 errors 

Это программа, в которой я получаю ошибки. Пожалуйста, помогите. Я чувствую, что мне просто нужна еще одна пара глаз, чтобы определить проблему для меня. импорт java.util.Scanner; импорт java.io. *;

public class DeCrossDiscipline 
{ 
     public static void main(String[] args) throws IOException 
     { 
     Scanner keyboard = new Scanner(System.in); 

     int deSelection; 
     int method; 
     int method2; 
     int method3; 
     blanklines(); 

     System.out.println("\tDifferential Equation Cross Discipline Project"); 

     System.out.println(); 

     demenu(); 
     deSelection = keyboard.nextInt(); 

     if (deSelection<1) 
     { 
      System.out.println(); 
      System.out.println("That is not a choice. Please try again.\n"); 
      demenu(); 
      deSelection = keyboard.nextInt(); 
      } 

     if (deSelection>3) 
     { 
      System.out.println(); 
      System.out.println("That is not a choice. Please try again.\n"); 
      demenu(); 
      deSelection = keyboard.nextInt(); 
      } 


     if (deSelection==1) 
     {menu1(); 
      method=keyboard.nextInt(); 
      if (method<1) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu1(); 
      method=keyboard.nextInt(); 
      } 
      if (method>2) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu1(); 
      method=keyboard.nextInt(); 
      } 

      if (method==1) 
      {rungeKutta1(); 
      } 
      if (method==2) 
      {euler1(); 
      } 
      } 
     if (deSelection==2) 
     {menu2(); 
      method2=keyboard.nextInt(); 
      if (method2<1) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu2(); 
      method2=keyboard.nextInt(); 
      } 
      if (method2>2) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu2(); 
      method2=keyboard.nextInt(); 
      } 
      if (method2==1) 
      {rungeKutta2(); 
      } 
      if (method2==2) 
      {euler2(); 
      } 
      } 
     if (deSelection==3) 
     {menu3(); 
      method3=keyboard.nextInt(); 
      if (method3<1) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu3(); 
      method3=keyboard.nextInt(); 
      } 
      if (method3>2) 
      { 
      System.out.println(); 
      System.out.println("Not a valid choice!"); 
      menu3(); 
      method3=keyboard.nextInt(); 
      } 

      if (method3==1) 
      {rungeKutta3(); 
      } 
      if (method3==2) 
      {euler3(); 
      } 
      }  

     } 

     public static void blanklines() 
     { 
     for (int l=0; l<11; l++) 
     { 
      System.out.println(); 
      } 
     } 

     public static void demenu() 
     { 
     System.out.println("1. y'=(x+1)^2\n" + 
          "2. y'=3(x^2)*(y-4)^2\n" + 
          "3. y'=3(x^2)*(y-2)^2"); 
     System.out.print("Please enter the number of which \ndifferential equation you want to use: "); 
     } 

     public static void menu1() 
     { 

     System.out.print("\n1. Runge Kutta\n" + 
          "2. Euler\n"); 
     System.out.print("Please enter the number of which \nmethod you want to use: ");     
     } 

     public static void menu2() 
     { 
     System.out.print("\n1. Runge Kutta\n" + 
          "2. Euler\n"); 
     System.out.print("Please enter the number of which \nmethod you want to use: ");     
     } 

     public static void menu3() 
     { 
     System.out.print("\n1. Runge Kutta\n" + 
          "2. Euler\n"); 
     System.out.print("Please enter the number of which \nmethod you want to use: ");     
     } 


     public static void rungeKutta1() throws IOException 
     { 
     Scanner keyboard = new Scanner(System.in); 

     double y; 
     double h; 
     double x; 
     double i; 


     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 

     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 

     double K1, K2, K3, K4, K5, K6, K7, K8, K9, K10; 

     File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 

     for (int number=1; number<=i; number++) 
      { 
      System.out.printf("When x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 
       K1 = Math.pow(x+1, 2); 
       K2 = x+h/2; 
       K3 = y+(h/2)*K1; 
       K4 = Math.pow(K2+1, 2); 
       K5 = y+(h/2)*K2; 
       K6 = Math.pow(K2+1, 2); 
       K7 = x+h; 
       K8 = y+h*K6; 
       K9 = Math.pow(K7+1, 2); 
       K10 = y+h/6*(K1+2*(K4+K6)+K9); 

       x = x+h; 

       y = K10; 

       } 
       outputFile.close(); 
       } 



     public static void euler1()throws IOException 
     { 
     Scanner keyboard = new Scanner(System.in); 

     double y; 
     double h; 
     double x; 
     double i; 

     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 


     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 

     double E1, E2, E3, E4, E5; 

      File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 


     for (int number=1; number<=i; number++) 
     { 
      System.out.printf("While x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 

      E1 = Math.pow(x+1, 2); 

      E2 = x+h; 

      E3 = y+(h*E1); 

      E4 = Math.pow(E2+1, 2); 

      E5 = y+h/2*(E1+E4); 

      x = x+h; 

      y = E5; 

      } 
      outputFile.close(); 
      } 



      public static void rungeKutta2() throws IOException 
      { 
      Scanner keyboard = new Scanner(System.in); 

     double y; 
     double h; 
     double x; 
     int i; 

     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 


     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 

     double R1, R2, R3, R4, R5, R6, R7, R8, R9, R10; 

     File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 

     for (int number=1; number<=i; number++) 
      { 
       System.out.printf("While x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 

       R1 = 3*(x*x)*Math.pow((y-4), 2); 

       R2 = x+h/2; 

       R3 = y+(h/2)*R1; 

       R4 = (3*(R2*R2))*Math.pow(R3-4, 2); 

       R5 = y+(h/2)*R4; 

       R6 = (3*(R2*R2))*Math.pow(R5-4, 2); 

       R7 = x+h; 

       R8 = y+h*R6; 

       R9 = (3*(R7*R7))*Math.pow(R8-4, 2); 

       R10 = y+h/6*(R1+2*(R4+R6)+R9); 


       x = x+h; 

       y = R10; 
       } 
       outputFile.close(); 
      } 



      public static void euler2() 
      { 
      int i; 
      double y; 
      double h; 
      double x; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 


     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 


      double E1, E2, E3, E4, E5; 

      File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 

      for (int number=0; number<=i; number++) 
       { 
       System.out.printf("While x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 

       E1 = 3*(x*x)*Math.pow((y-4), 2); 

       E2 = x+h; 

       E3 = y+h*E1; 

       E4 = (3*(E2*E2))*Math.pow((E3-4), 2); 

       E5 = y+h/2*(E1+E4); 

       x = x+h; 

       y = E5; 
       } 
       outputFile.close(); 
      } 



      public static void rungeKutta3() 
      { 
      Scanner keyboard = new Scanner(System.in); 

     double y; 
     double h; 
     double x; 
     int i; 

     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 


     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 

     double R1, R2, R3, R4, R5, R6, R7, R8, R9, R10; 

     File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 

     for (int number=1; number<=i; number++) 
      { 
       System.out.printf("While x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 

       R1 = 3*(x*x)*Math.pow((y-2), 2); 

       R2 = x+h/2; 

       R3 = y+(h/2)*R1; 

       R4 = (3*(R2*R2))*Math.pow(R3-2, 2); 

       R5 = y+(h/2)*R4; 

       R6 = (3*(R2*R2))*Math.pow(R5-2, 2); 

       R7 = x+h; 

       R8 = y+h*R6; 

       R9 = (3*(R7*R7))*Math.pow(R8-2, 2); 

       R10 = y+h/6*(R1+2*(R4+R6)+R9); 


       x = x+h; 

       y = R10; 
       } 
       outputFile.close(); 
      } 



      public static void euler3() 
      { 
      int i; 
      double y; 
      double h; 
      double x; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.print("\nPlease enter an initial y value: "); 
     y = keyboard.nextDouble(); 

     System.out.print("Please enter an initial x value: "); 
     x = keyboard.nextDouble(); 

     System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     if (h<=0) 
      { 
       System.out.println("\nEnter a valid h value"); 
       System.out.print("Please enter a h value: "); 
     h = keyboard.nextDouble(); 
     } 


     System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
     if (i<1) 
      { 
       System.out.println("\nPlease enter an amount of 1 or greater"); 
       System.out.print("Please enter the amount of results that you want: "); 
     i = keyboard.nextInt(); 
      } 

     System.out.println(); 

     System.out.print("Your results will be printed\n" 
          + "to a file. Please enter a name\n" 
          + "for the file: "); 
     String filename = keyboard.next(); 

     System.out.println(); 

     System.out.println("x values\t" + "y values"); 
     System.out.println("___________________________"); 


      double E1, E2, E3, E4, E5; 

      File file = new File(filename); 

     PrintWriter outputFile = new PrintWriter(filename); 

      for (int number=0; number<=i; number++) 
       { 
       System.out.printf("While x = %.2f\t", x); 
      System.out.printf("y = %.4f\n", y); 
      outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y); 
       outputFile.println(); 

       E1 = 3*(x*x)*Math.pow((y-2), 2); 

       E2 = x+h; 

       E3 = y+h*E1; 

       E4 = (3*(E2*E2))*Math.pow((E3-2), 2); 

       E5 = y+h/2*(E1+E4); 

       x = x+h; 

       y = E5; 
       } 
       outputFile.close(); 
      } 


      } 
+0

Предлагаю вам прочитать сообщение об ошибке и следовать предложенному предложению. В чем вы сомневаетесь? –

+0

Это очень много кода, чтобы прорыть ... Возможно, вы можете сузить его до тех областей, которые, по вашему мнению, вызывают ошибки. – WillardSolutions

ответ

1

В этом методе

public static void euler1()throws IOException 

вы заявляете, что этот метод может бросить IOException как FileNotFoundException.

Вы должны сделать это в каждом способе, который может произойти в том числе.

public static void euler2() 
{ 

Я настоятельно рекомендую использовать IDE для

  • поможет вам форматировать код.
  • рефакторинг класса, поэтому в нем не так много несвязанных методов.
  • добавьте эти исключения для себя по мере ввода.
+0

Спасибо за ответ. Я не могу поверить, что пропустил что-то такое простое. И вы правы, я просто не понимаю много концепций в java, поскольку я только начинаю. –

+0

@ T.B. это - то, где использование IDE с автоматическими исправлениями может помочь;) –

0
rungeKutta1() 
euler1() 
rungeKutta2() 
euler2() 
rungeKutta3() 
euler3() 

эти методы, которые я заметил, метание исключение filenotfound , так как эти методы используют эту конкретную строку

PrintWriter outputFile = new PrintWriter(filename);

вы можете изменить методы rungeKutta1()throws Exception или вы можете внедрить код внутри каждого метода для обработки исключения, подобного этому

try{ 
//your code inside the methods 
}catch(Exception e){ 
System.out.println("exeption"); 
} 
1

Проблема заключается в том, что euler2(), euler3() и rungeKutta3() не захватывают и не объявляют исключение FileNotFoundException (или это суперкласс IOException). Таким образом, вы можете либо обернуть соответствующий код там в попытке-улов блоках или для простоты просто объявить IOExceptions быть выброшены на этих 2 методов:

public static void euler2() throws IOException { [...] }

public static void euler3() throws IOException { [...] }

public static void rungeKutta3() throws IOException { [...] }

метательных FileNotFoundException в вашем основном методе, как предлагается в другом ответе, не имеет никакого эффекта, поскольку они уже должны быть пойманы тремя способами. Кроме того, FileNotFoundException - это исключение IOException, так что оно уже было закрыто.