2017-09-25 81 views
-1

這個程序提示商店所有者在一天的開始和結束時的現金數量以及文件的名稱。它應該檢查一天結束時的實際現金量是否等於預期值。掃描儀輸入未定義爲字符串? JAVA

我有一個txt文件,其中每行包含三個項目:發票號碼,現金金額和字母P如果支付金額或R如果收到。項目由空格分隔。

我有我的代碼

while (filename.hasNext()) { 
    invoice.add(filename.nextInt()); 
    cash.add(filename.nextDouble()); 
    PR.add(filename.next()); 
} 

這片麻煩它是一個語法錯誤,說我不能使用.nextInt() .nextDouble() .next() hasNext()

下面是完整的源代碼:

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

/** 
* Code for P11.11. This program takes in a list of baby names and outputs a list of boys and 
* girls names. 
* 
* @Michael Goedken 
*/ 
public class BalanceTransactions { 
    public static void main(String[] args) throws FileNotFoundException { 
     // The following lines ask the user to enter the file name and the balances at the beginning and the end 
     Scanner in = new Scanner(System.in); 

     System.out.print("Please enter the file name for input: "); 
     String filename = in.next(); 

     System.out.print("Please enter the cash amount at the start: "); 
     double start = in.nextDouble(); 

     System.out.print("Please enter the cash amount at the end: "); 
     double end = in.nextDouble(); 

     ArrayList<Integer> invoice = new ArrayList<Integer>(); 
     ArrayList<Double> cash = new ArrayList<Double>(); 
     ArrayList<String> PR = new ArrayList<String>(); 

     while (filename.hasNext()) { 
      invoice.add(filename.nextInt()); 
      cash.add(filename.nextDouble()); 
      PR.add(filename.next()); 
     } 

     for (int i = 0; i < invoice.size(); i++) { 
      if (PR.get(i).equals("P")) { 
       start -= cash.get(i); 
      } 
      if (PR.get(i).equals("R")) { 
       start += cash.get(i); 
      } 
     } 

     if (start == end || (double) Math.round(start * 1000000000)/1000000000 == end) { 
      System.out.println("There is the correct amount in the register."); 

     } else { 
      Double difference = ((double) Math.round(start * 100000)/100000) - end; 

      System.out.println("You are off by: " + difference); 
     } 
    } 
} 

編輯:我現在增加了一個新的掃描儀,我得到這個錯誤

Scanner fileScanner = new Scanner("/Users/MichaelGoedken/Desktop/transactions.txt"); 

     while (fileScanner.hasNext()) 
     { 
      invoice.add(fileScanner.nextInt()); 
      cash.add(fileScanner.nextDouble()); 
      PR.add(fileScanner.next()); 
     } 

錯誤:

Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Scanner.java:864) 
    at java.util.Scanner.next(Scanner.java:1485) 
    at java.util.Scanner.nextInt(Scanner.java:2117) 
    at java.util.Scanner.nextInt(Scanner.java:2076) 
    at BalanceTransactions.main(BalanceTransactions.java:34) 
+2

你認爲'filename.nextInt()'應該做什麼?你爲什麼這麼認爲? –

+2

因爲''filename''是一個''String''變量。這些方法由一個''Scanner''實例提供,在你的代碼中這個變量被稱爲''in''。 – f1sh

+0

@SotiriosDelimanolis我希望它會填充文本文件中的整數數組列表 –

回答

2

如果你要掃描的文件,嘗試使用此構造函數:

/** 
* Constructs a new Scanner that produces values scanned 
* from the specified file. Bytes from the file are converted into 
* characters using the specified charset. 
* 
* @param source A file to be scanned 
* @throws FileNotFoundException if source is not found 
*/ 
public Scanner(File source) throws FileNotFoundException { 
    this((ReadableByteChannel)(new FileInputStream(source).getChannel())); 
} 

現在你正在使用此構造方法,它沒有任何意義,因爲你作爲參數傳遞字符串:

public Scanner(String source) { 
    this(new StringReader(source), WHITESPACE_PATTERN); 
} 
+0

作爲「掃描儀」類的參數我要說些什麼?你只是把文件源,我試圖在那裏添加路徑,但看起來不正確 –

+0

@MikeGoedken你是什麼意思的「看起來不正確」?另外,不要只描述你的代碼,顯示它以避免誤解。 – Pshemo

+0

'公共掃描儀(?)爲此行拋出FileNotFoundException'我該如何添加到類的參數? –