2017-09-25 17 views
-1

このプログラムは、店舗の所有者に対し、1日の終わりと終わりの現金の金額とファイルの名前を入力します。 1日の終わりの現金の金額が期待値と等しいかどうかを確認する必要があります。文字列のスキャナ入力が未定義ですか? JAVA

私は、各行に3つの項目が含まれています:請求書番号、現金額、支払い金額の場合はP、受け取った場合はRです。項目はスペースで区切られます。

私はそれは私が.nextInt() .nextDouble() .next() hasNext()

ここでは、完全なソースコードで使用することはできませんと言って、構文エラーである私のコードでこの作品とのトラブル

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

を持っています:

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

EDITを:新しいスキャナを追加しました。このエラーが発生しました。

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

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

ERROR:

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

'Scanner'クラスのパラメータは何にしますか?あなたはファイルのソースを置くだけで、そこにパスを追加しようとしましたが、それは正しく見えません。 –

+0

@MikeGoedken "見栄えが悪い"とはどういう意味ですか?また、コードを記述するだけでなく、誤解を避けるために表示してください。 – Pshemo

+0

'public Sc​​anner(?)はこの行に対してFileNotFoundExceptionをスローするクラスのパラメータに何を追加しますか? –

関連する問題