Results 1 to 10 of 10
 03072009, 03:45 AM #1Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
What did i do wrong on thing method?
What did i do wrong on thing("this") method?
i recieved an error that the program is out of bound
i know i did someting wrong with the method in greed text.
Im looking at it and i cant find out where the error occurs.
can someone please tell me?
Java Code:import java.util.Scanner; public class sort2D { public static void main (String [] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer for number of rows range from 3 to 10: "); int m = input.nextInt(); while (m<3  m>10) { System.out.println("Enter an integer for number of rows range from 3 to 10 again: "); m = input.nextInt(); } System.out.println("Enter an integer for number of columns range from 3 to 10: "); int n = input.nextInt(); while (n<3  n>10) { System.out.println("Enter an integer for number of columns range from 3 to 10 again: "); n = input.nextInt(); } int [] [] arr = new int [m] [n]; //generate random numbers for (int i=0; i<arr.length; i++) { for (int j=0; j<arr[n].length; j++) { arr [i][j]= (int)((Math.random()*30)+1); } } //display arr int [][] arr1 = decRow(arr); for (int i=0; i<arr.length; i++) { for(int j=0; j<arr[n].length; j++) { System.out.print(arr1[i][j]+" "); } } }//end of main [COLOR="SeaGreen"]//method for sorting numbers in each row public static int[][] decRow(int[][] arr) { int [][] newArr = new int[arr.length][arr[0].length]; for (int i=0; i<arr.length; i++) { for (int j = arr.length  1; j >= 1; j) { int max = arr[i][0]; int maxloc =0; for (int k=1; k<=j; k++) { if(max<arr[i][k]) { max = arr[i][k]; maxloc = j; } } if (maxloc != j) { newArr[i][maxloc] = arr[i][j]; newArr[i][j] = max; } } } return newArr; }[/COLOR] }//end of class
Last edited by PureAwesomeness; 03072009 at 03:56 AM.
 03072009, 04:00 AM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
please let us know exactly what and where your error is. i don't see any thing("this") method
 03072009, 04:05 AM #3Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
please let us know exactly what and where your error is. i don't see any thing("this") method
i need someone help me check the method in green code
which is the one below
Java Code://method for sorting numbers in each row public static int[][] decRow(int[][] arr) { int [][] newArr = new int[arr.length][arr[0].length]; for (int i=0; i<arr.length; i++) { for (int j = arr.length  1; j >= 1; j) { int max = arr[i][0]; int maxloc =0; for (int k=1; k<=j; k++) { if(max<arr[i][k]) { max = arr[i][k]; maxloc = j; } } if (maxloc != j) { newArr[i][maxloc] = arr[i][j]; newArr[i][j] = max; } } } return newArr; }
 03072009, 05:03 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Again, can you explain what your method suppose to do.
 03072009, 05:23 AM #5Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
sorry! that i didnt explain what the this method suppose to do
i think i figured out the "out of bound error"
example
5213
4562
0542
the ideal output is
5321
6542
5420
im trying to use selection sort for the numbers in each row
Java Code://method for sorting numbers in decending order in each rows public static int[][] decRow(int[][] arr) { int [][] newArr = new int[arr.length][arr[0].length]; for (int i=0; i<arr.length; i++) { for (int j =1; j <= arr[0].length  1; j++) { int min = arr[i][0]; int minloc =0; for (int k=1; k<=j; k++) { if(min>arr[i][k]) { min = arr[i][k]; minloc = j; } } if (minloc != j) { newArr[i][minloc] = arr[i][j]; newArr[i][j] = min; } } } return newArr; }
 03072009, 05:26 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
How did you match the input into 2D array?
 03072009, 06:00 AM #7Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
How did you match the input into 2D array?
 03072009, 06:05 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Your input is this.
5213
4562
0542
But you want to pass 2D array, into the method decRow. How did you match those two.
 03072009, 06:28 AM #9Member
 Join Date
 Jan 2009
 Posts
 90
 Rep Power
 0
that is just an example
i create an array and generate random numbers with in it then pass it to the method
is this what you meant?
Java Code:import java.util.Scanner; public class sort2D { public static void main (String [] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer for number of rows range from 3 to 10: "); int m = input.nextInt(); while (m<3  m>10) { System.out.println("Enter an integer for number of rows range from 3 to 10 again: "); m = input.nextInt(); } System.out.println("Enter an integer for number of columns range from 3 to 10: "); int n = input.nextInt(); while (n<3  n>10) { System.out.println("Enter an integer for number of columns range from 3 to 10 again: "); n = input.nextInt(); } int [] [] arr = new int [m] [n]; //generate random numbers for (int i=0; i<arr.length; i++) { for (int j=0; j<arr[n].length; j++) { arr [i][j]= (int)((Math.random()*30)+1); } } //display arr int [][] arr1 = decRow(arr); for (int i=0; i<arr.length; i++) { for(int j=0; j<arr[n].length; j++) { System.out.print(arr1[i][j]+" "); } }
 03082009, 09:37 AM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Ya, it's fine. Reason to ask that question is your requirement is not clear. Better to explain it more clearly. Your code posted earlier and the input/output make some messup.
Similar Threads

method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03262010, 06:12 PM 
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02252009, 08:14 PM 
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09252008, 07:55 PM 
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05072008, 08:37 AM 
PLz i really need help on this final thing
By jason27131 in forum New To JavaReplies: 2Last Post: 08032007, 02:31 AM
Bookmarks