Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default 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; 03-07-2009 at 02:56 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    please let us know exactly what and where your error is. i don't see any thing("this") method

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    please let us know exactly what and where your error is. i don't see any thing("this") method
    there is a typo on my title suppose to be "this" not "thing"
    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;
    	}

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default 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;
    	}

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    How did you match the input into 2D array?
    i don't get what do you mean by that.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Your input is this.

    5213
    4562
    0542

    But you want to pass 2D array, into the method decRow. How did you match those two.

  9. #9
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    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]+" ");
    		}
    	}

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM

Posting Permissions

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