2016-04-12 9 views
0

入力ファイルから読み取った数量に基づいて、書籍の総数を計算しようとしています。私は書籍の総数を計算しようとしましたが、今では私のコードはファイルから読み込んだ最後の数量をtotalBooksの合計として生成しています。私は何が間違っているのか分かりませんが、どんな指導も大変ありがとうございます。ここでJava Total Books Calculation

は私が持っているものです...

注:私は修正しようとしている私のプログラムの一部は、コメントの下にある//計算合計ブックス

import java.util.Scanner; 
import java.io.File; 
import java.io.IOException; 

public class BookstoreInventory 
{ 
public static void main(String[] args)throws IOException 
{ 
//Vaiable declartions 
int edition, quanity; 
double pricePerBook; 
String isbn, author, title, publisherCode; 

//Open the file and set delimiters 
File file = new File("inventory.txt"); 
Scanner inputFile = new Scanner(file); 
inputFile.useDelimiter("_|/|\\r?\\n"); 

//read from the file 
while (inputFile.hasNext()) 
{ 
    isbn = inputFile.next(); 
    System.out.println(formatISBN(isbn)); //Need to move at end 
    author = inputFile.next(); 
    System.out.println(getLastName(author)); //need to remove at end 
    title = inputFile.next(); 
    System.out.println(truncateTitle(title)); //need to remove at end 
    edition = inputFile.nextInt(); 
    System.out.println(edition(edition)); //need to remove at end 
    publisherCode = inputFile.next(); 
    System.out.println(publisher(publisherCode)); //need to remove at end 
    quanity = inputFile.nextInt(); 
    System.out.println(quanity);  //need to remove at end 
    pricePerBook = inputFile.nextDouble(); 
    System.out.println("$" + pricePerBook); //need to remove at end 

    //Calculate Total Books 
    int totalBooks = 0; 
    totalBooks += quanity; 
    System.out.println("Total books: " + totalBooks); 
} 

//Close the flie 
inputFile.close(); 
} 

答えて

0

あなたは使用する必要がありますquantity+= inputFile.nextInt();

交換する代わりに、次の数量を追加して以前の数量を更新する必要があるため、総量を計算する必要があります

関連する問題