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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: 2 dime array with following methods

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 dime array with following methods

    1. reads and stores
    2. average high
    3. average low
    4.index high
    5. index low

    so far i have...
    import java.util.Scanner;

    public class Array
    {
    public static void main( String [] args )
    {
    double [] [] temp = { { 43, 3 },
    { 50, 6 },
    { 66, 32},
    { 68, 40},
    { 75, 55},
    { 88, 50},
    { 90, 66},
    { 98, 65},
    { 85, 50},
    { 76, 43},
    { 65, 42},
    { 52, 15} };


    Scanner scan = new Scanner( System.in );
    double [] getData = new double[12];
    for ( int i = 0; i < temp.length; i++ )
    {
    System.out.print( "Enter temp for month "
    + ( i + 1 ) + "\t" );
    getData[i] = scan.nextDouble( );
    }

    int indexLowTemp = 0;

    for ( int i = 1; i < temp.length; i++ )
    {
    if ( temp[i] < temp[indexLowTemp] )
    indexLowTemp = i;
    }

    System.out.println( " The smallest int appeared at index >" +
    indexLowTemp );


    int indexHighTemp = 0;

    for ( int i = 1; i < temp.length; i++ )
    {
    if ( temp[i] > temp[indexHighTemp] )
    indexHighTemp = i;
    }

    System.out.println( " The largest int appeared at index >" +
    indexHighTemp );

    double averageHigh = 0;
    for (int i=0; i<temp[i].length; i++)
    {
    double averageLow = 0;
    for (int j = 0; j < temp[j].length; j++)
    {
    averageHigh += temp[i];
    }
    averageHigh = averageHigh/temp[i].length;
    totalAverage += averageHigh;
    }
    totalAverage = totalAverage/temp.length;

    }

    with this as my major problem right now Array2.java:38: error: bad operand types for binary operator '<'
    if ( temp[i] < temp[indexLowTemp] )


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    bad operand types for binary operator '<'
    if ( temp[i] < temp[indexLowTemp] )
    What are you trying to compare? temp is a two dimensional array. The < operator does not work with arrays.
    It would work with elements of an array. For example: temp[1][2]

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    well I have found the min and max of an index with the < and > in a single dimension array. and I need to get the min and max of the array. with this array all the bigger number are in column 0 while the lower ones are in column 1 in not sure if I do it by the whole array or single columns

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    To look at an element in a two dim array, you need to use two indexes: temp[0][1]
    If you want to look at data in only one column, set the second index to the value for that column:
    temp[ix][TheDesiredColumn]

    That assumes that the first dim is the row and the second dim is the column.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    ok i tried
    if ( temp[i] < temp[0][1] )
    indexLowTemp = i;
    and if ( temp[0][1] < temp[indexLowTemp] )
    indexLowTemp = i;

    and still get the same error

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    temp[i] is an array, not an element of an array
    with a 2 dim array, you need two indexes
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    am i missing a line of code ? or is what i have correct just not set up correct

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    Can you explain. I don't understand your question.

    The compiler is giving an error because one of the operands in the comparison is an array, not an element of an array.
    You can not compare arrays with <
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    ok so i think i got a little further with
    import java.util.Scanner;

    public class Array2
    {
    public static void main( String [] args )
    {
    double [] [] temp = { { 43, 3 },
    { 50, 6 },
    { 66, 32},
    { 68, 40},
    { 75, 55},
    { 88, 50},
    { 90, 66},
    { 98, 65},
    { 85, 50},
    { 76, 43},
    { 65, 42},
    { 52, 15} };



    double indexLowTemp = temp[0][1];

    for ( int i = 1; i > temp.length; i++ )
    {

    if ( temp[0][1] > indexLowTemp )
    indexLowTemp = i;

    }
    System.out.println( " The smallest int appeared at index >" +
    indexLowTemp );
    }
    }
    it needs to ouput the lowest temp in the array which would be 3 at row 0 coulmn 1 and i cant seem to get that to output and im not even sure what index that is considered

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    not even sure what index that is considered
    You gave the answer here: row 0 coulmn 1
    those are the two indexes to the element: 3

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    ok but I'm out putting 3 instead of [0] [1] with that last code I entered. in a proper output for a array with 12 rows and 2 columns would be ?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    what is your question?
    A two dim array has two indexes for any element.


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    my question is first why with that code it says the index is 3 and not [0][1]

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    You need to format the code so it is easier to read:
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    why with that code it says the index is 3 and not [0][1]
    Obviously the code is printing the value of the element, not the indexes for the element.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    I wish I could but I'm doing this from a iPhone right now I don't have access to a comp it's te code used in entry #9. I can't seem to get the indexes to show instead of the elements

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    A suggestion: write a test program that defines a two dimensional array and uses a nested loop to print out all the elements in the array. That will require you to use indexing to access all the elements in the array.
    When you can get the test program to work, you will have more understanding of how to index an array.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    1. reads and stores
    2. average high
    3. average low
    4.index high
    5. index low

    with all these methods needed for this program should i set up a declared class file and then a seperate program to test? considering one of the methods i am going to use a .get

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    Depends on what the assignment says. I use a main() method in the class to be tested that calls all the methods in the class for testing instead of writing a separate class to put the main() method in.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    	double [] [] temp =	{  { 43, 3 },
    									{ 50, 6 },
    									{ 66, 32},
    									{ 68, 40},
    									{ 75, 55},
    									{ 88, 50},
    									{ 90, 66},
    									{ 98, 65},
    									{ 85, 50},
    									{ 76, 43},
    									{ 65, 42},
    									{ 52, 15} };
     
     
     
     
    		int indexLowTemp = 0;
    		double temp_low = Double.MAX_VALUE;
    		for (int i = 0; i < temp.length; i++)
    			if (temp[i][1] < indexLowTemp)
    			{
    				indexLowTemp = i;
    				temp_low = temp[i][1];
    			}
    		System.out.println("The smallest int appeared at index " + indexLowTemp);
     
     
     
    		int indexHighTemp = 0;
    		double temp_high = Double.MAX_VALUE;
    		for (int i = 0; i < temp.length; i++)
    			if (temp[i][1] > indexHighTemp)
    			{
    				indexHighTemp = i;
    				temp_high = temp[i][1];
    			}
    		System.out.println("The highest int appeared at index " + indexHighTemp);
     
     
    		}
     
     
     
    	}

    this is just giving me either the first index or that last index , not actually finding the min or max. and yes i need a seperate class program

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    i need a seperate class program
    What will the methods in the separate class do?


    What is being compared in the if statements in the code in post#19?
    if (temp[i][1] < indexLowTemp)
       ...
    if (temp[i][1] > indexHighTemp)

    What is to the left of the comparison operator?
    what is to the right
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    well cuz i need to use a .get somewhere in my program and
    isnt it comparing indexLow and indexHigh to the numbers i have in my array ?
    cuz i would think it would need to be
    if(temp[i][1] < temp [indexLowTemp)

    but that doest even compile

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    i would think it would need to be
    if(temp[i][1] < temp [indexLowTemp])

    but that doest even compile
    The temp array has two dimensions. The righthand reference needs to use TWO indexes, like the lefthand array reference does.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    double [] [] temp =	 { { 43, 3 },
    									{ 50, 6 },
    									{ 66, 32},
    									{ 68, 40},
    									{ 75, 55},
    									{ 88, 50},
    									{ 90, 66},
    									{ 98, 65},
    									{ 85, 50},
    									{ 76, 43},
    									{ 65, 42},
    									{ 52, 15} };
     
    	String [] string = { "high", "low", };
    			for( int i = 0; i < string.length; i++ )
    			System.out.println( " string " + i + " : "
    										+string[i] );
     
    Scanner scan = new Scanner( System.in );
    		int currentLow;
    		do
    		{
    			System.out.print( "Enter 1 for low > " );
    			currentLow = scan.nextInt( );
    		}	while ( currentLow < 0 || currentLow > 1 );
     
    		double indexLowTemp = temp[0][currentLow];
    		for ( int i = 1; i > temp.length; i++ )
    		{
    			if ( currentLow < temp[i].length )
    			{
    				//update memberMaxBill if necessary
    				if ( temp[i][currentLow] > indexLowTemp )
    					indexLowTemp = temp[i][currentLow];
    			}
     
    		}
    				System.out.println ( "\nThe min temp for "
    									+ string[currentLow] + " is "
    									+( indexLowTemp ) );
     
     
    Scanner scan1 = new Scanner( System.in );
    		int currentHigh;
    		do
    		{
    			System.out.print( "Enter 0 for high > " );
    			currentHigh = scan.nextInt( );
    		}	while ( currentHigh < 0 || currentHigh > 1 );
     
    		double indexHighTemp = temp[0][currentHigh];
    		for ( int i = 1; i < temp.length; i++ )
    		{
    			if ( currentHigh < temp[i].length )
    			{
    				//update memberMaxBill if necessary
    				if ( temp[i][currentHigh] > indexHighTemp )
    					indexHighTemp = temp[i][currentHigh];
    			}
     
    		}
    				System.out.println ( "\nThe max temp for "
    									+ string[currentHigh] + " is "
    									+( indexHighTemp ) );
     
     
     
     
     
     
     
    		}
     
     
     
    	}

    ok i tried this at it seems to work but i am getting that actually temperature reading instead of the index

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 dime array with following methods

    i am getting that actually temperature reading instead of the index
    With the following expression: theArray[theIndex]
    the value of the contents of the array is given by: theArray[theIndex]
    and the value of the index is in the variable: theIndex

    It depends on which one you want.
    Use theIndex if you want the value of the index
    Use theArray[theIndex] if you want the value from the array.

    This statement:
    indexHighTemp = temp[i][currentHigh];
    copies the contents of the array to the variable: indexHighTemp

    What do you want to put in that variable? The contents or the value of the index that is in i or currentHigh?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dime array with following methods

    i want the value of the index but i have tried everything and either it gives me the value from the array or it will just output a 0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. Needing some help with array getter and setter methods!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 5th, 2012, 07:11 PM
  4. I need help with my methods please?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:22 AM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM