Results 1 to 10 of 10
- 03-07-2009, 02:45 AM #1
Member
- 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 classLast edited by PureAwesomeness; 03-07-2009 at 02:56 AM.
- 03-07-2009, 03:00 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
please let us know exactly what and where your error is. i don't see any thing("this") method
- 03-07-2009, 03:05 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
there is a typo on my title suppose to be "this" not "thing"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; }
- 03-07-2009, 04:03 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Again, can you explain what your method suppose to do.
- 03-07-2009, 04:23 AM #5
Member
- 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; }
- 03-07-2009, 04:26 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How did you match the input into 2D array?
- 03-07-2009, 05:00 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
i don't get what do you mean by that.How did you match the input into 2D array?
- 03-07-2009, 05:05 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Your input is this.
5213
4562
0542
But you want to pass 2D array, into the method decRow. How did you match those two.
- 03-07-2009, 05:28 AM #9
Member
- 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]+" "); } }
- 03-08-2009, 08:37 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 mess-up.
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM -
PLz i really need help on this final thing
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks