2017-03-03 27 views
-2

首先請原諒我的錯誤輸入錯誤和張貼錯誤,因爲我是新手,並且仍在學習如何使用它。無法讀取Java文件中的數字和值?

我應該創建一個代碼,詢問用戶他是否想要存入或提取,然後要求他輸入文件名,然後使用裏面的信息添加到帳戶;無論如何,這段代碼大約有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++; 
      } 

     } 
    } 

} 

順便說一句,如果你告訴我如何包含代碼的前幾行使用計算器正確

+0

你認爲哪一行應該從文件中讀取? – shmosel

+0

@shmosel感謝您的快速回復。該文件由雙數數字組成,可以看作每行中的數字。所以代碼必須讀取所有行 –

+0

關於你的[即將被刪除的問題](http://stackoverflow.com/questions/42775060/how-to-read-a-txt-file-in-java-and -store-its-values?noredirect = 1),你已經在這裏問過這個問題並得到了一個*詳細的答案。對不起,直言不諱,但請付出一些努力學習Java。 –

回答

0

我在猜測您想要讀取該文件,但是您並未在程序中的任何位置執行此操作。此行File depositFile = new File(depositAmount);實際上並不讀取文件,它只創建一個File類對象。我強烈建議你google 如何閱讀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(); 
       } 
      } 
     } 
    } 
} 

} 
+0

很好的回覆,非常感謝我的朋友 –

0

如果你希望你的循環掃描的文件,而不是標準輸入將是非常有益的,您需要先將掃描儀指向文件:

deposit = new Scanner(depositFile); 
+0

但我已經有了可變存款的掃描儀;掃描儀存款=新掃描儀(System.in); –

+0

@AmmarA但它不掃描文件,除非你創建一個新的掃描儀。 – shmosel

+0

這是否意味着我應該刪除system.in並添加文件名呢?或者我應該創建一個新的掃描儀變量? –