8 Replies Latest reply: Mar 15, 2013 8:21 AM by 996672 RSS

    Trouble reading from file

    996672
      Hello, I am hoping that someone will be able to assist me in working out what is wrong with some code I am working on.

      Here is what I have so far:
      // This program reads ouput from a file while calculating the monthly fee and amount deposited in a persons bank account.

      import java.io.*;
      import java.util.Scanner;

      public class bank
      {
      public static void main(String[] args) throws FileNotFoundException
      {
      Double amountInAccount;
      String line;

      File file = new File("AcctInfo.txt");
      Scanner input = new Scanner(file);

      while (input.hasNext())
      {
      line = input.nextLine();
      System.out.println(line);

      amountInAccount = Double.parseDouble(line);
      System.out.println("You have" + " " + amountInAccount);
      System.out.print("So," + " ");

      if (amountInAccount < 1500)
      {
      System.out.print("You owe $5 in monthly fees");
      }
      else if (amountInAccount >= 1500)
      {
      System.out.print("You owe $0 in monthly fees");
      }
      else if (amountInAccount < 1000)
      {
      System.out.print("You owe $10 in monthly fees");
      }
      else if (amountInAccount >= 1000)
      {
      System.out.print("You owe $0 in monthly fees");
      }
      else if (amountInAccount < 25000)
      {
      System.out.print("You owe $30 in monthly fees");
      }
      else if (amountInAccount >= 25000)
      {
      System.out.print("You owe $0 in monthly fees");
      }
      else
      {
      System.out.print("You owe $0 in monthly fees");
      }
      } // End While

      input.close();

      } // End main

      } // End class

      What I need to do is print the information in the file and then tell the "customer" how much they owe in monthly fees for the types of account they have.

      When I just have the program print the info in the file, it's works fine, but it starts to have problems when I had the if-else, so I know that is the issue.

      Hope that someone will be able to help me, and to hear from them soon. Thank You.

      Also here is a link to the file, in case you would like to see it: https://www.dropbox.com/s/b1qr9m5br9vvy4l/AcctInfo.txt
        • 1. Re: Trouble reading from file
          J2EE_Life
          amountInAccount = Double.parseDouble(line);
          Not every line you read from .txt can be converted to Double ( you have text also written there) .
          • 2. Re: Trouble reading from file
            996672
            Ok, yes that makes sense. In that case what else should it be? A byte maybe? Though, if I think about it; would that be too small to hold all the information? Or, do I need to separate calls so that the program will catch all of the information, the double and maybe something like a string?
            • 3. Re: Trouble reading from file
              J2EE_Life
              String line;
              line = input.nextLine();
              In that case what else should it be? A byte maybe? Though, if I think about it; would that be too small to hold all the information?
              So u are reading in String and that's fine.
              All you need is a conditional check to find whether the read line is a number/amount and if yes, then use amountInAccount = Double.parseDouble(line);

              As I see your .txt file, I find the amount is in 5th(31000.35),11th(345.89),17th(1235.23) line, etc i.e when you read 5th line and every next 6th line you can parse to double.
              I think with these inputs you can complete the program now.
              • 4. Re: Trouble reading from file
                996672
                J2EE Life,

                Thank you for your assistance, I only have one last question and then I will have completed what I was trying to accomplish with this code.

                The code I have is here: http://textuploader.com/?p=6&id=kagZ

                My question is, in bankMethod() when I am trying to call a total balance for the account, how would I do this? As you can see, I thought that I had to subtract amountInAccount by the findMonthlyFee method; though I see now that this will not work.
                • 5. Re: Trouble reading from file
                  J2EE_Life
                  Well that's easy.
                  private String findMonthlyFee()
                  make it to return double
                  return "$10";
                  In the method findMonthlyFee(), chnage return statements so that it returns double i.e. in this case 10 and not "$10".
                  I can see why you are returning "$10" instead of just 10 because you use that method as below :
                  System.out.println("You owe " + findMonthlyFee() + " in monthly fees.\n"); // calls method findMonthlyFee.
                  Change that as:
                  System.out.println("You owe $" + findMonthlyFee() + " in monthly fees.\n");
                  int totalBalance;
                  Also make totalBalance as double.
                  • 6. Re: Trouble reading from file
                    996672
                    Hopefully, this will be the last question I have for this program.

                    Your suggestions all worked very well, Thank You. Though, now when I run the program the total balance calculates as being $-10.0 for all accounts, I think it's trying to calculate the balance by subtracting amountInAccount by all variables located in the findMonthlyFee method. To fix this issue, do I need to call an argument in findMonthlyFee() so that the program will not try to calculate the total balance by using all variables in the method?
                    • 7. Re: Trouble reading from file
                      J2EE_Life
                      Your suggestions all worked very well, Thank You.
                      You are welcome. Just keep learning. If you mark my answers helpful/correct that would be great. Thanks

                      The trick is to shift following line at appropriate place. Currently you are calculating totalBalance once at the top of bankMethod(). But what you need is to calculate this for each record.
                      totalBalance = amountInAccount - findMonthlyFee();
                      You need to keep above statement in switch case as below (see the comment):
                       case 5:
                                              try
                                              { 
                                                  amountInAccount = Double.parseDouble(line); 
                                         totalBalance = amountInAccount - findMonthlyFee();  // YOU NEED IT HERE. REMOVE THE CALCULATION FROM ABOVE AND PUT IT HERE ONLY.
                                                  System.out.println("You have: $" + amountInAccount);
                                                  System.out.println("You owe $" + findMonthlyFee() + " in monthly fees.\n"); // calls method findMonthlyFee.
                                                  System.out.println("The total balance is $ " + totalBalance + "for your account.\n"); // Finds the balance of the customers account after monthly fees have been taken out.
                                              } catch(Exception e) { e.printStackTrace(); }
                                              break;
                      • 8. Re: Trouble reading from file
                        996672
                        Ok, yeah that makes a lot more sense.

                        Once again, thank you for your help!