10 Replies - 758 Views - Last Post: 13 March 2010 - 05:40 PM Rate Topic: -----

#1 sammc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 10

Compile errors of Java Array

Posted 11 March 2010 - 06:12 AM

Hi,
I have a project that is receiving compilation errors, which I am not sure how to fix. Each time I make changes in an attempt to fix my code I end up with more errors. Can someone look into?

Commissions8Test is as follows:
public class Commissions8Test
{
   public static void main( String[] args )// ..................................main method begins program execution
   {

      int[] salarysArray = { 8700, 6800, 9400, 10000, 8300, 7800, 8500, 9100, 7600, 8700 }; // .....one-dimensional array of employee salarys

      Commissions8 myCommissions8 = new Commissions8( salarysArray );
      myCommissions8.processsalarys();
   } // ........................................................................end main
} // ...........................................................................end class Commissions8Test



Commissions8 is as follows:
public class Commissions8
{
   private double[] salarys; // ..................................................8.Barray of employee salarys


   public Commissions8( salarysArray )// ..................11two-argument constructor initializes courseName and salarys array
   {
      salarys = salarysArray; // .............................................14store salarys
   } //.......................................................................15end two-argument Commissions8 constructor


   public void processsalarys()// ..............................................METHOD... processsalrys
   {
      System.out.println();//...................................................print line
        outputBarChart(); // ...................................................Calls outputBarChart to print salary distribution chart
   } // ........................................................................end method processsalarys


   public void outputBarChart()// ..............................................METHOD... outputBarChart
   {
      System.out.println( "Salary Distribution:" );//...........................HEADER... "Salary Distribution"
      int[] frequency = new int[ 11 ];// .......................................Declares/Create array freq. of 11 ints to store the frequency of salary's in each salary category
      for ( double salary : salarys )// ...........................................for each salary, increment the appropriate frequency
         //++frequency[ salary / 1000 ];
         ++frequency[(salary * .09 + 200) / 1000 ];
      for ( int count = 0; count < frequency.length; count++ )// ...............for each salary frequency, print bar in chart
      {
         if ( count == 10 )// ..................................................output bar label ( "000-099: ", ..., "900-999: ", "1000: " )
            System.out.printf( "%5d: ",   1000 );
         else
            System.out.printf( "%02d-%02d: ",
               count * 1000, count * 1000 + 999  );
         for ( int stars = 0; stars < frequency[ count ]; stars++ )// ..........print bar of asterisks
            System.out.print( "*" );
         System.out.println(); // ..............................................start a new line of output
      } // .....................................................................end outer for
   } // ........................................................................end output bar chart displaying salary distribution
} // ...........................................................................end class Commissions8



Compilation errors are as follows

Commissions8Test.java:
Line 11 ... Commissions8 myCommissions8 = new Commissions8( salarysArray );
Cannot find symbol
symbol: Constructor Commissions8(int[])
location: class Commissions8

Commissions8.java:
Line 10 ... public Commissions8( String name, salarysArray )
Cannot find symbol
symbol: class salarysArray
location: class Commissions8
<identifier> expected

Line 12 ... salarys = salarysArray;
Cannot find symbol
symbol: variable salarysArray
location: class Commissions8

Line29 ... ++frequency[(salary * .09 + 200) / 1000 ];
possible loss of precision
found : double
required : int

THx,
Samm

Is This A Good Question/Topic? 0
  • +

Replies To: Compile errors of Java Array

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Compile errors of Java Array

Posted 11 March 2010 - 07:11 AM

array should be initialized like so...
      int[] salarysArray = new int[] {1200, 2011, 2333} ; // 


Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: Compile errors of Java Array

Posted 11 March 2010 - 07:48 AM

Quote

Commissions8Test.java:
Line 11 ... Commissions8 myCommissions8 = new Commissions8( salarysArray );
Cannot find symbol
symbol: Constructor Commissions8(int[])
location: class Commissions8

This error is telling you that you haven't defined a constructor to accept an int param. For this constructor public Commissions8( salarysArray ), you need to define type for the salarysArray parameter. Remember- all variables have type and name.


Quote

Line 10 ... public Commissions8( String name, salarysArray )
Cannot find symbol
symbol: class salarysArray
location: class Commissions8
<identifier> expected

Again, you need to define type for salarysArray.

Quote

Line 12 ... salarys = salarysArray;
Cannot find symbol
symbol: variable salarysArray
location: class Commissions8

This error is a result of not defining the type for the salarysArray parameter, as I pointed out in the previous two errors.

Quote

Line29 ... ++frequency[(salary * .09 + 200) / 1000 ];
possible loss of precision
found : double
required : int

This error is telling you that you can't use doubles as the indices for an array. The .09 literal makes everything a double, so you need to either add a cast to an int or make it an int by modifying your formula.
Was This Post Helpful? 1
  • +
  • -

#4 sammc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 10

Re: Compile errors of Java Array

Posted 11 March 2010 - 08:34 AM

Thank you ... Checking it out now.
Samm
Was This Post Helpful? 0
  • +
  • -

#5 sammc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 10

Re: Compile errors of Java Array

Posted 11 March 2010 - 01:12 PM

Better ... Down to the one error concerning the division (++frequency[(salary * .09 + 200) / 1000 ];)
Still with the same error on that

++frequency[(salary * .09 + 200) / 1000 ];
possible loss of precision
found : double
required : int



Commissions8Test

// Chapter 7 - Problem 7.10
// Example on Pg 273
// Commissions8.java - 03.11.10 v3
public class Commissions8Test
{
   public static void main( String[] args )// ..................................main method begins program execution
   {
      double[] salarysArray = new double[]{ 8700, 6800, 9400, 10000, 8300, 7800, 8500, 9100, 7600, 8700 }; // .....one-dimensional array of employee salarys
      // int[] salarysArray = new int[] {1200, 2011, 2333}
      Commissions8 myCommissions8 = new Commissions8( salarysArray );
      myCommissions8.processSalarys();
   } // ........................................................................end main
} // ...........................................................................end class Commissions8Test



Commissions8
//// Chapter 7 - Problem 7.10
// Example on Pg 269
// Commissions8.java - 03.10.10 v4

public class Commissions8
{
   private double[] salarys; // ..................................................8.Barray of employee salarys


   public Commissions8( double[] salarysArray )// ..................11two-argument constructor initializes courseName and salarys array
   {
      salarys = salarysArray; // .............................................14store salarys
   } //.......................................................................15end two-argument Commissions8 constructor


   public void processSalarys()// ..............................................METHOD... processsalrys
   {
        outputBarChart(); // ...................................................Calls outputBarChart to print salary distribution chart
   } // ........................................................................end method processsalarys


   public void outputBarChart()// ..............................................METHOD... outputBarChart
   {
      System.out.println( "Salary Distribution:" );//...........................HEADER... "Salary Distribution"
      int[] frequency = new int[ 11 ];// .......................................Declares/Create array freq. of 11 ints to store the frequency of salary's in each salary category
      for ( double salary : salarys )// ...........................................for each salary, increment the appropriate frequency
         //++frequency[ salary / 1000 ];
         ++frequency[(salary * .09 + 200) / 1000];
      for ( int count = 0; count < frequency.length; count++ )// ...............for each salary frequency, print bar in chart
      {
         if ( count == 10 )// ..................................................output bar label ( "00-09: ", ..., "90-99: ", "100: " )
            System.out.printf( "%5d: ",   1000 );
         else
            System.out.printf( "%02d-%02d: ",
               count * 1000, count * 1000 + 999  );
         for ( int stars = 0; stars < frequency[ count ]; stars++ )// ..........print bar of asterisks
            System.out.print( "*" );
         System.out.println(); // ..............................................start a new line of output
      } // .....................................................................end outer for
   } // ........................................................................end output bar chart displaying salary distribution
} // ...........................................................................end class Commissions8



I spoke with my Professor, he thought I needed to:
Return the result to an int, and use that as the index to the freq array.

I need to read up on that. Don't understand at the moment.

Samm
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Compile errors of Java Array

Posted 11 March 2010 - 01:18 PM

You need to type cast to an int type..as it requires an int

change this
++frequency[(salary * .09 + 200) / 1000];



to this
    ++frequency[(int) ((salary * .09 + 200) / 1000)];


Was This Post Helpful? 1
  • +
  • -

#7 sammc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 10

Re: Compile errors of Java Array

Posted 11 March 2010 - 04:40 PM

Works ... Thanks all!

Samm


Commissions9Test
public class Commissions9Test
{
   public static void main( String[] args )// ..................................main method begins program execution
   {
      double[] salarysArray = new double[]{ 87000, 68000, 94000, 10000, 83000, 78000, 85000, 91000, 76000, 87000 }; // .....one-dimensional array of employee salarys
      Commissions9 myCommissions9 = new Commissions9( salarysArray );
      myCommissions9.processSalarys();
   } // ........................................................................end main
} // ...........................................................................end class Commissions9Test



Commissions9
public class Commissions8
{
   private double[] salarys; // ................................................array of employee salarys


   public Commissions8( double[] salarysArray )// ..............................constructor initializes salarys array
   {
      salarys = salarysArray; // ...............................................store salarys
   } //.........................................................................end two-argument Commissions8 constructor


   public void processSalarys()// ..............................................METHOD... processsalrys
   {
        outputBarChart(); // ...................................................Calls outputBarChart to print salary distribution chart
   } // ........................................................................end method processsalarys


   public void outputBarChart()// ..............................................METHOD... outputBarChart
   {
      System.out.println( "Salary Distribution:" );//...........................HEADER... "Salary Distribution"
      int[] frequency = new int[ 11 ];// .......................................Declares/Create array freq. of 11 ints to store the frequency of salary's in each salary category
      for ( double salary : salarys )// ........................................for each salary, increment the appropriate frequency
         ++frequency[(int) ((salary * .09 + 200) / 1000)];//....................CALCULATION ...
      for ( int count = 0; count < frequency.length; count++ )// ...............for each salary frequency, print bar in chart
      {
         if ( count == 10 )// ..................................................output bar label ( "00-999:, 1000-1999:, "10000: " )
            System.out.printf( "%5d: ",   10000 );
         else
            System.out.printf( "%02d-%02d: ",
               count * 1000, count * 1000 + 999  );
         for ( int stars = 0; stars < frequency[ count ]; stars++ )// ..........print bar of asterisks
            System.out.print( "*" );
         System.out.println(); // ..............................................start a new line of output
      } // .....................................................................end outer for
   } // ........................................................................end output bar chart displaying salary distribution
} // .....................................


Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Compile errors of Java Array

Posted 11 March 2010 - 04:57 PM

No Problem :)
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: Compile errors of Java Array

Posted 11 March 2010 - 06:30 PM

I'd like to expand on array indexing a little. To start, think of an array like a bookshelf with a limited space, which we'll call length. We'll now index these books from 0 through length-1. So if we have 4 books, length = 4, then we'll index those books [0, 1, 2, 3]. Note that we don't ever have element 2.3, 1.5, 0.7, etc. All of the indices are integer values. So therefore, if you have a double or a float value as the index, Java assumes that it isn't a mathematical integer because doubles and floats are designed to hold all real numbers, not just ints, so that's why you get an error.
Was This Post Helpful? 2
  • +
  • -

#10 sammc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 10

Re: Compile errors of Java Array

Posted 13 March 2010 - 05:21 PM

I had a hard time the arrays and calculations on the arrays. Thanks for explaining it the way you did. Makes alot more sense now.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: Compile errors of Java Array

Posted 13 March 2010 - 05:40 PM

Not a problem. Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1