Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default how to multiply numbers in rows and print it next to it

    Here is my program:
    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.StringTokenizer;
    
    public class bill {
    
     public static void main(String[] args) {
       System.out.println("\t<===========================>");
       System.out.println("\tCOMPUTER & ELECTRONICS SHOP");
       System.out.println("\t<===========================>");
       System.out.println("\t   14xx Washington Ave \n  \t      NY 12xxx Saturn");
       // System.out.println("Memo# 458");
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
                java.util.Date date = new java.util.Date();
                String datetime = dateFormat.format(date);
                System.out.println("Memo# 458 \t\t\t\tDate " + datetime);
                       
       System.out.println("**--------------------------------------------------** ");
       System.out.println("SrNo.\tITEMS \tQTY \tRATE \tAMOUNT ");
       System.out.println("**--------------------------------------------------** ");
       try {
       FileInputStream fstream =
        new FileInputStream("C:\\Users\\FK\\Desktop\\bill.txt");
       // Get the object of DataInputStream
       DataInputStream in = new DataInputStream(fstream);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
       String strLine;
       int rows = 0;
       float total = 0f;
       float avg;
       float avg1,sum;
     double gt;
     double tax;
     
     
       //Read File Line By Line
       while ((strLine = br.readLine()) != null) {
        rows++;
        // Print the content on the console
        StringTokenizer Tok = new StringTokenizer(strLine, ",");
        int currentToken = 0;
        int totalTokens = Tok.countTokens();
        while (Tok.hasMoreElements()) {
         currentToken++;
         String tokenValue = Tok.nextElement().toString();
         System.out.print(tokenValue + "\t");
         if(currentToken==totalTokens){
          total+=Float.parseFloat(tokenValue);
         
         }
        }
       System.out.print("\n");
       }
       avg = total/rows;
      // System.out.println("\t 10 Days Moving Average =\t" + avg);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
       
     
        System.out.printf("Total\t\t\t\t$%.2f", total);
        tax=(.08*total);
        System.out.printf("\nTax\t\t\t\t$%.2f", tax);
        gt=tax+total;
        System.out.println("\n-------------------------------------------------- ");
        System.out.printf("Amount to be paid \t\t\t$%.2f", gt);
        
        System.out.println("\nGoods once sold cannot be taken back \nor exchanged.\t\t    Sign Here___________");
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
        //Close the input stream
        in.close();
       } catch (Exception e) { //Catch exception if any
         System.err.println("Error: " + e.getMessage());
       }
     }
    }
    Here is my question:
    I want to multiply "QTY" and "RATE" and the answer should print under "AMOUNT" column. Also I want to add all the "AMOUNT" values for "TOTAL"

    I am attaching this file where the program takes the values from:
    bill.txtAmount column in this file is just an example.
    I want to make a program where computer calculate all the Amount values.

    Any help is appreciated.
    Thanks

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    All extensions are in bold

    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.StringTokenizer;
    
    public class bill {
    
     public static void main(String[] args) {
       System.out.println("\t<===========================>");
       System.out.println("\tCOMPUTER & ELECTRONICS SHOP");
       System.out.println("\t<===========================>");
       System.out.println("\t   14xx Washington Ave \n  \t      NY 12xxx Saturn");
       // System.out.println("Memo# 458");
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
                java.util.Date date = new java.util.Date();
                String datetime = dateFormat.format(date);
                System.out.println("Memo# 458 \t\t\t\tDate " + datetime);
                       
       System.out.println("**--------------------------------------------------** ");
       System.out.println("SrNo.\tITEMS \tQTY \tRATE \tAMOUNT ");
       System.out.println("**--------------------------------------------------** ");
       try {
       FileInputStream fstream =
        new FileInputStream("C:\\Users\\Aziz\\Desktop\\bill.txt");
       // Get the object of DataInputStream
       DataInputStream in = new DataInputStream(fstream);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
       String strLine;
       int rows = 0;
       float total = 0f;
       float avg;
       float avg1,sum;
       double gt;
       double tax;
    [B]   double qty = 1;
       double rate = 1;[/B]
     
     
       //Read File Line By Line
       while ((strLine = br.readLine()) != null) {
        rows++;
        // Print the content on the console
        StringTokenizer Tok = new StringTokenizer(strLine, ",");
        int currentToken = 0;
        int totalTokens = Tok.countTokens();
        while (Tok.hasMoreElements()) {
         currentToken++;
         String tokenValue = Tok.nextToken();
    [B]     if (currentToken == 3) qty = Double.parseDouble(tokenValue); 
         if (currentToken == 4) rate = Double.parseDouble(tokenValue); [/B]
         System.out.print(tokenValue + "\t");
         if(currentToken==totalTokens){
    [B]    	 total+=(rate*qty);
        	 System.out.print(rate*qty);[/B]
         }
        }
       System.out.print("\n");
       }
       avg = total/rows;
      // System.out.println("\t 10 Days Moving Average =\t" + avg);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
       
     
        System.out.printf("Total\t\t\t\t$%.2f", total);
        tax=(.08*total);
        System.out.printf("\nTax\t\t\t\t$%.2f", tax);
        gt=tax+total;
        System.out.println("\n-------------------------------------------------- ");
        System.out.printf("Amount to be paid \t\t\t$%.2f", gt);
        
        System.out.println("\nGoods once sold cannot be taken back \nor exchanged.\t\t    Sign Here___________");
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
        //Close the input stream
        in.close();
       } catch (Exception e) { //Catch exception if any
         System.err.println("Error: " + e.getMessage());
       }
     }
    }
    Your bill.txt should look like this now:
    Java Code:
    1,JavaBook,1,100
    2,Mouse,2,25.4
    3,Camera,1,150
    4,Shirt,5,20.3
    5,Harddrive,1,80
    6,USB,2,15.7
    7,Cable,1,10
    8,Modem,1,30



    Another way of doing this is storing each line in an array:
    Java Code:
    ....
    String[] strLine2 = new String[4];
    ....
    while ((strLine = br.readLine()) != null) {
         strLine2 = strLine.split(",");
         .....
    If you use an array, you wouldn't need the first token in your bill.txt. Also, accessing each each token is a lot easier.
    Last edited by Lil_Aziz1; 01-16-2010 at 07:49 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. multiply two matrixes
    By smart princess in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 07:43 PM
  2. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  3. display column value multiply with 100
    By tiiim83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-15-2009, 04:40 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  5. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •