Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Errors in my Simple Java code

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Unhappy Errors in my Simple Java code

    Hi,

    I have this simple java code:

    public class PriceAndWeight {
     
    	  //price on the first line and weight on the second line
    	   private static double DoubleDimentionArrayA[][] = {{0.60,50},{0.90,100},{1.30,250},{2.10,500},{3.50,1000},{5.50,2000},{0.0,0}};
    	   	private double[] biggestWeight = {};
    		private double compareBiggestPrize = 0;
     
    	public static double biggestWeightOfLetterWithArray(double[][] DoubleDimentionArrayA) {
    	 for(int i = 0; i<DoubleDimentionArrayA.length; ++i){
    	   for(int j = 0; j<DoubleDimentionArrayA[i].length; ++j){
    	     	if(DoubleDimentionArrayA[i]<=0.6){
    	      	biggestWeight[i] = DoubleDimentionArrayA[i][j];
    	      	}
          }

    I am having some errors on these lines:
    if(DoubleDimentionArrayA[i]<=0.6){
    biggestWeight[i] = DoubleDimentionArrayA[i][j];
    It complained on the first line that : "The operator <= is undefined for the argument type(s) double[], double"
    and on the second line, it complains that : "change modifier of biggestWeight to static"

    The first error, I don't quite know why since I am comparing the double in the 2D array with 0.6 to see if it is less than or equal to 0.6. For the second error, do I have to necessarily make the modifier static before it works and why is it even complaining of this?

    Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Errors in my Simple Java code

    The error message is clear, so try to understand what it's telling you:

    The operator <= is undefined for the argument type(s) double[], double

    It says that you're comparing a type double[] to a type double, a double array to a double value. How can that be happening? Well, look at the statement again:

    if(DoubleDimentionArrayA[i]<=0.6)

    What is DoubleDimentionArrayA[i]? Thanks to your great variable naming (though not great spelling) the symbol DoubleDimentionArrayA[i] designates the whole ROW of a 2-dimensional array of doubles, but not a SINGLE double value the same as DoubleDimentionArrayA[i][j] would.

    Get it?

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Errors in my Simple Java code

    Oh stupid me! thanks. I didn't even see it that my using a double array was affecting it. I just was wondering why my comparing the value of a double array and a double value was wrong until you pointed out the double array thing. What about the second error? Do I necessarily have to make the biggestWeight variable static before using it and why?

    Thanks

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Errors in my Simple Java code

    Ah, I ignored that one. This statement has bigger problems:

    private double[] biggestWeight = {};

    I don't know the reason for the error you've gotten - it may be the compiler doing its best to deal with an unusual situation, but I also don't know why you've written the statement above. What are you trying to do?

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Errors in my Simple Java code

    Ok, I now tried to work around the error but I guess I must have misunderstood how to work with double dimension arrays in Java. I have two different arrays as follows :

    private static int weight [] = {50, 100, 250, 500, 1000, 2000, 0};
    private static double prices [] = {0.60, 0.90, 1.30, 2.10, 3.50, 5.50, 0};

    and I want to convert these two arrays into one 2D array. I tried this:
    private static double DoubleDimensionArrayA[][] = {{0.60,50},{0.90,100},{1.30,250},{2.10,500},{3.50, 1000},{5.50,2000},{0.0,0}};

    Where the price is in the first line and the weight on the second line. Is this the correct way to represent the two arrays above?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Errors in my Simple Java code

    You're making my head hurt and a bad design choice. Using parallel arrays to track program data is bad enough, but combining single-dimension parallel arrays into multi-dimensional parallel arrays is a recipe for disaster.

    Java provides the tools to do OOP. Create a class with the necessary fields to track the desired data. Create objects of the class and populate the object's data with the desired values, and put those OBJECTS into an array, if you must. For example, for the data you've shown, the Class might be ItemForSale with two fields, weightInPounds and pricePerPound.

    I'm guessing at names and measurements, but I hope you get the idea. If you must use parallel arrays (and I hope you don't), then stick to single-dimension arrays.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 7th, 2014)

Similar Threads

  1. Replies: 7
    Last Post: May 8th, 2014, 12:51 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. [SOLVED] Beginner java: I'm getting one of three errors everytime I run my code?
    By Benner in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 21st, 2013, 12:52 AM
  4. Simple program with errors
    By mstratmann in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 30th, 2012, 11:26 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM

Tags for this Thread