If I want to be able to display the “total sales” within the file “Cart.txt”, how would I do that?












1















I want the Cart.txt file to be able write whatever the total sales are in the file itself. The file right now just says:



3,2,Shoes
3,4,Shirt
2,5,Car


This is the current output:



run:
Enter how many items you are buying
3
Enter the items you are buying, structured as followed
Quantity,Price,Item Name:
3,2,Shoes
3,4,Shirt
2,5,Car
Those values were written to Cart.txt
Sold 3 of Shoes at $2.00 each.
Sold 3 of Shirt at $4.00 each.
Sold 2 of Car at $5.00 each.
Total sales: $28.00


This is the code itself:



package shop;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class Shop
{

public static void main(String args)
{
String fileName = "Cart.txt";
PrintWriter outputStream = null;

try
{
outputStream= new PrintWriter (fileName);
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file "+ fileName);
System.exit(0);
}

System.out.println("Enter how many items you are buying");
Scanner keyboard = new Scanner (System.in);
Scanner intinput = new Scanner (System.in);
int input = intinput.nextInt();

System.out.println("Enter the items you are buying, structured as followed"
+ " nQuantity,Price,Item Name:");


for(int count=1; count<=input; count++)
{
String line = keyboard.nextLine();
outputStream.println(line);

}

outputStream.close();
System.out.println("Those values were written to "+ fileName);

try
{
Scanner inputStream = new Scanner(new File(fileName));
String line = inputStream.toString();
double total = 0;
for(int count=1; count<=input; count++)
{

line = inputStream.nextLine();
String ary = line.split (",");
int quantity = Integer.parseInt (ary[0]);
double price = Double.parseDouble(ary[1]);
String description = ary[2];
System.out.printf("Sold %d of %s at $%1.2f each. n",
quantity, description, price);
total += quantity * price;

}

System.out.printf("Total sales: $%1.2fn", total);
inputStream.close();

}
catch (FileNotFoundException e)
{
System.out.println("Cannot find file " + fileName);
}
catch (IOException e)
{
System.out.println("Problem with input file " + fileName);
}

}
}









share|improve this question





























    1















    I want the Cart.txt file to be able write whatever the total sales are in the file itself. The file right now just says:



    3,2,Shoes
    3,4,Shirt
    2,5,Car


    This is the current output:



    run:
    Enter how many items you are buying
    3
    Enter the items you are buying, structured as followed
    Quantity,Price,Item Name:
    3,2,Shoes
    3,4,Shirt
    2,5,Car
    Those values were written to Cart.txt
    Sold 3 of Shoes at $2.00 each.
    Sold 3 of Shirt at $4.00 each.
    Sold 2 of Car at $5.00 each.
    Total sales: $28.00


    This is the code itself:



    package shop;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.File;
    import java.io.PrintWriter;
    import java.util.Scanner;

    public class Shop
    {

    public static void main(String args)
    {
    String fileName = "Cart.txt";
    PrintWriter outputStream = null;

    try
    {
    outputStream= new PrintWriter (fileName);
    }
    catch (FileNotFoundException e)
    {
    System.out.println("Error opening the file "+ fileName);
    System.exit(0);
    }

    System.out.println("Enter how many items you are buying");
    Scanner keyboard = new Scanner (System.in);
    Scanner intinput = new Scanner (System.in);
    int input = intinput.nextInt();

    System.out.println("Enter the items you are buying, structured as followed"
    + " nQuantity,Price,Item Name:");


    for(int count=1; count<=input; count++)
    {
    String line = keyboard.nextLine();
    outputStream.println(line);

    }

    outputStream.close();
    System.out.println("Those values were written to "+ fileName);

    try
    {
    Scanner inputStream = new Scanner(new File(fileName));
    String line = inputStream.toString();
    double total = 0;
    for(int count=1; count<=input; count++)
    {

    line = inputStream.nextLine();
    String ary = line.split (",");
    int quantity = Integer.parseInt (ary[0]);
    double price = Double.parseDouble(ary[1]);
    String description = ary[2];
    System.out.printf("Sold %d of %s at $%1.2f each. n",
    quantity, description, price);
    total += quantity * price;

    }

    System.out.printf("Total sales: $%1.2fn", total);
    inputStream.close();

    }
    catch (FileNotFoundException e)
    {
    System.out.println("Cannot find file " + fileName);
    }
    catch (IOException e)
    {
    System.out.println("Problem with input file " + fileName);
    }

    }
    }









    share|improve this question



























      1












      1








      1








      I want the Cart.txt file to be able write whatever the total sales are in the file itself. The file right now just says:



      3,2,Shoes
      3,4,Shirt
      2,5,Car


      This is the current output:



      run:
      Enter how many items you are buying
      3
      Enter the items you are buying, structured as followed
      Quantity,Price,Item Name:
      3,2,Shoes
      3,4,Shirt
      2,5,Car
      Those values were written to Cart.txt
      Sold 3 of Shoes at $2.00 each.
      Sold 3 of Shirt at $4.00 each.
      Sold 2 of Car at $5.00 each.
      Total sales: $28.00


      This is the code itself:



      package shop;

      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.File;
      import java.io.PrintWriter;
      import java.util.Scanner;

      public class Shop
      {

      public static void main(String args)
      {
      String fileName = "Cart.txt";
      PrintWriter outputStream = null;

      try
      {
      outputStream= new PrintWriter (fileName);
      }
      catch (FileNotFoundException e)
      {
      System.out.println("Error opening the file "+ fileName);
      System.exit(0);
      }

      System.out.println("Enter how many items you are buying");
      Scanner keyboard = new Scanner (System.in);
      Scanner intinput = new Scanner (System.in);
      int input = intinput.nextInt();

      System.out.println("Enter the items you are buying, structured as followed"
      + " nQuantity,Price,Item Name:");


      for(int count=1; count<=input; count++)
      {
      String line = keyboard.nextLine();
      outputStream.println(line);

      }

      outputStream.close();
      System.out.println("Those values were written to "+ fileName);

      try
      {
      Scanner inputStream = new Scanner(new File(fileName));
      String line = inputStream.toString();
      double total = 0;
      for(int count=1; count<=input; count++)
      {

      line = inputStream.nextLine();
      String ary = line.split (",");
      int quantity = Integer.parseInt (ary[0]);
      double price = Double.parseDouble(ary[1]);
      String description = ary[2];
      System.out.printf("Sold %d of %s at $%1.2f each. n",
      quantity, description, price);
      total += quantity * price;

      }

      System.out.printf("Total sales: $%1.2fn", total);
      inputStream.close();

      }
      catch (FileNotFoundException e)
      {
      System.out.println("Cannot find file " + fileName);
      }
      catch (IOException e)
      {
      System.out.println("Problem with input file " + fileName);
      }

      }
      }









      share|improve this question
















      I want the Cart.txt file to be able write whatever the total sales are in the file itself. The file right now just says:



      3,2,Shoes
      3,4,Shirt
      2,5,Car


      This is the current output:



      run:
      Enter how many items you are buying
      3
      Enter the items you are buying, structured as followed
      Quantity,Price,Item Name:
      3,2,Shoes
      3,4,Shirt
      2,5,Car
      Those values were written to Cart.txt
      Sold 3 of Shoes at $2.00 each.
      Sold 3 of Shirt at $4.00 each.
      Sold 2 of Car at $5.00 each.
      Total sales: $28.00


      This is the code itself:



      package shop;

      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.File;
      import java.io.PrintWriter;
      import java.util.Scanner;

      public class Shop
      {

      public static void main(String args)
      {
      String fileName = "Cart.txt";
      PrintWriter outputStream = null;

      try
      {
      outputStream= new PrintWriter (fileName);
      }
      catch (FileNotFoundException e)
      {
      System.out.println("Error opening the file "+ fileName);
      System.exit(0);
      }

      System.out.println("Enter how many items you are buying");
      Scanner keyboard = new Scanner (System.in);
      Scanner intinput = new Scanner (System.in);
      int input = intinput.nextInt();

      System.out.println("Enter the items you are buying, structured as followed"
      + " nQuantity,Price,Item Name:");


      for(int count=1; count<=input; count++)
      {
      String line = keyboard.nextLine();
      outputStream.println(line);

      }

      outputStream.close();
      System.out.println("Those values were written to "+ fileName);

      try
      {
      Scanner inputStream = new Scanner(new File(fileName));
      String line = inputStream.toString();
      double total = 0;
      for(int count=1; count<=input; count++)
      {

      line = inputStream.nextLine();
      String ary = line.split (",");
      int quantity = Integer.parseInt (ary[0]);
      double price = Double.parseDouble(ary[1]);
      String description = ary[2];
      System.out.printf("Sold %d of %s at $%1.2f each. n",
      quantity, description, price);
      total += quantity * price;

      }

      System.out.printf("Total sales: $%1.2fn", total);
      inputStream.close();

      }
      catch (FileNotFoundException e)
      {
      System.out.println("Cannot find file " + fileName);
      }
      catch (IOException e)
      {
      System.out.println("Problem with input file " + fileName);
      }

      }
      }






      java file






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 2:38









      Nicholas K

      7,78361637




      7,78361637










      asked Nov 15 '18 at 18:06









      Arvinder SinghArvinder Singh

      113




      113
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Perform the following :




          1. Write to the file after calculating the total :
            outputStream.println(total);

          2. Close the outputStream after writing the total to the file




          EDIT:



          Make the following changes in this block of code :



              System.out.println("Enter the items you are buying, structured as 
          followed" + " nQuantity,Price,Item Name:");
          double tot = 0.0;
          for (int count = 1; count <= input; count++) {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr = line.split(",");
          tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total sales: $" + tot);
          outputStream.close();
          System.out.println("Those values were written to " + fileName);


          Here we calculate the total for all entries and then just write it once to the file.



          Output




          3,2,Shoes



          3,4,Shirt



          2,5,Car



          Total sales: $28.0







          share|improve this answer


























          • Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

            – Arvinder Singh
            Nov 15 '18 at 18:18











          • Why are you first writing to the file and then trying to read it?

            – Nicholas K
            Nov 15 '18 at 18:33











          • My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

            – Arvinder Singh
            Nov 15 '18 at 18:44











          • Edited the answer.

            – Nicholas K
            Nov 15 '18 at 18:52






          • 1





            Thank you!!! That works :)

            – Arvinder Singh
            Nov 15 '18 at 18:59



















          0














          I would keep a running total and then add it in at the end:



          Double total = 0; 

          for(int count=1; count<=input; count++)
          {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr= line.split(",");
          total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total: " + total);





          share|improve this answer


























          • Sorry, I see someone just answered something similar.

            – Sean Leroy
            Nov 15 '18 at 18:22











          • The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

            – Arvinder Singh
            Nov 15 '18 at 18:38











          • I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

            – Sean Leroy
            Nov 15 '18 at 18:46













          • They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

            – Arvinder Singh
            Nov 15 '18 at 18:49











          • it is the original block of code with added lines around it. Replace your for loop with this.

            – Sean Leroy
            Nov 15 '18 at 18:50













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325473%2fif-i-want-to-be-able-to-display-the-total-sales-within-the-file-cart-txt-ho%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Perform the following :




          1. Write to the file after calculating the total :
            outputStream.println(total);

          2. Close the outputStream after writing the total to the file




          EDIT:



          Make the following changes in this block of code :



              System.out.println("Enter the items you are buying, structured as 
          followed" + " nQuantity,Price,Item Name:");
          double tot = 0.0;
          for (int count = 1; count <= input; count++) {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr = line.split(",");
          tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total sales: $" + tot);
          outputStream.close();
          System.out.println("Those values were written to " + fileName);


          Here we calculate the total for all entries and then just write it once to the file.



          Output




          3,2,Shoes



          3,4,Shirt



          2,5,Car



          Total sales: $28.0







          share|improve this answer


























          • Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

            – Arvinder Singh
            Nov 15 '18 at 18:18











          • Why are you first writing to the file and then trying to read it?

            – Nicholas K
            Nov 15 '18 at 18:33











          • My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

            – Arvinder Singh
            Nov 15 '18 at 18:44











          • Edited the answer.

            – Nicholas K
            Nov 15 '18 at 18:52






          • 1





            Thank you!!! That works :)

            – Arvinder Singh
            Nov 15 '18 at 18:59
















          0














          Perform the following :




          1. Write to the file after calculating the total :
            outputStream.println(total);

          2. Close the outputStream after writing the total to the file




          EDIT:



          Make the following changes in this block of code :



              System.out.println("Enter the items you are buying, structured as 
          followed" + " nQuantity,Price,Item Name:");
          double tot = 0.0;
          for (int count = 1; count <= input; count++) {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr = line.split(",");
          tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total sales: $" + tot);
          outputStream.close();
          System.out.println("Those values were written to " + fileName);


          Here we calculate the total for all entries and then just write it once to the file.



          Output




          3,2,Shoes



          3,4,Shirt



          2,5,Car



          Total sales: $28.0







          share|improve this answer


























          • Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

            – Arvinder Singh
            Nov 15 '18 at 18:18











          • Why are you first writing to the file and then trying to read it?

            – Nicholas K
            Nov 15 '18 at 18:33











          • My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

            – Arvinder Singh
            Nov 15 '18 at 18:44











          • Edited the answer.

            – Nicholas K
            Nov 15 '18 at 18:52






          • 1





            Thank you!!! That works :)

            – Arvinder Singh
            Nov 15 '18 at 18:59














          0












          0








          0







          Perform the following :




          1. Write to the file after calculating the total :
            outputStream.println(total);

          2. Close the outputStream after writing the total to the file




          EDIT:



          Make the following changes in this block of code :



              System.out.println("Enter the items you are buying, structured as 
          followed" + " nQuantity,Price,Item Name:");
          double tot = 0.0;
          for (int count = 1; count <= input; count++) {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr = line.split(",");
          tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total sales: $" + tot);
          outputStream.close();
          System.out.println("Those values were written to " + fileName);


          Here we calculate the total for all entries and then just write it once to the file.



          Output




          3,2,Shoes



          3,4,Shirt



          2,5,Car



          Total sales: $28.0







          share|improve this answer















          Perform the following :




          1. Write to the file after calculating the total :
            outputStream.println(total);

          2. Close the outputStream after writing the total to the file




          EDIT:



          Make the following changes in this block of code :



              System.out.println("Enter the items you are buying, structured as 
          followed" + " nQuantity,Price,Item Name:");
          double tot = 0.0;
          for (int count = 1; count <= input; count++) {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr = line.split(",");
          tot += (Integer.parseInt(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total sales: $" + tot);
          outputStream.close();
          System.out.println("Those values were written to " + fileName);


          Here we calculate the total for all entries and then just write it once to the file.



          Output




          3,2,Shoes



          3,4,Shirt



          2,5,Car



          Total sales: $28.0








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 18:58

























          answered Nov 15 '18 at 18:13









          Nicholas KNicholas K

          7,78361637




          7,78361637













          • Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

            – Arvinder Singh
            Nov 15 '18 at 18:18











          • Why are you first writing to the file and then trying to read it?

            – Nicholas K
            Nov 15 '18 at 18:33











          • My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

            – Arvinder Singh
            Nov 15 '18 at 18:44











          • Edited the answer.

            – Nicholas K
            Nov 15 '18 at 18:52






          • 1





            Thank you!!! That works :)

            – Arvinder Singh
            Nov 15 '18 at 18:59



















          • Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

            – Arvinder Singh
            Nov 15 '18 at 18:18











          • Why are you first writing to the file and then trying to read it?

            – Nicholas K
            Nov 15 '18 at 18:33











          • My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

            – Arvinder Singh
            Nov 15 '18 at 18:44











          • Edited the answer.

            – Nicholas K
            Nov 15 '18 at 18:52






          • 1





            Thank you!!! That works :)

            – Arvinder Singh
            Nov 15 '18 at 18:59

















          Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

          – Arvinder Singh
          Nov 15 '18 at 18:18





          Yea I tried that already before, but the program runs exactly the same, and the file looks the same.

          – Arvinder Singh
          Nov 15 '18 at 18:18













          Why are you first writing to the file and then trying to read it?

          – Nicholas K
          Nov 15 '18 at 18:33





          Why are you first writing to the file and then trying to read it?

          – Nicholas K
          Nov 15 '18 at 18:33













          My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

          – Arvinder Singh
          Nov 15 '18 at 18:44





          My prompt wants me to first enter items like I'm purchasing something, store it to a file, and then display the items as well as the total. Then it wants me to write the total to the file as well which is what I'm having trouble with. I liked your response because I tried that too, but for some reason the total doesn't end up in the file at the end, only the original items.

          – Arvinder Singh
          Nov 15 '18 at 18:44













          Edited the answer.

          – Nicholas K
          Nov 15 '18 at 18:52





          Edited the answer.

          – Nicholas K
          Nov 15 '18 at 18:52




          1




          1





          Thank you!!! That works :)

          – Arvinder Singh
          Nov 15 '18 at 18:59





          Thank you!!! That works :)

          – Arvinder Singh
          Nov 15 '18 at 18:59













          0














          I would keep a running total and then add it in at the end:



          Double total = 0; 

          for(int count=1; count<=input; count++)
          {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr= line.split(",");
          total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total: " + total);





          share|improve this answer


























          • Sorry, I see someone just answered something similar.

            – Sean Leroy
            Nov 15 '18 at 18:22











          • The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

            – Arvinder Singh
            Nov 15 '18 at 18:38











          • I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

            – Sean Leroy
            Nov 15 '18 at 18:46













          • They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

            – Arvinder Singh
            Nov 15 '18 at 18:49











          • it is the original block of code with added lines around it. Replace your for loop with this.

            – Sean Leroy
            Nov 15 '18 at 18:50


















          0














          I would keep a running total and then add it in at the end:



          Double total = 0; 

          for(int count=1; count<=input; count++)
          {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr= line.split(",");
          total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total: " + total);





          share|improve this answer


























          • Sorry, I see someone just answered something similar.

            – Sean Leroy
            Nov 15 '18 at 18:22











          • The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

            – Arvinder Singh
            Nov 15 '18 at 18:38











          • I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

            – Sean Leroy
            Nov 15 '18 at 18:46













          • They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

            – Arvinder Singh
            Nov 15 '18 at 18:49











          • it is the original block of code with added lines around it. Replace your for loop with this.

            – Sean Leroy
            Nov 15 '18 at 18:50
















          0












          0








          0







          I would keep a running total and then add it in at the end:



          Double total = 0; 

          for(int count=1; count<=input; count++)
          {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr= line.split(",");
          total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total: " + total);





          share|improve this answer















          I would keep a running total and then add it in at the end:



          Double total = 0; 

          for(int count=1; count<=input; count++)
          {
          String line = keyboard.nextLine();
          outputStream.println(line);
          String arr= line.split(",");
          total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
          }
          outputStream.println("Total: " + total);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 18:40

























          answered Nov 15 '18 at 18:22









          Sean LeroySean Leroy

          687




          687













          • Sorry, I see someone just answered something similar.

            – Sean Leroy
            Nov 15 '18 at 18:22











          • The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

            – Arvinder Singh
            Nov 15 '18 at 18:38











          • I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

            – Sean Leroy
            Nov 15 '18 at 18:46













          • They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

            – Arvinder Singh
            Nov 15 '18 at 18:49











          • it is the original block of code with added lines around it. Replace your for loop with this.

            – Sean Leroy
            Nov 15 '18 at 18:50





















          • Sorry, I see someone just answered something similar.

            – Sean Leroy
            Nov 15 '18 at 18:22











          • The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

            – Arvinder Singh
            Nov 15 '18 at 18:38











          • I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

            – Sean Leroy
            Nov 15 '18 at 18:46













          • They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

            – Arvinder Singh
            Nov 15 '18 at 18:49











          • it is the original block of code with added lines around it. Replace your for loop with this.

            – Sean Leroy
            Nov 15 '18 at 18:50



















          Sorry, I see someone just answered something similar.

          – Sean Leroy
          Nov 15 '18 at 18:22





          Sorry, I see someone just answered something similar.

          – Sean Leroy
          Nov 15 '18 at 18:22













          The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

          – Arvinder Singh
          Nov 15 '18 at 18:38





          The doesn't work :( because you defined count as a double and it can't be converted as an int and you also use it as an int in the for statement.

          – Arvinder Singh
          Nov 15 '18 at 18:38













          I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

          – Sean Leroy
          Nov 15 '18 at 18:46







          I know in your example you use integers as input. Are you allowing the user to enter doubles? I also changed count to total.

          – Sean Leroy
          Nov 15 '18 at 18:46















          They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

          – Arvinder Singh
          Nov 15 '18 at 18:49





          They can enter double for the prices only so it can be like: 2,3.50,Shoes for example. I'll try it with your updated code. Where exactly in the code should I place it? between what lines

          – Arvinder Singh
          Nov 15 '18 at 18:49













          it is the original block of code with added lines around it. Replace your for loop with this.

          – Sean Leroy
          Nov 15 '18 at 18:50







          it is the original block of code with added lines around it. Replace your for loop with this.

          – Sean Leroy
          Nov 15 '18 at 18:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325473%2fif-i-want-to-be-able-to-display-the-total-sales-within-the-file-cart-txt-ho%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values