New to file reading/writing Java

最后都变了- 提交于 2020-01-05 15:17:33

问题


I'm trying to create a banking program that reads account information from .txt file. It's just supposed to ask what you want to do, get your account number, find out how much you want to withdraw/deposit, and then make the changes on the text file.

I'm having some trouble with the FileNotFoundException. I've never used it before and I'm not sure how to "catch" it. I've searched a little and found some answers that don't make sense to me, something about a "try" statement and a FileReader which I wasn't planning on using. I have no idea what they do or where they would go. And guidance you could give me? (Please excuse the half-baked code, I cannot run it to see what works until this problem is fixed)

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


public class Main {

public static void main(String[] args){
    option();
    }

public static void option(){
    System.out.println("Enter the number for your selection \n 1) Deposit \n 2) Withdraw \n 0) Exit");
    Scanner keyboard = new Scanner(System.in);
    int input = keyboard.nextInt();
    if(input==0) close();
    System.out.println("Please enter your account number:");
    String account = keyboard.nextLine();
    System.out.println("Please enter the amount for your transaction:");
    String amount = keyboard.nextLine();

    if(input==1) deposit(account, amount);          //if 1 they want a deposit
    //else if(input==2) withdraw(account, amount);  //if 2 they want a deposit
    else if(input==0) close();                      //if 0 end the transaction 
    else {
        System.out.println("That is not a valid option, try again.");   //if a wrong character is entered, restart
        option();
    }
}

public static void deposit(String account, String amount) throws FileNotFoundException {
    int i=-2;
    Scanner file = new Scanner(new File("info.txt"));
    String line = file.nextLine();
    String[] tokens = line.split(" ");
    do {
        i++;
        i++;
    }
    while (account!=tokens[i]);

    int ballance = Integer.parseInt(tokens[i]);
    int deposit = Integer.parseInt(amount);
    int updated = (ballance+deposit);
    tokens[i] = Integer.toString(updated);

    System.out.println("Your new ballance is $"+tokens[i]);
    PrintWriter fileOut = new PrintWriter("info.txt");
    fileOut.close();
    file.close();
}

/*public static void withdraw(String account, String amount) throws FileNotFoundException {
    int i=0;
    Scanner file = new Scanner(new File("info.txt"));
    String line = file.nextLine();
    String[] tokens = line.split(" ");
    if (account==tokens[i]){
        i++;
        int ballance = Integer.parseInt(tokens[i]);
        int deposit = Integer.parseInt(amount);
        int updated = (ballance+deposit);
        tokens[i] = Integer.toString(updated);
        } 
    System.out.println("withdraw"); 
}
*/
public static void close(){
    System.out.println("Thank You for banking with us!");
    option();
}

}

回答1:


I don't know if I understood you right, but do you mean you don't know how to use try/catch construct?

Best way is to read tutorial:

https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html

Fast addition to your code:

public static void deposit(String account, String amount) throws FileNotFoundException {
int i=-2;
try{
Scanner file = new Scanner(new File("info.txt"));
String line = file.nextLine();
String[] tokens = line.split(" ");
do {
    i++;
    i++;
}
while (account!=tokens[i]);

int ballance = Integer.parseInt(tokens[i]);
int deposit = Integer.parseInt(amount);
int updated = (ballance+deposit);
tokens[i] = Integer.toString(updated);

System.out.println("Your new ballance is $"+tokens[i]);
PrintWriter fileOut = new PrintWriter("info.txt");
} catch(FileNotFoundException e){
  //handling code
}
finally{
fileOut.close();
file.close();

} }




回答2:


When it comes to Exceptions there are 2 ways you can handle it. One is to throw the exception like you have done (after you add the throws to every calling method up to main). This stops any code done after.

Or you can catch the exception and handle it yourself. This allows code after to still be called.

As an example I have used your deposit method that reads the File to throw and catch an exception. The catch is called first since it allows code to be called after. The output is also below.

public static void main(String[] args) throws FileNotFoundException {
    catchDeposit("","");
    System.out.println("print after catchDepsoit");
    throwDeposit("","");
    System.out.println("print after throwDepsoit");
}
public static void catchDeposit(String account, String amount) {
    int i=-2;
    try {
        Scanner file = new Scanner(new File("info.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    }
}
public static void throwDeposit(String account, String amount) throws FileNotFoundException{
    int i=-2;
    Scanner file = new Scanner(new File("info.txt"));

output

File not found
print after catchDepsoit
Exception in thread "main" java.io.FileNotFoundException: info.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at Random.throwDeposit(Random.java:25)
    at Random.main(Random.java:10)

Hope this helps.



来源:https://stackoverflow.com/questions/32640133/new-to-file-reading-writing-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!