Java读/写文件的新手 [英] New to file reading/writing Java

查看:128
本文介绍了Java读/写文件的新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个银行程序,该程序从.txt文件读取帐户信息.只是要问您要做什么,获取您的帐号,找出要提取/存入的金额,然后在文本文件上进行更改.

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.

我在FileNotFoundException方面遇到了一些麻烦.我以前从未使用过它,并且不确定如何捕获"它.我进行了一些搜索,找到了一些对我来说没有意义的答案,例如有关"try"语句和我不打算使用的FileReader的信息.我不知道他们在做什么或去哪里.还有您可以给我的指导? (请原谅半熟的代码,在解决此问题之前,我无法运行它以查看其工作原理)

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();
}

}

推荐答案

我不知道我是否理解正确,但是您是否表示不知道如何使用try/catch构造?

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

快速添加代码:

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();

} }

这篇关于Java读/写文件的新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆