Results 1 to 4 of 4
Thread: return position in array
- 11-14-2011, 07:07 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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; } }
- 11-14-2011, 08:11 PM #2
Re: return position in array
You might change what check() returns to be the index vs a boolean. Look at the String class's indexOf method for an example.If the method check is true how do i return the position where the number was located
- 11-19-2011, 07:52 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Re: return position in array
Should i change the return type to an int or int [][]?
- 11-19-2011, 09:08 PM #4
Similar Threads
-
Get Characted at Array Position?
By doa24uk in forum New To JavaReplies: 3Last Post: 04-20-2011, 07:33 AM -
Return multiply array changes?
By Teclis in forum New To JavaReplies: 4Last Post: 04-20-2011, 07:16 AM -
return array
By rfviki in forum New To JavaReplies: 2Last Post: 11-03-2010, 01:05 PM -
return array problem
By doha786 in forum New To JavaReplies: 3Last Post: 03-30-2010, 05:08 PM -
exchange the position of array elements
By hacikho in forum New To JavaReplies: 6Last Post: 09-20-2009, 10:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks