Connecting Tech Pros Worldwide Help | Site Map

Display Strings from a 2 dimensional array

owz owz is offline
Newbie
 
Join Date: Nov 2006
Posts: 13
#1: Mar 7 '07
This is what im trying 2 get
Sales Figures for 2006 in £000's


Sales staff name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter Max Min Total

John Smith 24 30 15 17 30 15 86
Joe Jones 24 19 10 20 24 10 73
Jill Evans 24 21 20 28 28 20 93

Quarterly totals 72 70 45 65

Total sales are £252,000


Star performers by quarters are:
Quarter 1 John Smith
Quarter 2 John Smith
Quarter 3 Jill Evans
Quarter 4 Jill Evans


i am haveing problems with displaying the star performers. I have got the code 2 display the correct value for the quarter, but the names do not work and are duplicated.

this is what i get:

Star performers by quarters are:

Quarters John Smith

Quarters Paul Evans correct

Quarters John Smith

Quarters John Smith correct

Quarters Jill Evans

Quarters Paul Evans

Quarters Steven Smith correct

Quarters John Smith

Quarters Joe Jones

Quarters Jill Evans correct

all help is welcome. thanx in advance.



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3.  
  4. public class SalesDetails
  5. {
  6.  
  7.         public static void main(String[] args) throws IOException
  8.         {
  9.  
  10.  
  11.     final String   textFilesPath = "C:\\Documents and Settings\\owz\\Desktop\\UNI WORK\\Comp Programming\\";//f:\Desktop\UNI WORK\computer programming
  12.     BufferedReader inputFile;
  13.     String        name;
  14.     int             year;
  15.     String [] names=new String[8];
  16.     int [] [] salesFigures= new int [8] [4];
  17.     int people=0;
  18.     int max;
  19.     int min;
  20.     int total;
  21.     int maxtotal=0;
  22.  
  23.     inputFile = Text.open ( textFilesPath + "sales details.txt" );
  24.  
  25.     System.out.println ( "Salesmen Name\\Sales Figures" );
  26.  
  27.  
  28.     year= Text.readInt (inputFile);
  29.     name= Text.readLine (inputFile);
  30.  
  31.     while ( name.equals ( "****" ) == false )
  32.     {
  33.        names[people]= name;
  34.         for (int q=0; q<4; q++) {    
  35.  
  36.  
  37.         salesFigures[people][q]= Text.readInt ( inputFile );
  38.         }
  39.     people=people +1;
  40.  
  41.  
  42.  
  43.       name = Text.readLine ( inputFile );
  44.  
  45.     }//end-while
  46.  
  47.     inputFile.close();
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     System.out.print("\t\t\t\t\t");    System.out.println("Sales Figures for 2007 in £000's \n");
  56.  
  57.     System.out.println("Sales staff name\t       1st quarter\t       2st quarter\t       3st quarter\t       4st quarter\t      Max\t       Min\t           Total \n");
  58.  
  59.       for (int i=0;i<people; i++) {
  60.           System.out.print(names[i]);
  61.           if (names[i].length()<=11) {
  62.               System.out.print("\t");
  63.           }
  64.           for (int q=0; q<4; q++) {    
  65.  
  66.  
  67.          System.out.print("\t\t"+ salesFigures[i][q]);
  68.           }
  69.               max=salesFigures[i][0];
  70.               min=salesFigures[i][0];
  71.           for (int q=1; q<4; q++) {
  72.  
  73.           if (salesFigures[i][q]>max) {
  74.               max=salesFigures[i][q];
  75.           }
  76.             if(salesFigures[i][q]<min) 
  77.             min=salesFigures[i][q];
  78.           } 
  79.  
  80.  
  81.              total=0;
  82.               for (int q=0; q<4; q++) {
  83.  
  84.               total=total+salesFigures[i][q];
  85.  
  86.                  }     
  87.  
  88.  
  89.          {               
  90.              maxtotal = maxtotal + total;
  91.  
  92.         System.out.print("\t       "+ max +"\t        "+ min+"\t\t  "+ total);
  93.  
  94.  
  95.          System.out.println(); 
  96.  
  97.          }//output max,min and total
  98.  
  99.       }//ouput people and they're figures 
  100.        System.out.println();
  101.        System.out.print("Quarterly totals");
  102.  
  103.       int qTotal;
  104.       for (int q=0; q<4; q++) {
  105.          qTotal = 0;
  106.          for (int i=0;i<people; i++) {
  107.  
  108.                qTotal = qTotal + salesFigures[i][q];
  109.  
  110.          }
  111.  
  112.          System.out.print("\t\t"+qTotal);  
  113.  
  114.      }
  115.  
  116.       System.out.println();
  117.       System.out.print("\t\t\t\t\t\t\t\t\t\t\tTotal Sales = £" +maxtotal+",000"  );
  118.       System.out.println();
  119.       System.out.println();
  120.       System.out.print("Star performers by quarters are:");
  121.       System.out.println();
  122.  
  123.  
  124.  
  125.  
  126.  
  127.       int maxsales;
  128.       String maxnames=("");
  129.  
  130.  
  131.       for (int q=0; q<4; q++) {
  132.  
  133.          maxsales = 0;
  134.  
  135.          for (int i=0;i<people;i++) {
  136.              if (salesFigures[i][q]>maxsales) {
  137.  
  138.  
  139.                  maxsales=salesFigures[i][q];
  140.  
  141.  
  142.                      //System.out.println("Quarters\t" +maxsales) ;
  143.  
  144.                       System.out.println("Quarters\t" +names[i]) ;
  145.  
  146.                   }  
  147.  
  148.              }
  149.  
  150.       //System.out.println("Quarters\t" +names[i]) ;
  151.   }            
  152.  
  153.  
  154.  
  155.  
  156.   }//main     
  157. }//InputFile
  158.  
  159.  
owz owz is offline
Newbie
 
Join Date: Nov 2006
Posts: 13
#2: Mar 7 '07

re: Display Strings from a 2 dimensional array


Quote:

Originally Posted by owz

This is what im trying 2 get
Sales Figures for 2006 in £000's


Sales staff name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter Max Min Total

John Smith 24 30 15 17 30 15 86
Joe Jones 24 19 10 20 24 10 73
Jill Evans 24 21 20 28 28 20 93

Quarterly totals 72 70 45 65

Total sales are £252,000


Star performers by quarters are:
Quarter 1 John Smith
Quarter 2 John Smith
Quarter 3 Jill Evans
Quarter 4 Jill Evans


i am haveing problems with displaying the star performers. I have got the code 2 display the correct value for the quarter, but the names do not work and are duplicated.

this is what i get:

Star performers by quarters are:

Quarters John Smith

Quarters Paul Evans correct

Quarters John Smith

Quarters John Smith correct

Quarters Jill Evans

Quarters Paul Evans

Quarters Steven Smith correct

Quarters John Smith

Quarters Joe Jones

Quarters Jill Evans correct

all help is welcome. thanx in advance.



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3.  
  4. public class SalesDetails
  5. {
  6.  
  7.         public static void main(String[] args) throws IOException
  8.         {
  9.  
  10.  
  11.     final String   textFilesPath = "C:\\Documents and Settings\\owz\\Desktop\\UNI WORK\\Comp Programming\\";//f:\Desktop\UNI WORK\computer programming
  12.     BufferedReader inputFile;
  13.     String        name;
  14.     int             year;
  15.     String [] names=new String[8];
  16.     int [] [] salesFigures= new int [8] [4];
  17.     int people=0;
  18.     int max;
  19.     int min;
  20.     int total;
  21.     int maxtotal=0;
  22.  
  23.     inputFile = Text.open ( textFilesPath + "sales details.txt" );
  24.  
  25.     System.out.println ( "Salesmen Name\\Sales Figures" );
  26.  
  27.  
  28.     year= Text.readInt (inputFile);
  29.     name= Text.readLine (inputFile);
  30.  
  31.     while ( name.equals ( "****" ) == false )
  32.     {
  33.        names[people]= name;
  34.         for (int q=0; q<4; q++) {    
  35.  
  36.  
  37.         salesFigures[people][q]= Text.readInt ( inputFile );
  38.         }
  39.     people=people +1;
  40.  
  41.  
  42.  
  43.       name = Text.readLine ( inputFile );
  44.  
  45.     }//end-while
  46.  
  47.     inputFile.close();
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     System.out.print("\t\t\t\t\t");    System.out.println("Sales Figures for 2007 in £000's \n");
  56.  
  57.     System.out.println("Sales staff name\t       1st quarter\t       2st quarter\t       3st quarter\t       4st quarter\t      Max\t       Min\t           Total \n");
  58.  
  59.       for (int i=0;i<people; i++) {
  60.           System.out.print(names[i]);
  61.           if (names[i].length()<=11) {
  62.               System.out.print("\t");
  63.           }
  64.           for (int q=0; q<4; q++) {    
  65.  
  66.  
  67.          System.out.print("\t\t"+ salesFigures[i][q]);
  68.           }
  69.               max=salesFigures[i][0];
  70.               min=salesFigures[i][0];
  71.           for (int q=1; q<4; q++) {
  72.  
  73.           if (salesFigures[i][q]>max) {
  74.               max=salesFigures[i][q];
  75.           }
  76.             if(salesFigures[i][q]<min) 
  77.             min=salesFigures[i][q];
  78.           } 
  79.  
  80.  
  81.              total=0;
  82.               for (int q=0; q<4; q++) {
  83.  
  84.               total=total+salesFigures[i][q];
  85.  
  86.                  }     
  87.  
  88.  
  89.          {               
  90.              maxtotal = maxtotal + total;
  91.  
  92.         System.out.print("\t       "+ max +"\t        "+ min+"\t\t  "+ total);
  93.  
  94.  
  95.          System.out.println(); 
  96.  
  97.          }//output max,min and total
  98.  
  99.       }//ouput people and they're figures 
  100.        System.out.println();
  101.        System.out.print("Quarterly totals");
  102.  
  103.       int qTotal;
  104.       for (int q=0; q<4; q++) {
  105.          qTotal = 0;
  106.          for (int i=0;i<people; i++) {
  107.  
  108.                qTotal = qTotal + salesFigures[i][q];
  109.  
  110.          }
  111.  
  112.          System.out.print("\t\t"+qTotal);  
  113.  
  114.      }
  115.  
  116.       System.out.println();
  117.       System.out.print("\t\t\t\t\t\t\t\t\t\t\tTotal Sales = £" +maxtotal+",000"  );
  118.       System.out.println();
  119.       System.out.println();
  120.       System.out.print("Star performers by quarters are:");
  121.       System.out.println();
  122.  
  123.  
  124.  
  125.  
  126.  
  127.       int maxsales;
  128.       String maxnames=("");
  129.  
  130.  
  131.       for (int q=0; q<4; q++) {
  132.  
  133.          maxsales = 0;
  134.  
  135.          for (int i=0;i<people;i++) {
  136.              if (salesFigures[i][q]>maxsales) {
  137.  
  138.  
  139.                  maxsales=salesFigures[i][q];
  140.  
  141.  
  142.                      //System.out.println("Quarters\t" +maxsales) ;
  143.  
  144.                       System.out.println("Quarters\t" +names[i]) ;
  145.  
  146.                   }  
  147.  
  148.              }
  149.  
  150.       //System.out.println("Quarters\t" +names[i]) ;
  151.   }            
  152.  
  153.  
  154.  
  155.  
  156.   }//main     
  157. }//InputFile
  158.  
  159.  

No problem have solved it now needed 2 diclear the array
salesFiugres[0][0];
Reply