6 Replies - 793 Views - Last Post: 30 November 2012 - 10:15 AM Rate Topic: -----

#1 lphthephunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

bad operand type for binary operator ">="

Posted 29 November 2012 - 09:24 PM

I'm trying to assign letter grades to the test average of students in an array. I have written all of this code so far, and this is the only part that has left me stumped. The values in the array testavg are obviously in array format, and I'm trying to tell the program that if the students test average is >= 90, then they will receive an 'A'. My question here is what do I need to do to get this operator to work? Any help would be greatly appreciated.




public class table
{
	public static void main(String[] args)
	{

		String names[][]=
		{
			{"Adam  "},
		    {"Smith "},
			{"Jones "},
			{"Becky "},
			{"Taylor"}
		};
		int Test1[][] =
		{
			{90},
			{89},
			{86},
			{76},
			{95}
		};
		int Test2[][] =
		{
			{92},
			{79},
			{85},
			{90},
			{87}
		};
		int Test3[][] =
		{
			{93},
			{80},
			{90},
			{87},
			{92}
		};
		int Final[][] =
		{
			{90},
			{77},
			{86},
			{92},
			{89}
		};



		double testavg[] =
		{
			91.25,
			81.25,
			86.75,
			86.25,
			90.75

		};

		char grade;

		if (testavg >= 90.0)  //this is the trouble area
		{
			grade = 'A';
		}
		double totalpoints[][] =
		{
			{154.55},
			{78.37},
			{86.22},
			{90.28},
			{89.63}
		};


		double grades[][] =
		{
			{},
			{},
			{},
			{},
			{}
		};

		System.out.println ("                    North Georgia College & State University");
		System.out.println ("                   Report for Spring Semester 2012\n\n");
		System.out.println ("   Name\t  Test1  Test2  Test3  Final  Average  Total Points  Grade");
		System.out.println ("   -----------------------------------------------------------------");
		for (int i=0; i<names.length; i++)
		{
			System.out.println("\n   " + names[i][0] + "  " + Test1[i][0] + "     " + Test2[i][0] + "     " + Test3[i][0] + "     " +Final[i][0] + "    " + testavg[i] + "      "
			+ totalpoints[i][0]);
		}


		System.out.println("\n\n\n");


	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: bad operand type for binary operator ">="

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: bad operand type for binary operator ">="

Posted 29 November 2012 - 09:53 PM

You cannot compare objects using the relational operators (Excluding primitive wrappers). Both sides must be of primitive type (char, byte, int, ...). What you want to do is loop through the amount of tests that have been taken using the counter as the index of the array.

		
		for (int i = 0; i < testavg.length; i++)
			if (testavg[i] >= 90)
				grade = 'A';



You also should not be having a char 'grade', but assigning them to the grades array from what I can see. This should be easy enough.

Also, for what reason do you use a 2 dimensional array ([][]) instead of a single one ([])? it is unnecessary.

This post has been edited by Mylo: 29 November 2012 - 09:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 lphthephunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: bad operand type for binary operator ">="

Posted 29 November 2012 - 09:59 PM

Thank you very much! That makes a lot of sense. I was using the multidimensional at first because I thought it may make more sense, but then realized a single array would be much more simple. I haven't gotten around to changing them yet because I've been trying to get this problem corrected.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Re: bad operand type for binary operator ">="

Posted 30 November 2012 - 06:34 AM

Check out my tutorial Moving Away From Parallel Arrays. You should really move towards classes and objects over parallel (especially parallel 2D) arrays.
Was This Post Helpful? 0
  • +
  • -

#5 lphthephunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: bad operand type for binary operator ">="

Posted 30 November 2012 - 06:36 AM

To assign the letter grades to my array for them, do I need to define an index for the portion that is
grades = 'A'; ?
Was This Post Helpful? 0
  • +
  • -

#6 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: bad operand type for binary operator ">="

Posted 30 November 2012 - 06:43 AM

Yes. If you are using grades as a char, parallel array you would need to assign it values with a given index.

ex:
some loop()
{
    if (someGrade[i] > 90)
    {
         grades[i] = 'A';
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 lphthephunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: bad operand type for binary operator ">="

Posted 30 November 2012 - 10:15 AM

View Postipushmycar, on 30 November 2012 - 06:43 AM, said:

Yes. If you are using grades as a char, parallel array you would need to assign it values with a given index.

ex:
some loop()
{
    if (someGrade[i] > 90)
    {
         grades[i] = 'A';
    }
}


Thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1