2 Replies - 129 Views - Last Post: 06 December 2017 - 08:04 AM Rate Topic: -----

#1 leornungtarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 17

double/int read from file to array

Posted 05 December 2017 - 11:25 PM

Hello all, I'm having some trouble with the below code. The code works as is, but not as needed. As of now I am creating the BeginningBalance.dat file within the code and using that information for the rest of the code, but I need to read an existing BeginningBalance.dat/any named file which contains both integers and doubles instead. Any advice or direction would be much appreciated. Thanks.

import java.util.Scanner;     
import java.io.*;  
           
public class StillWorkingOnIt
{
   public static void main(String[] args) throws IOException
   {
      Scanner keyboard = new Scanner(System.in);
      double[] numbers = {111, 100.00, 200.00, 50.00,
                          222, 200.00, 300.00, 100.00};
      
      PrintWriter inputFile = new PrintWriter("BeginningBalance.dat");
      
      inputFile.printf("%.0f",numbers[0]);
      inputFile.println("\n"+numbers[1]);      
      inputFile.println(numbers[2]);
      inputFile.println(numbers[3]);
      inputFile.printf("%.0f",numbers[4]);
      inputFile.println("\n"+numbers[5]);
      inputFile.println(numbers[6]);
      inputFile.println(numbers[7]);
      inputFile.close();
      
      printInfo();
      firstLine(numbers);
      begBal(numbers);
      financeCharge(numbers);
      purchases(numbers);
      payments(numbers);
      endingBal(numbers);
      listTotals(numbers);
   }
   
   public static void printInfo()
   {
       System.out.println("Cust No\tBeg.Bal.\tFinance Charge"+
                         "\tPurchases"+"  "+"Payments\tEnding Bal.");
   }
   public static void firstLine(double[] numbers)
   {
      double fiCh = .01 * numbers[1];
      double endBal = ((numbers[1]+numbers[2]+fiCh)-numbers[3]);
      System.out.printf("%.0f",numbers[0]);
      System.out.printf("\t\t%.2f",numbers[1]);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",numbers[2]);
      System.out.printf("\t\t%.2f",numbers[3]);
      System.out.printf("\t\t%.2f",endBal);
      System.out.printf("\n%.0f",numbers[4]); 
   }  
   
   public static void begBal(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[5]);
   }
   
   public static void financeCharge(double[] numbers)
   {
      double fiCh = .01;
      System.out.printf("\t\t%.2f",(numbers[5]*fiCh));
   }
   
   public static void purchases(double[] numbers)
   {
      System.out.printf("\t\t\t%.2f",numbers[6]);
   }
   
   public static void payments(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[7]);
   }
   public static void endingBal(double[] numbers)
   {
      double fiCh = numbers[5]*.01;
      double endBal = ((numbers[5]+numbers[6]+fiCh)-numbers[7]);
      System.out.printf("\t%.2f",endBal);
   }
   public static void listTotals(double[] numbers) throws IOException
   {
      double acc1 = numbers[0];
      double acc2 = numbers[4];
      double begBal = numbers[1]+numbers[5];
      double fiCh = ((numbers[1]*.01)+numbers[5]*.01);
      double pur = numbers[2]+numbers[6];
      double pay = numbers[3]+numbers[7];
      double endBalA = ((numbers[1]*.01)+numbers[1]+numbers[2]-numbers[3]);
      double endBalB = ((numbers[5]*.01)+numbers[5]+numbers[6]-numbers[7]);
      double endBalTotal = endBalA + endBalB;
      System.out.print("\n\nTotals");
      System.out.printf("\t%.2f",begBal);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",pur);
      System.out.printf("\t\t%.2f",pay);
      System.out.printf("\t%.2f",endBalTotal);
      
      PrintWriter outputFile = new PrintWriter("NewBalance.dat");
      outputFile.printf("%.0f",acc1);
      outputFile.printf("\n%.2f",endBalA);
      outputFile.printf("\n%.0f",acc2);
      outputFile.printf("\n%.2f",endBalB);
      outputFile.close();
   }
   
}



Is This A Good Question/Topic? 0
  • +

Replies To: double/int read from file to array

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,028
  • Joined: 20-September 08

Re: double/int read from file to array

Posted 06 December 2017 - 03:53 AM

Your question is unclear, as is your code. It's difficult to understand since your naming is wrong. Method names should be verbs.

http://technojeeves....yle-conventions

But i don't really know what the problem is, since you can read your file as doubles, whether a decimal point exists or not

This post has been edited by g00se: 06 December 2017 - 04:02 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: double/int read from file to array

Posted 06 December 2017 - 08:04 AM

Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1