Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Financial tsunami- Exercise_8_17.

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Financial tsunami- Exercise_8_17.

    (Financial tsunami) Banks lend money to each other. In tough economic times,
    if a bank goes bankrupt, it may not be able to pay back the loan. A bank’s
    total assets are its current balance plus its loans to other banks. The diagram in
    Figure 8.8 shows five banks. The banks’ current balances are 25, 125, 175, 75,
    and 181 million dollars, respectively. The directed edge from node 1 to node 2
    indicates that bank 1 lends 40 million dollars to bank 2.

    If a bank’s total assets are under a certain limit, the bank is unsafe. The money it
    borrowed cannot be returned to the lender, and the lender cannot count the loan in
    its total assets. Consequently, the lender may also be unsafe, if its total assets are
    under the limit. Write a program to find all the unsafe banks. Your program reads
    the input as follows. It first reads two integers n and limit, where n indicates the
    number of banks and limit is the minimum total assets for keeping a bank safe. It
    then reads n lines that describe the information for n banks with IDs from 0 to n-1.
    The first number in the line is the bank’s balance, the second number indicates
    the number of banks that borrowed money from the bank, and the rest are pairs
    of two numbers. Each pair describes a borrower. The first number in the pair
    is the borrower’s ID and the second is the amount borrowed. For example, the
    input for the five banks in Figure 8.8 is as follows (note that the limit is 201):

    5 201
    25 2 1 100.5 4 320.5
    125 2 2 40 3 85
    175 2 0 125 3 75
    75 1 0 125
    181 1 2 125

    The total assets of bank 3 are (75 + 125), which is under 201, so bank 3 is
    unsafe. After bank 3 becomes unsafe, the total assets of bank 1 fall below
    (125 + 40). Thus, bank 1 is also unsafe. The output of the program should be
    Unsafe banks are 3 1

    (Hint: Use a two-dimensional array borrowers to represent loans.
    borrowers[i][j] indicates the loan that bank i loans to bank j. Once bank j
    becomes unsafe, borrowers[i][j] should be set to 0.)





    my problem is in this part of the code, i keep getting out of bound index but it clearly should be fine (on "j" btw!), i add a <= but it gets 3 now instead of 2 before giving me an out of bounds (i was testing around)...

    for(int i = 0; i <= 0; i++) {
          int length = borrow[i];          //Use borrow here :
          bankBorrowed[i] = new double[length];
          System.out.println(length); //Testing
          for(int j = 0; j < length; j++) {
            System.out.print("Enter the bank: "); //Useless line of code
            int bank = input.nextInt(); //Useless line of code
            System.out.print("Enter the amount: ");
            double amount = input.nextDouble();
            bankBorrowed[i][j] = amount;
          }
        }


    import java.util.*;
    //Making code look good ;)
    public class Exercise_8_17 {
      public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in);
     
        System.out.print("Enter the number of banks: ");                                //Number Of banks
        int numberOfBanks = input.nextInt();  
     
        System.out.print("Enter the limit: ");
        double limit = input.nextDouble();
     
        double[][] bankBorrowed = new double[numberOfBanks][];            //Initializing the matrix to get which bank loaned and how much.
        double[] bankCurrentBalance = new double[numberOfBanks];
     
        for(int i = 0; i < numberOfBanks; i++) {                                    //getting current value for each bank from 0 to n-1
          System.out.print("Enter current balance for bank " + i + ": ");
          bankCurrentBalance[i] = input.nextDouble();
        }
     
        int[] borrow = new int[numberOfBanks];
     
        for(int i = 0; i < numberOfBanks; i++) {                                     //Using this to store the number of borrowers for each bank to use for (for loop).
          System.out.print("How many banks borrowed from bank " + i + ": ");
          borrow[i] = input.nextInt();
        }
     
        for(int i = 0; i <= 0; i++) {
          int length = borrow[i];          //Use borrow here :
          bankBorrowed[i] = new double[length];
          System.out.println(length); //Testing
          for(int j = 0; j < length; j++) {
            System.out.print("Enter the bank: "); //Useless line of code
            int bank = input.nextInt(); //Useless line of code
            System.out.print("Enter the amount: ");
            double amount = input.nextDouble();
            bankBorrowed[i][j] = amount;
          }
        }
     
        double[] total = getTotal(numberOfBanks, bankCurrentBalance, bankBorrowed, borrow); //Method ;)
        boolean[] bruh = isTrue(limit, total, numberOfBanks); //Method ;)
     
        System.out.print("The banks unsafe are: ");
        for(int i = 0; i < numberOfBanks; i++) {
          if(bruh[i] == false) {
            System.out.print(i + " ");
          }
        }
     
      }
     
      public static double[] getTotal(int numberOfBanks, double[] bankCurrentBalance, double[][] bankBorrowed, int[] borrow) {    //Getting the total
        double[] total = new double[numberOfBanks];
     
        for(int i = 0; i < numberOfBanks; i++) {
          total[i] += bankCurrentBalance[i];
          for(int j = 0; j < borrow.length; j++) {
            total[i] += bankBorrowed[i][j];
          }
        }
        return total;
      }
     
      public static boolean[] isTrue(double limit, double[] total, int numberOfBanks) {
        boolean[] bruh = new boolean[numberOfBanks];
     
        for(int i = 0; i < numberOfBanks; i++) {
          if(total[i] < limit) {
            bruh[i] = false;
          } else {
            bruh[i] = true;
          }
        }
        return bruh;
      }
    }


    --- Update ---

    I fixed the mistake at getTotal() method from j < borrow.length to j < length.

    Ofcourse i initialized the value length everytime in the outer loop with int length = borrow[i];

    Now i get this error:

    java.lang.NullPointerException
    at Exercise_8_17.getTotal(Exercise_8_17.java:61)
    at Exercise_8_17.main(Exercise_8_17.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:267)
    Last edited by UniverseCloud; November 5th, 2019 at 10:05 AM. Reason: Clarification

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Financial tsunami- Exercise_8_17.

    java.lang.NullPointerException
    at Exercise_8_17.getTotal(Exercise_8_17.java:61)
    There was a variable with a null value when line 61 was executed. Look at line 61 to see what variables were used and determine which one was null;
    If you can not tell by looking at the code, add a print statement just before line 61 that prints the values of each of the variables used on line 61.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    UniverseCloud (November 6th, 2019)

  4. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Financial tsunami- Exercise_8_17.

    i just found it x-x..

    problem was in the outer loop x-x(from i >= 0 to i < 5), i just realized that the program was actually just running the inner loop once and leaving the other array values as null x-x.

    it was so obvious that i missed it >.> but im just happy now lol thanks.

    for(int i = 0; i < 5; i++) {
          int length = borrow[i];
          bankBorrowed[i] = new double[borrow[i]];
          System.out.println(length); //Testing
          for(int j = 0; j < length; j++) {
            System.out.print("Enter the bank: "); //Useless line of code
            int bank = input.nextInt(); //Useless line of code
            System.out.print("Enter the amount: ");
            bankBorrowed[i][j] = input.nextDouble();
          }
        }