Results 1 to 10 of 10
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default Array manipulation

    Hi

    i have problem here is the code for it.i have to display the numbers associated with worker id in the following format given below..

    Java Code:
    String arr[][]=new String[6][2];
    
    arr[0][0]="1573187";
    arr[0][1]="CEO id";
    arr[1][0]="2871882";
    arr[1][1]="worker id";
    arr[2][0]="36963";
    arr[2][1]="worker id";
    arr[3][0]="190444";
    arr[3][1]="Manager id";
    arr[4][0]="027634";
    arr[4][1]="worker id";
    arr[5][0]="964956";
    arr[5][1]="worker id";
    
    
    for(int i=0;i<6;i++)
    {
       for(int j=i+1;j<6;j++)
         {
            if(arr[i][1].equals(arr[j][1]))
    
              {
                 System.out.println(arr[i][1]+" "+arr[i][0]+" "+arr[j][0]);
                }
           }
      }
    ouput:
    worker id 2871882 36963
    worker id 2871882 027634
    worker id 2871882 964956
    worker id 36963 027634
    worker id 36963 964956
    worker id 027634 964956

    But
    Expected Output is
    worker id 2871882 36963 027634 964956

    can anyone please help

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Keyed data storage.

    Quote Originally Posted by Ms.Ranjan View Post
    ....can anyone please help
    Yes, use a Map of Worker objects keyed by an id field. I enjoy digging through code, but this array indexing is tedious and error prone.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default A search in a table

    Hello Ms.Ranjana

    Here is a solution for your problem. A simple way to think about two-dimensional arrays in Java is "there is no two-dimensional array". Yes, it does sound corny, but it's true. Java does not support two-dimensional arrays like Pascal for example. In Java, we have an array of arrays. So in your problem, consider the array to be an array of Strings. So the first index will specify which array you are looking at and the second index will get the element in the current array at the specified index. Consider the table below:
    Java Code:
    1573187[COLOR="White"]_[/COLOR]CEO id
    2871882[COLOR="White"]_[/COLOR]worker id
    36963[COLOR="White"]___[/COLOR]worker id
    where each row represents an array and each column represents a part of the array. Notice that the second column gives the type of ID and the first gives the value. So the indexes would be
    Java Code:
    arr[row][column]
    Here is the solution:
    Java Code:
    public class Main{
    	public static void main(String[] arg) {
    		String arr[][]=new String[6][2];
    
    		arr[0][0]="1573187";
    		arr[0][1]="CEO id";
    		arr[1][0]="2871882";
    		arr[1][1]="worker id";
    		arr[2][0]="36963";
    		arr[2][1]="worker id";
    		arr[3][0]="190444";
    		arr[3][1]="Manager id";
    		arr[4][0]="027634";
    		arr[4][1]="worker id";
    		arr[5][0]="964956";
    		arr[5][1]="worker id";
    
    
    		String search = "worker id";
    		System.out.print(search);
    		for(int i=0;i<6;i++){
    			if (arr[i][1].equals(search)){
    				System.out.print(" " + arr[i][0]);
    			}
    		}
    		System.out.println();
    	}
    }
    which gives the output
    Java Code:
    worker id 2871882 36963 027634 964956
    I hope this was what you where looking for, Ms.Ranjana. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    thankyou....but the problem is i have lots of records like this so i cannot search for worker id alone and display the number related to it

    arr[0][0]="1573187";
    arr[0][1]="CEO id";
    arr[1][0]="2871882";
    arr[1][1]="worker id";
    arr[2][0]="36963";
    arr[2][1]="worker id";
    arr[3][0]="190444";
    arr[3][1]="Manager id";
    arr[4][0]="027634";
    arr[4][1]="worker id";
    arr[5][0]="964956";
    arr[5][1]="CEO id";
    arr[6][0]="38914";
    arr[6][1]="Manger id";
    arr[7][0]="83736";
    arr[7][1]="CEO id";

    output should be:
    CEO id 1573187 964956 83736
    worker id 2871882 36963 027634
    Manager id 190444 38914

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    A suggestion on making your code more readable: use a final variable instead of a literal for the indexes.
    Instead of arr[i][0] use arr[i][WrkrNbrIdx] & arr[i][WrkrNameIdx]
    with these definitions;
    final int WrkrNbrIdx = 0;
    final int WrkrNameIdx = 1;

    System.out.println(arr[i][1]+" "+arr[i][0]+" "+arr[j][0]);
    With the above println() statement there is no way to get:
    worker id 2871882 36963 027634 964956
    I think you need to state what the assignment is more clearly. Looking at you second post, it looks like you want to find all the different worker ids and for each one, output the numbers associated with it.
    For this you'll need a way to remember all the unique worker ids, say a Map as Nicholas suggests, using the worker id as the key and storing the numbers as the value associated with the key. So you'll need to think of an class that can store the numbers.
    Last edited by Norm; 07-17-2008 at 04:48 AM.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Method added

    Hello Ms.Ranjan

    Here is the revision:
    Java Code:
    public class Main{
    	private static String arr[][]=new String[8][2];
    
    	public static void main(String[] arg) {
    		arr[0][0]="1573187";
    		arr[0][1]="CEO id";
    		arr[1][0]="2871882";
    		arr[1][1]="worker id";
    		arr[2][0]="36963";
    		arr[2][1]="worker id";
    		arr[3][0]="190444";
    		arr[3][1]="Manager id";
    		arr[4][0]="027634";
    		arr[4][1]="worker id";
    		arr[5][0]="964956";
    		arr[5][1]="CEO id";
    		arr[6][0]="38914";
    		arr[6][1]="Man[COLOR="RoyalBlue"]a[/COLOR]ger id";
    		arr[7][0]="83736";
    		arr[7][1]="CEO id";
    		printIDs("CEO id");
    		printIDs("worker id");
    		printIDs("Manager id");
    	}
    
    [COLOR="RoyalBlue"]	public static void printIDs(String key){
    		System.out.print(key);
    		for(int i = 0; i < arr.length; i++){
    			if (arr[i][1].equals(key)){
    				System.out.print(" " + arr[i][0]);
    			}
    		}
    		System.out.println();
    	}[/COLOR]
    }
    The code in blue is important. The program outputs:
    Java Code:
    CEO id 1573187 964956 83736
    worker id 2871882 36963 027634
    Manager id 190444 38914
    Does this help you, Ms.Ranjan? ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Smile

    Thankyou very much Tim,Norm and Nicholas i could solve it...

    once again thanks..

  8. #8
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    It's my pleasure Ms.Ranjan ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  9. #9
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Hey, this is fun. This is multi-dimentional array.

  10. #10
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by matt_well View Post
    Hey, this is fun. This is multi-dimentional array.
    Hello matt_well. I agree with you. You can make a datastructure with any number of dimensions. It only gets more complicated to visualize.
    Java Code:
    1 dimension[COLOR="White"]__[/COLOR]- a line
    2 dimensions - a block
    3 dimensions - a cube
    4 dimensions - a animated cube
    5 dimensions - a line of animated cubes
    6 dimensions - a block of animated cubes
    7 dimensions - a cube of animated cubes
    8 dimensions - a animated cube of animated cubes
    This is how I picture them. One can go on forever. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. How do I strip down integers for future manipulation?
    By frasifrasi in forum New To Java
    Replies: 12
    Last Post: 06-14-2008, 03:17 AM
  2. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  3. [SOLVED] File Movement/Manipulation
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 04-23-2008, 01:39 AM
  4. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:04 AM
  5. String Manipulation Task
    By hiranya in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 12:07 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
  •