我在将 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
打印出内容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 匹配并将其写出来,我认为我的问题是数组列表(我应该做一个第三个数组列表)或者只是把所有东西都变成数组,任何建议都可以,干杯!!!!顺便说一句,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();