• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

JAVA: Multiplying all elements of a 2-D Array by a constant PLEASE HELP

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

LegolasElf

Member
Joined
Jul 29, 2005
Location
Kenner, LA
Hey guys I'm trying to multiply every element of a 2D array by a double but I'm getting erroneous output.

This is the main file:

Code:
import java.io.*;
import java.util.Scanner;
 
public class prog5
{
   public static void main(String[] args) throws FileNotFoundException  
   {
      
       String[] names = new String[10];
       String first, last;
       int[][] ints = new int[10][4];
       double[][] individual_month_sales = new double[10][4];
       Sales one = new Sales();
       
       
       File inFile = new File("fal.data");
       Scanner in = new Scanner(inFile);

       int count = 0;
       
       while(in.hasNext())
       {
            last = in.next();
            first = in.next();
            names[count] = last + first; 

           
           for(int row=0; row < 10; row++){
               for(int col=0; row < 4; row++){
                    ints[row][col] = in.nextInt();
                }
            }
           count++;
        } 
        
       
        one.monthlySales(ints, individual_month_sales);
        
        for(int n=0; n<10; n++)
        {
            for(int m=0; m<4; m++)
            {
               System.out.println(individual_month_sales[n][m]); 
            }
        }
           
    }
}

And this is the class which has the method that does the multiplication:


Code:
class Sales
{
    private int[][] integer_data;
    private double[][] month_sales;
      
    public void monthlySales(int[][] integer_data, double[][] month_sales)
    {
        for(int i=0; i<10; i++)
        {
            for(int j=0; j<4; j++)
            {
                month_sales[i][j] = 195.85 * integer_data[i][j];
            }
        }
    }
    
}

This is my input file:

Code:
Manyana, Michael 30 25 45 18
Henderson, Marge 22 30 32 35
Striker, Nancy 32 30 33 31
Johnson, Fred 12 17 19 15
Ryan, Renee 22 17 28 16
Anderson, Roy 15 18 19 20
Bendix, Sue 21 25 20 23
Sparks, Angie 25 27 23 28
Calhoun, Drew 31 30 29 28
Zilder, Zeek 15 11 12 14

So basically I have stored all the integers of this file in a 2-D array and i want to multiply each element by 195.85 and store the result into another 2-D array. This is the output that I am getting

Output:

Code:
2937.75
0.0
0.0
0.0
2154.35
0.0
0.0
0.0
2350.2
0.0
0.0
0.0
2741.9
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

Any help is greatly appreciated guys.
 
I haven't done anything in Java in a while, but just from a straight logic perspective, this doesn't look right.

Code:
for(int row=0; row < 10; row++){
               for(int col=0; row < 4; row++){
                    ints[row][col] = in.nextInt();
                }
            }

You're incrementing row in your inner for loop instead of col.
 
I haven't done anything in Java in a while, but just from a straight logic perspective, this doesn't look right.

Code:
for(int row=0; row < 10; row++){
               for(int col=0; row < 4; row++){
                    ints[row][col] = in.nextInt();
                }
            }

You're incrementing row in your inner for loop instead of col.

copy/paste fail? :p
 
Code:
for(int row=0; row < 10; row++){
     for(int col=0; row < 4; row++){
          ints[row][col] = in.nextInt();
     }
}

Should probably be...

HTML:
for(int row=0; row < 10; row++){
     for(int col=0; col < 4; col++){
          ints[row][col] = in.nextInt();
     }
}
 
Stupid mistake on my behalf. One last thing that I need to do is to print my results to a file. This is how Im doing it but its giving me an error:

Code:
import java.io.*;
import java.util.Scanner;
 
public class prog5
{
   public static void main(String[] args) throws FileNotFoundException  
   {
      
       String[] names = new String[10];
       String first, last;
       int[][] ints = new int[10][4];
       double[][] individual_month_sales = new double[10][4];
       double[] row_sales = new double[10];
       double[] col_sales = new double[4];
       Sales one = new Sales();
        
       File inFile = new File("fal.data");
       Scanner in = new Scanner(inFile);
       
        FileWriter fstream = new FileWriter("out.data");
        BufferedWriter out = new BufferedWriter(fstream);
       

       
       int count = 0;
       while(in.hasNext())
       {
           for(int row=0; row < 10; row++)
           {
               last = in.next();
               first = in.next();
               names[count] = last + first; 

               for(int col=0; col < 4; col++)
               {
                   ints[row][col] = in.nextInt();

                }
               count++;
            }
        } 
        in.close();
        
       one.monthlySales(ints, individual_month_sales);
       one.individualSales(individual_month_sales, row_sales); //sum of row sales
       one.salesForEachMonth(individual_month_sales, col_sales); //sum of column sales      
       PrintReport(names, individual_month_sales, row_sales, col_sales);   
       
       out.close();
    }
    
   public static void PrintLine()
   {
      out.println("---------------------------------------------------------------------------");
   }
       
   public static void PrintHeadings()
   {
      out.println("NAME (Last,First)  Month 1     Month 2    Month 3     Month 4   Total Sales");
      PrintLine();
   }    
   
   public static void PrintReport(String[] names, double[][] individual_month_sales, double[] row_sales, double[] col_sales)
   {
    
      out.println("                            SALES REPORT");
      PrintHeadings();
      
    for(int i=0; i<10; i++)
    {   
       out.print(names[i]);
       out.print("      ");
       
       for(int j=0; j<4; j++)
       { 
           out.print(individual_month_sales[i][j]); 
           out.print(" ");       
        }
        
       out.print(row_sales[i]); 
       out.println(); 
    }
    
     PrintLine();
     out.print("SALES/MONTH      ");   
    for(int a=0; a<4; a++)
    {
        out.print("   ");   
        out.print(col_sales[a]); 
        out.print(" ");       
    }
          
   }
}

I am getting the error "Cannot Find Symbol - Variable out" everywhere where I am printing something to the file out.data. Am I doing this correctly?
 
"out" isn't defined in the function you're using it in.

You defined "out" in your main function. You're trying to use it in PrintReport().
Either pass it in, move the declaration inside PrintReport(), or move what's in PrintReport() into the main function.

#2 would probably be best in this case, since you aren't doing anything with "out" in your main function, so there's no reason for the declaration for it to be there.
 
Last edited:
"out" isn't defined in the function you're using it in.

You defined "out" in your main function. You're trying to use it in PrintReport().
Either pass it in, move the declaration inside PrintReport(), or move what's in PrintReport() into the main function.

#2 would probably be best in this case, since you aren't doing anything with "out" in your main function, so there's no reason for the declaration for it to be there.

Ok so what about the PrintHeadings() and PrintLine() functions? I am required to have those. Do i just do this in every single function seperately?

FileWriter fstream = new FileWriter("out.data");
BufferedWriter out = new BufferedWriter(fstream);
 
Ah, apologies, overlooked the other two functions.
Well, if you're required to have those three functions just to print output (seems somewhat excessive), then you should just pass in the variable to each.
 
This is how I passed out to all print functions. Is this correct? I am still getting an error saying

"Cannot find symbol - method println"

Code:
import java.io.*;
import java.util.Scanner;
 
public class prog5
{
   public static void main(String[] args) throws FileNotFoundException  
   {
      
       String[] names = new String[10];
       String first, last;
       int[][] ints = new int[10][4];
       double[][] individual_month_sales = new double[10][4];
       double[] row_sales = new double[10];
       double[] col_sales = new double[4];
       Sales one = new Sales();
       
       File inFile = new File("fal.data");
       Scanner in = new Scanner(inFile);
     
       FileWriter fstream = new FileWriter("out.data");
       BufferedWriter out = new BufferedWriter(fstream);       
       
       int count = 0;
       while(in.hasNext())
       {
           for(int row=0; row < 10; row++)
           {
               last = in.next();
               first = in.next();
               names[count] = last + first; 

               for(int col=0; col < 4; col++)
               {
                   ints[row][col] = in.nextInt();

                }
               count++;
            }
        } 
        in.close();
        
       one.monthlySales(ints, individual_month_sales);
       one.individualSales(individual_month_sales, row_sales); //sum of row sales
       one.salesForEachMonth(individual_month_sales, col_sales); //sum of column sales      
       PrintReport(names, individual_month_sales, row_sales, col_sales, out); //prints report to a file   
       out.close();
       
       
   }
    
   public static void PrintLine(BufferedWriter out) 
   {
      out.println("---------------------------------------------------------------------------");
   }
       
   public static void PrintHeadings(BufferedWriter out) 
   {
      out.println("NAME (Last,First)  Month 1     Month 2    Month 3     Month 4   Total Sales");
      PrintLine(out);
   }    
   
   public static void PrintReport(String[] names, double[][] individual_month_sales, double[] row_sales, double[] col_sales, BufferedWriter out) 
   {
      out.println("                            SALES REPORT");
      PrintHeadings(out);
      
    for(int i=0; i<10; i++)
    {   
       out.print(names[i]);
       out.print("      ");
       
       for(int j=0; j<4; j++)
       { 
           out.print(individual_month_sales[i][j]); 
           out.print(" ");       
        }
        
       out.print(row_sales[i]); 
       out.println(out); 
    }
    
     PrintLine(out);
     out.print("SALES/MONTH      ");   
    for(int a=0; a<4; a++)
    {
        out.print("   ");   
        out.print(col_sales[a]); 
        out.print(" ");       
    }
  
          
   }
}
 
Back