2014-12-04 52 views
1

我正在製作一個可以將內存保存到文件的內存計算器,但我一直在收到「文件未找到」請幫助! 下面是從我的記憶計算器擴展節省內存計算器:嘗試使用java保存文件時出錯

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.util.Scanner; 



class SaveSciMemCalc extends SciMemCalc { 

public int displayMenu(){ 

    @SuppressWarnings("resource") 
    Scanner input = new Scanner(System.in); 
    int choice = -1; 
    while (choice < 1|| choice > 8){ 
     System.out.println(); 
     System.out.println("Menu:"); 
     System.out.println("1. Add"); 
     System.out.println("2. Subtract"); 
     System.out.println("3. Multiply"); 
     System.out.println("4. Divide"); 
     System.out.println("5. Power"); 
     System.out.println("6. Logarithm"); 
     System.out.println("7. Clear"); 
     System.out.println("8. Save"); 
     System.out.println("9. Quit"); 
     System.out.println(); 
     System.out.println("What would you like to do?"); 
    choice = input.nextInt(); 
    if (choice < 1|| choice > 9){ 
     System.out.println(choice +" wasn't one of the options"); 
    } 
    if (choice == 9){ 
     System.out.println("Thank you, good bye!"); 
     System.exit(0); 
    } 
    } 
    return choice; 
} 
String save(String saveList) throws FileNotFoundException{ 
    setCurrentValue(0); 
    File dir = new File(saveList); 
    PrintWriter pw = new PrintWriter(dir); 
    pw.println(dir); 
    pw.close(); 
    return "Saving..."; 
} 

} 

這裏是我做了運行計算器的驅動程序類:

import java.io.FileNotFoundException; 
import java.util.Arrays; 




public class SaveSciMemCalcDriver { 

public static void main(String[] args) { 
    String dog = ""; 
    SaveSciMemCalc calc = new SaveSciMemCalc(); 
    int p = 0; 
    String[] saves = new String[10]; 
    while (true){ 

     System.out.println("The current value is " + calc.getCurrentValue()); 

     double currentValue = calc.getCurrentValue(); 

     int choice = calc.displayMenu(); 

     double second = 0; 
     String o = null; 
     if (choice < 6) { 
      second = calc.getOperand("What is the second number? "); 
     } 

     if (choice == 1) { 
      calc.add(second); 
      o = " + "; 
     } else if (choice == 2){ 
      calc.subtract(second); 
      o = " - "; 
     } else if (choice == 3){ 
      calc.multiply(second); 
      o = " * "; 
     } else if (choice == 4){ 
      calc.divide(second); 
      o = "/"; 
     } else if (choice == 5){ 
      calc.power(second); 
      o = "^"; 
     } else if (choice == 6){ 
      calc.log(); 
      o = "log"; 
     } else if (choice == 7){ 
      calc.clear(); 
      Arrays.fill(saves, null); 
     } else if (choice == 8){ 
      for (int i=0;i<saves.length;i++){ 
       dog=dog+saves[i]+"\n"; 
      } 
      try { 
       calc.save(dog); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      Arrays.fill(saves, null); 
     } 
     if (choice == 6){ 
      saves[p] = o+currentValue+" = "+calc.getCurrentValue(); 
     }else{ 
      saves[p] = currentValue+o+second+" = "+calc.getCurrentValue(); 
     } 
    p++; 
    if (choice == 7){ 
     p=0; 
    if(choice == 8){ 
     p=0; 
    } 
    } 
} 
} 
} 

回答

1

它看起來像你積累什麼到保存在dog中,然後通過dog作爲saveList參數,然後將其作爲文件名。

for (int i=0;i<saves.length;i++) { 
    dog=dog+saves[i]+"\n";   // collect what to save 
} 

... 

calc.save(dog);      // pass it to save 

 

String save(String saveList) throws FileNotFoundException{ 
        ^^^^^^^^ = contains *what* to save 

    setCurrentValue(0); 
    File dir = new File(saveList); <-- Passed as filename to File constructor 
    PrintWriter pw = new PrintWriter(dir); 
    pw.println(dir);     <-- ...as well as written to file 
    pw.close(); 
    return "Saving..."; 
} 

相反,做

String save(String filename, String saveList) throws FileNotFoundException{ 
      ^^^^^^^^^^^^^^^ 

    setCurrentValue(0); 
    File dir = new File(filename); 
         ^^^^^^^^ 
    PrintWriter pw = new PrintWriter(dir); 
    pw.println(saveList); 
       ^^^^^^^^ 
    pw.close(); 
    return "Saving..."; 
} 
+0

它說theres仍然是文件名錯誤,是不是calculator.txt可以接受?編輯:沒關係,想通了。感謝您的幫助! – 2014-12-04 20:17:50

+0

如果問題解決了,請將答案標記爲已接受。 – aioobe 2014-12-04 20:49:12

0

文件構造丟失的文件的路徑。 一個空字符串文件構造函數引發FileNotFoundException