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 2 of 2

Thread: type mismatch problem

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default type mismatch problem

    Here is a problem im working on for my intro to java class:
    1.Design a class named Location for locating a maximal value and its location in a twodimensional
    array. The class contains:
    2.Private data fields row and column that store the indices it a two dimensional
    array as int type.
    3.A no-arg constructor that creates a location with default values.
    4.A constructor with the arguments for row and column.
    5.The method named locateLargest(double [][] x) that returns the location of the
    largest element in a two-dimensional array. The return value is an instance of
    Location. This method should also print the largest value of the array.
    2/3
    The method is: public Location locateLargest(double [][] x) {}
    6.A toString() method that prints a Location object in the form [row][column].


    My code:
    package classAssignment;
    import java.util.Scanner;
    public class Location {
    	private int row;
    	private int column;
    	private int[][] array = new int[row][column];
    	public static void main(String[] args) {
    		Scanner numberInput = new Scanner(System.in);
    		System.out.println("Enter the number of rows and columns of the array: ");
    		int row = numberInput.nextInt();
    		int column = numberInput.nextInt();
    		Location l1 = new Location(row, column);
     
     
    	}//end main
     
    	Location(){}//end constructor
     
    	Location(int row, int column){
    		this.row = row;
    		this.column = column;
    	}//end arg constructor
     
     
     
    	public Location locateLargest(double[][] x){
    		double max = 0;
    		for (int i = 0; i < x.length; i++){
    			for (int j = 0; j < x.length; i ++){
    				if (x[i][j] > max)
    					max = x[i][j];
    			}
    		}
     
    		return x;
     
    	}//end locateLargest()
     
    	public int getRow(){
    		return this.row;
    	}//end getRow()
     
    	public int getColumn(){
    		return this.column;
    	}//end getColumn()
     
    }//end class

    My problem is in the locateLargest method. I can't return the max because it is of type location and I have no idea how to convert it into a double. I've even tried declaring the array in multiple other places like in another class or in the locateLargest method itself but those ideas didn't work either. Any help would be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: type mismatch problem

    can't return the max because it is of type location and I have no idea how to convert it into a double.
    Use the new statement to create an instance of the Location class with the row and column where the largest was found. Then return it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Type mismatch assigning factory class result to subclass
    By roger1990 in forum Object Oriented Programming
    Replies: 5
    Last Post: July 28th, 2013, 11:23 PM
  2. Type mismatch: cannot convert from void to ClassX.MethodX
    By RevMoses77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 12:05 PM
  3. user input mismatch problem
    By JavaN00b101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 07:02 AM
  4. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM