在嵌套循环中更改静态类变量的值

杰里·康加斯涅米(Jerri Kangasniemi)

我在嵌套循环中更改类变量的值时遇到问题-我不知道为什么。我猜是因为该变量是static但这是一个静态方法,因为它用于从文件中列出系统中的User,所以它必须是静态的(我从main方法调用它是为了将文件读取到TreeMaps)。是否无法从方法内部重写静态类变量?如果有可能-我到底在做什么错?

public class Loan{

protected int noOfLoans;
protected int noOfReturns;
protected User user=new User();
protected static Book book= new Book();
protected Map <Integer, Book> currentLoans=new TreeMap <Integer, Book>();
protected Map <Integer, Book> returned=new TreeMap <Integer, Book>();   
protected static Map<Integer, Loan> loanList=new TreeMap<Integer, Loan>();

public static void main(String[] args){
    readLoans();
}

public static void readLoans(){
    loanList.clear();
    BufferedReader reader = null;
    try {
        reader=new BufferedReader(new FileReader("loans.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String line = null;
    try {
        line = reader.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    while (line!=null) {
        String[] splitOut=line.split("-");
        String[] loan_User=splitOut[0].split(",");
        String[] loan_CurrentLoans=splitOut[2].split(",");
        String[] loan_Returned=splitOut[4].split(",");
        Loan loan = new Loan();
        loan.user.setFirstName(loan_User[0]);
        loan.user.setSurname(loan_User[1]);
        loan.user.setPersonalID(loan_User[2]);
        for (int i = 1; i <= Integer.parseInt(splitOut[1]); i++) {
            book.setName(loan_CurrentLoans[((Integer.parseInt
                    (splitOut[1])-1)*4)]);
            book.setAuthorFirstname(loan_CurrentLoans[((Integer.parseInt
                    (splitOut[1])-1)*4)+1]);
            book.setAuthorSurname(loan_CurrentLoans[((Integer.parseInt
                    (splitOut[1])-1)*4)+2]);
            book.setISBN(loan_CurrentLoans[((Integer.parseInt
                    (splitOut[1])-1)*4)+3]);
            loan.currentLoans.put(i, book);
        }
        for (int i = 1; i <= Integer.parseInt(splitOut[3]); i++) {
            book.setName(loan_Returned[((Integer.parseInt
                    (splitOut[3])-1)*4)]);
            book.setAuthorFirstname(loan_Returned[((Integer.parseInt
                    (splitOut[3])-1)*4)+1]);
            book.setAuthorSurname(loan_Returned[((Integer.parseInt
                    (splitOut[3])-1)*4)+2]);
            book.setISBN(loan_Returned[((Integer.parseInt
                    (splitOut[3])-1)*4)+3]);
            loan.returned.put(i, book);
        }
        loan.setNoOfLoans(Integer.parseInt(splitOut[1]));
        loan.setNoOfReturns(Integer.parseInt(splitOut[3]));
        loanList.put(loanList.size()+1, loan);
        try {
            line=reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

这是供参考的输入行:

John,Doe,8012311213-2-a book,Author,Authorson,1234567890123,another book,Author,Authorson,2345678901234-1-a returned book,Author,Authorson,3456789012345

在行上方打印时,我希望得到什么:

Current Loans:
1. a book by Author Authorson (1234567890123)
2. another book by Author Authorson (2345678901234)

Returned Loans:
1. a returned book by Author Authorson (3456789012345)

我现在得到的是:

Current Loans:
1. a book by Author Authorson (1234567890123)
2. a book by Author Authorson (1234567890123)

Returned Loans:
1. a book by Author Authorson (1234567890123)

readLoans();
System.out.println(loanList.get(2).currentLoans.get(1).toString());
System.out.println(loanList.get(2).currentLoans.get(2).toString());

退货

a returned book by Author Authorson (3456789012345)
a returned book by Author Authorson (3456789012345)

这使我相信我实际上不能创建静态Book对象的实例,而必须使其成为非静态对象,并尝试在方法中创建该对象的实例。如果是这样-我该怎么做?

杰里·康加斯涅米(Jerri Kangasniemi)

我是如何解决的;

public static void readLoans(){
    // Reads the bookList and userList.
    readBooks();
    readUsers();
    // Creates a new BufferedReader and tries to read "loans.txt"
    BufferedReader reader = null;
    try {
        reader=new BufferedReader(new FileReader("loans.txt"));
    }
    // Catches exception if "books.txt" does not exist.
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String line = null;
    // tries to read the first line and interpret it as a String.
    try {
        line = reader.readLine();
    } 
    // Catches IOexception if any is thrown when trying to read line.
    catch (IOException e) {
        e.printStackTrace();
    }
    // Loop as long as "line" is not empty, i.e. as long as a Loan is read.
    while (line!=null) {
        // split the String "line" at every RegEx "-"
        String[] splitOut=line.split("-");
        // Create a String from the first index of the first split.
        String user = splitOut[0];
        /* Split the second and third index of the first split and create
         * new Stringarrays from them.*/
        String[] loans = splitOut[1].split(",");
        String[] returns = splitOut[2].split(",");
        User aUser = new User();
        /* Find the user in the userList whose personal ID matches the 
         * String "user" that we created. This is the user that we want to
         * create (a) loan/s and/or (a) returned loan/s for.*/
        for (int i = 1; i < userList.size()+1; i++) {
            if (userList.get(i).getPersonalID().equals(user)) {
                /*Set the variables for the User.*/
                aUser.setFirstname(userList.get(i).getFirstname());
                aUser.setSurname(userList.get(i).getSurname());
                aUser.setPersonalID(userList.get(i).getPersonalID());
                aUser.setTelephone(userList.get(i).getTelephone());
                aUser.setLoans(userList.get(i).getLoans());
                aUser.setReturns(userList.get(i).getReturns());
                // Create an ArrayList for Loans and Returns for every user
                ArrayList<Loan> listOfloans = new ArrayList<Loan>();
                ArrayList<Loan> listOfreturns = new ArrayList<Loan>();
                // if the new user has any loans...
                for (int j = 0; j < aUser.getLoans(); j++) {
                    for (int k = 1; k < bookList.size()+1; k++) {
                        /* ... find the "Book" object with the
                         * corresponding ISBN...*/
                        if (bookList.get(k).getIsbn().equals(loans[j*3])) {
                            // ...then create a new loan object for each...
                            Loan loan = new Loan();
                            // ...and set the variables of each loan...
                            loan.setTitle(bookList.get(k).getTitle());
                            loan.setAuthor_firstname(bookList.get(k).
                                    getAuthor_firstname());
                            loan.setAuthor_surname(bookList.get(k).
                                    getAuthor_surname());
                            try {
                                loan.setIsbn(bookList.get(k).getIsbn());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            loan.setMaxLoan(bookList.get(k).getMaxLoan());
                            loan.setOnLoan(bookList.get(k).getOnLoan());
                            loan.setAvailable(bookList.get(k).
                                    getAvailable());
                            loan.setSignature(loans[j*3+1]);
                            loan.setTimestamp(loans[j*3+2]);
                            /* ...then add each one to the "listOfloans"
                             * ArrayList.*/
                            listOfloans.add(loan);
                        }
                    }
                }
                /* if the "listOfloans" ArrayList is not empty, 
                 * add the loan to loanList with User as Key.*/
                if (!listOfloans.isEmpty()) {
                    loanList.put(aUser, listOfloans);
                }
                // if the new user has any returned loans...
                for (int j = 0; j < aUser.getReturns(); j++) {
                    for (int k = 1; k < bookList.size()+1; k++) {
                        /* ... find the "Book" object with the
                         * corresponding ISBN...*/
                        if(bookList.get(k).getIsbn().equals(returns[j*4])){
                            // ...then create a new loan object for each...
                            Loan loan = new Loan();
                            // ...and set the variables of each loan...
                            loan.setTitle(bookList.get(k).getTitle());
                            loan.setAuthor_firstname(bookList.get(k).
                                    getAuthor_firstname());
                            loan.setAuthor_surname(bookList.get(k).
                                    getAuthor_surname());
                            try {
                                loan.setIsbn(bookList.get(k).getIsbn());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            loan.setMaxLoan(bookList.get(k).getMaxLoan());
                            loan.setOnLoan(bookList.get(k).getOnLoan());
                            loan.setAvailable(bookList.get(k)
                                    .getAvailable());
                            loan.setSignature(returns[j*4+1]);
                            loan.setTimestamp(returns[j*4+2]);
                            loan.setReturndate(returns[j*4+3]);
                            /* ...then add each one to the "listOfreturns"
                             * ArrayList.*/
                            listOfreturns.add(loan);
                        }
                    }
                }
                /* if the "listOfreturns" ArrayList is not empty, 
                 * add the returned loan to returnList with User as Key.*/
                if (!listOfreturns.isEmpty()) {
                    returnList.put(aUser, listOfreturns);   
                }
            }
        }
        // tries to read the next line and interpret it as a String.
        try {
            line=reader.readLine();
        }
        // Catches IOexception if any is thrown when trying to read line.
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    // try to close the BufferedReader.
    try {
        reader.close();
    }
    // Catches IOexception if any is thrown when trying to close.
    catch (IOException e) {
        e.printStackTrace();
    }
}

实例化Book对象以及对象和方法是静态的,这是一个问题。我不得不重写一些主要问题在幕后的方法。感谢您提供的所有帮助!=)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

变量在循环中更改

来自分类Dev

将值存储在嵌套的for循环中的不同变量中

来自分类Dev

在嵌套循环中将变量重置为初始值

来自分类Dev

循环中的 C 变量更改

来自分类Dev

JS for循环中的局部变量到全局变量,循环结束后更改值

来自分类Dev

java在for循环中更改值

来自分类Dev

在嵌套循环中查找类 avg

来自分类Dev

在MATLAB的嵌套parfor循环中访问变量

来自分类Dev

循环中的Ansible和嵌套变量

来自分类Dev

如何在for循环中捕获嵌套变量?

来自分类Dev

在双嵌套四循环中组合变量

来自分类Dev

在R中的嵌套for循环中创建变量

来自分类Dev

在while循环中测试嵌套变量

来自分类Dev

嵌套 jQuery 循环中的变量范围

来自分类Dev

Julia:嵌套循环中变量的范围

来自分类Dev

如何从另一个类的静态方法更改静态变量的值

来自分类Dev

如何从另一个类的静态方法更改静态变量的值

来自分类Dev

跳过嵌套循环中的相同值

来自分类Dev

承诺在for循环中更改全局变量

来自分类Dev

如何更新for循环中更改的变量?

来自分类Dev

从C#更改for循环中的arduino变量

来自分类Dev

更改for循环中的变量值?

来自分类Dev

在 shell 脚本循环中更改变量

来自分类Dev

在循环中更改bash变量名称

来自分类Dev

初始化变量并在for循环中更改其值后访问它

来自分类Dev

初始化变量并在for循环中更改其值后访问它

来自分类Dev

Python SQLite。LIMIT 变量不会在循环中更改值

来自分类Dev

在嵌套的for循环中使用变量/动态条件变量

来自分类Dev

在for循环中使用变量的存储值