Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在Java中读取文件中的数字和值?_Java_File Io - Fatal编程技术网

Can';在Java中读取文件中的数字和值?

Can';在Java中读取文件中的数字和值?,java,file-io,Java,File Io,首先,请原谅我的打字错误和发帖错误,因为我是这个网站的新手,还在学习如何使用它 我应该创建一个代码,询问用户是否要存款或取款,然后要求他输入文件名,然后使用其中的信息添加到帐户中;无论如何,这段代码大约完成了25%,到目前为止,我在测试代码时遇到了问题,因为在我输入文件名后,当它询问我时,它似乎没有读取文件名,你知道这可能是什么原因吗?有什么建议或反馈吗 import java.io.*; import java.util.*; public class read { public

首先,请原谅我的打字错误和发帖错误,因为我是这个网站的新手,还在学习如何使用它

我应该创建一个代码,询问用户是否要存款或取款,然后要求他输入文件名,然后使用其中的信息添加到帐户中;无论如何,这段代码大约完成了25%,到目前为止,我在测试代码时遇到了问题,因为在我输入文件名后,当它询问我时,它似乎没有读取文件名,你知道这可能是什么原因吗?有什么建议或反馈吗

import java.io.*;
import java.util.*;

public class read {

    public static void main(String [] args)  throws FileNotFoundException {
        Double currentbalance = 500.00;
        System.out.println("To deposit enter 1");
        System.out.println("To Withdrwawl enter 2");
        Scanner depositOrWithdrawl = new Scanner(System.in);
        int choose = depositOrWithdrawl.nextInt();
        if(choose == 1) {

            double currentBalance = 500.00;
            System.out.println("your current balance is: $"+ currentBalance);
            System.out.println("Please enter the file name to deposit: ");
            Scanner deposit = new Scanner(System.in);
            String depositAmount = deposit.nextLine();
            File depositFile = new File(depositAmount);
            int nextLine = 1;


            while(deposit.hasNextLine()){
                double numbers = deposit.nextDouble();
                System.out.println("You are about to add: $"+ numbers + " To your Balance");
                currentBalance = currentBalance + numbers;
                System.out.println("Your current balance is: $" + currentBalance);
                nextLine++;
            }

        }
    }

}

顺便说一下,如果您告诉我如何包含代码的前几行以正确使用stackoverflow,这将非常有帮助。

如果您希望循环扫描文件而不是stdin,则需要先将扫描仪指向文件:

deposit = new Scanner(depositFile);

我猜您想读取该文件,但在程序的任何地方都没有这样做。此行<代码>文件存款文件=新文件(存款金额)实际上并不读取文件,它只创建一个
文件
类对象。我强烈建议你用谷歌搜索如何读取java文件。关于这个主题有很多非常好的教程。无论如何,以下代码应该可以帮助您完成您正在尝试执行的操作:-

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
// It is a bad practice to start a Java class name with a lower case character
public class IdentifiableImpl {


public static void main(String[] args) throws FileNotFoundException {
    Double currentbalance = 500.00;
    System.out.println("To deposit enter 1");
    System.out.println("To Withdrwawl enter 2");
    Scanner depositOrWithdrawl = new Scanner(System.in);
    int choose = depositOrWithdrawl.nextInt();
    if (choose == 1) {

        double currentBalance = 500.00;
        System.out.println("your current balance is: $" + currentBalance);
        System.out.println("Please enter the file name to deposit: ");
        // Scanner deposit = new Scanner(System.in);
        String depositAmount = depositOrWithdrawl.nextLine();
        File depositFile = new File(depositAmount);
        //  Create a stream to read from a file
        BufferedReader br = new BufferedReader(new FileReader(depositFile));
        // String st;
        // int nextLine = 1;
        String input;
        // Read a file line by line
        try {
            while ((input = br.readLine()) != null) {
                try {
                    // Convert read line into double
                    double numbers = Double.valueOf(input);
                    System.out.println("You are about to add: $" + numbers + " To your Balance");
                    currentBalance = currentBalance + numbers;
                    System.out.println("Your current balance is: $" + currentBalance);
                    // nextLine++;
                } 
// Handle bad input gracefully
catch (NumberFormatException e) {
                    System.out.println("Seems like there is an invalid amount in input file." + input
                            + " Will not proceed further");
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//Close the stream in finally block
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

}

您认为应该从文件中读取哪一行?@shmosel感谢您的快速回复。该文件由双倍数字组成,双倍数字可以看作是每行中的一个数字。因此,代码必须通读所有行。考虑到您的,您已经在这里提出了这个问题,并得到了详细的答案。很抱歉直言不讳,但请在学习Java上投入一些精力。但我已经有了一个可变存款的扫描仪;扫描仪存放=新扫描仪(System.in)@AmmarA,但除非创建新的扫描仪,否则它不会扫描文件。这是否意味着我应该删除system.in并添加文件名?或者我应该创建一个新的scanner变量?@AmmarA您可以创建一个新的scanner变量。或者你可以在循环之前加上这一行。非常感谢。我在这一部分已经呆了两天了,多亏了你,我把它修好了