0

ISBN 番号を本のタイトルの名前に変更するのに問題があります

ISBN コード、著者、タイトル、残りの本のレベルの印刷開始:

0201403765 Jan Skansholm    Ada 95 from the Beginning   100
0202535665 M. Ben-Ari           Software Engineering            25
034565976X Michael Feldman  Program Construction            12
080539057X M.A. Weiss           Data Structures             30
0805645782 Ken Arnold           Java for Programmers            10
0905297568 A. Badone            Chaos Theory                    15

whats in を出力しtransactions.txtます:

0201403765     -55
0201403765       2
0202535665      10
0202535665     -28
034565976X      -7
080539057X     -15
0905297568      13
0905297568      -5

したがって、基本的に必要なことは、次のように、ISBN が一致する場合は本のタイトルに変更することです。

Java from the Beginning -55
Java from the Beginning   2
Software Engineering     10
Software Engineering    -28
Program Construction     -7
Data Structures -15
Chaos Theory     13
Chaos Theory     -5

私が抱えている問題は、以下のコードでマークされた 1 にあります。isbn が一致するかどうかを確認する方法が本当にわかりません。そうであれば、どのタイトルが isbn と一致するかを確認して書き出す方法がわかりません。私の問題は配列リストだと思います ( 3番目のarraylist)またはすべてを配列にするだけで、どんなアドバイスでも構いません、乾杯!!!! ところで1は完全にばかげて間違っています....

import java.util.*;  
import java.io.*;
class inventory{ 
    static void intial(){
        try{
            RandomAccessFile in = new RandomAccessFile("books.dat","r");
            ArrayList<String> list1=new ArrayList<String>();
            String author ,title , isbn;
            int level=0;
            while(in.getFilePointer()<in.length()){
                author = in.readUTF();  // author, at most 20 characters
                title = in.readUTF();   // title, at most 40 characters
                isbn = in.readUTF();    // ISBN
                level = in.readInt();   // level, i.e. copies in stock (>=0)
            //System.out.printf("%5d", isbn+author+title+level);
            System.out.println(isbn+" "+author+" "+title+" "+level);
            list1.add(title);
            list1.add(isbn);
            //list1.add(level);
        }
        in.close();
        System.out.println(" ");
        String isbn2;
        int level2=0;
        //try{
            Scanner out = new Scanner(new File ("transactions.txt"));
            ArrayList<String> list2=new ArrayList<String>();
            while(out.hasNextLine()){
                isbn2 = out.next();
                level2 = out.nextInt();
                System.out.println(isbn2 +" "+level2);
                list2.add(isbn2);
                //list2.add(level2);
            }
            out.close();

     1)     for (isbn: list1){
            for(isbn2: list2){
                if(isbn.contains(isbn2)){
                    System.out.println(title+" "+level);
                }
            }   
        }
        }
        catch(IOException f){
            System.out.println("file error");
            f.printStackTrace();
        }
    }

}
class BookShop{
    public static void main(String[]args){
        inventory x = new inventory();
        x.intial();
4

2 に答える 2

3

次のように、各本を保持するオブジェクトを作成します。

class Book {
    public final String isbn;
    public final String title;
    public final String author;
    public final int level;

    public Book(String isbn, String title, String author, int level) {
        this.isbn = isbn; this.title = title; this.author = author; this.level = level;
    }
}

ArrayList に Book オブジェクトを設定します。そうすれば、並列配列を必要とするのではなく、1 つの Book のすべてが 1 か所にあります。

(私がこのクラスを書いた方法は不変です。なぜなら、オブジェクトが入力ファイルから読み込まれるとオブジェクトを更新する理由がないからです。これが Python や Haskell の場合、タプルを使用するだけで済みます。セレモニーですが、Java ではそのオプションがありません。)

于 2012-04-23T19:38:52.487 に答える
3

まず、本のデータを保存するための Book オブジェクトを作成します。

public Book{
    private String ISBN ="";
    private String title="";
    private String author="";
    private int level;
    public Book(String ISBN, String title,String author, int level){
        this.ISBN=ISBN;
        this.title=title;
        this.author=author;
        this.level=level;
    }
    public String getTitle(){
        return title;
    }
    public String getISBN(){
        return ISBN;
    }
    public String getAuthor(){
        return author;
    }
    public int getLevel(){
        return level;
    }
}

Book オブジェクトを ArrayList に追加します。

ArrayList<Book> bkList = new ArrayList<Book>();
bkList.add(new Book('ISBN','Title','Author'));

get() メソッドで Book データを取得します。

Book tempBook;
for (int x=0;x<bkList.Size();x++){
    tempBook=bkList.get(x);
    System.out.println(tempBook.getTitle()+" "+tempBook.getLevel());
}
于 2012-04-23T19:41:00.387 に答える