Results 1 to 4 of 4
  1. #1
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default return position in array

    Hi,
    my code below fills an array with random numbers and asks the user to guess if a number is located in a particular column. If the number is in that column it will print true, if not it will print false.

    If the method check is true how do i return the position where the number was located in the array and print it?

    Thanks for your help.

    Java Code:
    import java.util.Scanner;
    public class ColContains {
    	public static void main(String [] args){
    		Scanner in = new Scanner(System.in);
    		
    		System.out.println("What row size array would you like?");
    		int rowSize = in.nextInt();
    		
    		System.out.println("What column size array would you like?");
    		int colSize = in.nextInt();
    		
    		int [][]a = fillarray(rowSize, colSize);
    		
    		System.out.println("Select a number between 0 and 4?");
    		int number = in.nextInt();
    		
    		System.out.println("Guess a column this number is contained in from 0 to " + (colSize-1) + "?");
    		int col = in.nextInt();
    		
    		boolean answer = check(a, number, col);
    		System.out.println(answer);
    		print(a);
    		
    	}
    	
    	public static boolean check(int [][]a, int value, int col ){
    		for(int k=0; k<a.length; k++){
    			if(a[k][col]==value){
    				return true;
    			}
    		}
    		return false;
    	}
    	
    	public static void print(int [][] a){
    		for(int i=0; i<a.length; i++){
    			for(int j=0; j<a[i].length; j++){
    				
    				System.out.print(a[i][j]);
    				System.out.print(" ");
    
    			}
    		System.out.println("");
    		}
    		
    	}
    	
    	public static int[][] fillarray(int rowSize, int colSize){
    		int[][] a = new int [rowSize][colSize];
    		
    		for(int i=0; i<a.length; i++){
    			for(int j=0; j<a[i].length; j++){
    				
    				a[i][j] = (int) (Math.random()*5);
    
    			}
    		}
    		return a;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: return position in array

    If the method check is true how do i return the position where the number was located
    You might change what check() returns to be the index vs a boolean. Look at the String class's indexOf method for an example.

  3. #3
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: return position in array

    Should i change the return type to an int or int [][]?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: return position in array

    If check() is to return the index to the found value, that would be an int.

Similar Threads

  1. Get Characted at Array Position?
    By doa24uk in forum New To Java
    Replies: 3
    Last Post: 04-20-2011, 07:33 AM
  2. Return multiply array changes?
    By Teclis in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 07:16 AM
  3. return array
    By rfviki in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 01:05 PM
  4. return array problem
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-30-2010, 05:08 PM
  5. exchange the position of array elements
    By hacikho in forum New To Java
    Replies: 6
    Last Post: 09-20-2009, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •