The output should now look like this Row 1 sum 30 Row 2 sum

The output should now look like this:

Row 1 sum: 30
Row 2 sum: 48
Row 3 sum: 55

Count: 12
Grand total: 133
Mean: 11.08

Even numbers: 7
Odd numbers: 5

How to add even and odd numbers counts to existing code

public class Problem {
   public static void main(String [] args)
   {
       int iVal;
       int iRow;
       int iRowSum;
       int iToken;
      
       int totalSum = 0;
       int totalEle = 0;

       iRowSum = 0;
       iRow = 1;
       try
       {
           Scanner ifsInput = new Scanner(new File(\"c:\\home\\student\\lastname/9_1_Input.txt\"));
           while(ifsInput.hasNextLine())
           {
               while(ifsInput.hasNextInt())
               {
                   for(iToken = 0; iToken <= 3; iToken++)
                   {
                       iVal = ifsInput.nextInt();
                       iRowSum = iRowSum + iVal;
                      
                       // increasing count of total element
                       totalEle++;
                      
                   }
                  
                   // adding into totalSum
                   totalSum = totalSum + iRowSum;
                  
                   System.out.println(\"Row \" + iRow + \" sum: \" + iRowSum);
                   iRowSum = 0;
                   iRow++;
               }
           }
           // added new code to print grand total and mean value
           System.out.println(\"Count: \"+totalEle);
           System.out.println(\"Grand total: \"+totalSum);
           System.out.println(\"Mean: \"+String.format(\".2f\", (double)/totalSum/totalEle);
       }
       catch (FileNotFoundException sMsg)
       {
           System.out.println(\"The file cannot be found or opened.\");
       }
   }
}

Solution

Input999.txt( Save this file under D Drive Then the path of the file pointing to it is D:\\\\Input999.txt)

11 22 33 44
22 33 44 55
33 44 55 66

___________________________

Problem.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Problem {
   public static void main(String [] args)
   {
       //Declaring variables
   int iVal;
   int iRow;
   int iRowSum;
   int iToken;
   double mean=0.0;
   int evenCount=0,oddCount=0;
   int totalSum = 0;
   int totalEle = 0;
   iRowSum = 0;
     
   iRow = 1;
     
   try
   {
   Scanner ifsInput = new Scanner(new File(\"D:\\\\Input999.txt\"));
   while(ifsInput.hasNextLine())
   {
   while(ifsInput.hasNextInt())
   {
   for(iToken = 0; iToken <= 3; iToken++)
   {
   iVal = ifsInput.nextInt();
     
   //Calculating the even count
   if(iVal%2==0)
       evenCount++;
   //Calculating the odd count
   else
       oddCount++;
     
   //Calculating the sum of elements in each row
   iRowSum = iRowSum + iVal;
  
   // increasing count of total element
   totalEle++;
  
   }
  
   // adding into totalSum
   totalSum = totalSum + iRowSum;
  
   //Displaying the sum of elements in each row
   System.out.println(\"Row \" + iRow + \" sum: \" + iRowSum);
   iRowSum = 0;
   iRow++;
   }
   }
   //Displaying the number of elements
   System.out.println(\"Count: \"+totalEle);
     
   //Displaying the sum of all elements
   System.out.println(\"Grand total: \"+totalSum);
     
   //Calculating the mean
   mean=(double)totalSum/totalEle;
  
  
   //Displaying the mean
   System.out.printf(\"Mean: %.2f\ \",mean);
     
   //Displaying the number of even nos
   System.out.println(\"Even Numbers :\"+evenCount);
     
   //Displaying the number of odd nos
   System.out.println(\"Odd Numbers :\"+oddCount);
   }
   catch (FileNotFoundException sMsg)
   {
   System.out.println(\"The file cannot be found or opened.\");
   }
   }
   }
     

_________________________________

Output:

Row 1 sum: 110
Row 2 sum: 154
Row 3 sum: 198
Count: 12
Grand total: 462
Mean: 38.50
Even Numbers :6
Odd Numbers :6

___________Thank You

The output should now look like this: Row 1 sum: 30 Row 2 sum: 48 Row 3 sum: 55 Count: 12 Grand total: 133 Mean: 11.08 Even numbers: 7 Odd numbers: 5 How to add
The output should now look like this: Row 1 sum: 30 Row 2 sum: 48 Row 3 sum: 55 Count: 12 Grand total: 133 Mean: 11.08 Even numbers: 7 Odd numbers: 5 How to add
The output should now look like this: Row 1 sum: 30 Row 2 sum: 48 Row 3 sum: 55 Count: 12 Grand total: 133 Mean: 11.08 Even numbers: 7 Odd numbers: 5 How to add

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site