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

    Default output for list of names in a format

    I have a file data.txt which has these contents
    Robin=CS897
    Harry=education
    Harry=system admin
    Tom=Alternate doctor
    The program read the file and gives the name and value,for only the list of names in the arraylist al.
    Java Code:
    public static void main(String args[])throws IOException
    	{
    	ArrayList<String> al=new ArrayList<String>();
    	al.add("Harry");
    	al.add("Robin");
    	String st1;
    	BufferedReader br=new BufferedReader(new FileReader("src/data.txt"));
    		  try {
    			  
    				  
    				while((st1=br.readLine())!=null)
    				  {
    				 String attributevalue=null;
    		         String[] temp=st1.split("=");
    		         for(String s:al)
    				  {
    		        	 //System.out.println("Arraylist split up:"+s);
    			        	  if(temp[0].equalsIgnoreCase(s)) 
    			        	  {
    			        		  if((temp.length>=2))
    			        		  {
    				        	  attributevalue=temp[1];
    			        		  }
    			        		  else
    			        		  {
    			        		    attributevalue=null;
    			        		  }
    				        	  System.out.println("attributename:"+s+"  "+"attributevalue:"+attributevalue);
    			        	  }	
    			        	  
    				  }
    			  }
    		   } catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    Output
    attributename:Robin attributevalue:CS897
    attributename:Harry attributevalue:education
    attributename:Harry attributevalue:system admin

    Expected Output
    attributename:Robin attributevalue:CS897
    attributename:Harry attributevalue:education;system admin

    The name Harry has two values, i want the output to look like this so how can i get this??any suggestion plz...

    Thanks

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you need to read the file into a buffer, filter out the data, then display it.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Another way (possibly more complicated, but...)

    Create a class representing a person that contains a name and an ArrayList of 'jobs', then an ArrayList of the class. When you read the data in, check if the ArrayList contains a person with the name, and if it does, add an attribute and continue. Otherwise, add a new person to the ArrayList.

    @AB Sounds complicated... I've never had a reason to try it, and I have no idea how it would work... The OP would probably need some sort of example :o
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I guess my advice was too cryptic...?

    Java Code:
    [COLOR=NAVY]import[/COLOR] java[COLOR=BLACK].[/COLOR]util[COLOR=BLACK].[/COLOR][COLOR=BLACK]*[/COLOR][COLOR=BLACK];[/COLOR]
    
    [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]class[/COLOR] CafeBabe [COLOR=BLACK]{[/COLOR]    
      [COLOR=PURPLE]private[/COLOR] Map[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK],[/COLOR]StringBuilder[COLOR=BLACK]>[/COLOR] map[COLOR=BLACK];[/COLOR]
      
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] main[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=PURPLE]final[/COLOR] String DATA [COLOR=BLACK]=[/COLOR] 
                [COLOR=SILVER]"Robin=CS897\n"[/COLOR] [COLOR=BLACK]+[/COLOR]
                [COLOR=SILVER]"Harry=education\n"[/COLOR] [COLOR=BLACK]+[/COLOR]
                [COLOR=SILVER]"Harry=system admin\n"[/COLOR] [COLOR=BLACK]+[/COLOR]
                [COLOR=SILVER]"Tom=Alternate doctor"[/COLOR][COLOR=BLACK];[/COLOR]
        
        List[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK]>[/COLOR] al[COLOR=BLACK]=[/COLOR][COLOR=NAVY]new[/COLOR] ArrayList[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK]>[/COLOR][COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      	al[COLOR=BLACK].[/COLOR]add[COLOR=BLACK]([/COLOR][COLOR=SILVER]"Harry"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      	al[COLOR=BLACK].[/COLOR]add[COLOR=BLACK]([/COLOR][COLOR=SILVER]"Robin"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        
        CafeBabe cbb [COLOR=BLACK]=[/COLOR] [COLOR=NAVY]new[/COLOR] CafeBabe[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        cbb[COLOR=BLACK].[/COLOR]createBuffer[COLOR=BLACK]([/COLOR]al[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        cbb[COLOR=BLACK].[/COLOR]filter[COLOR=BLACK]([/COLOR]DATA[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        cbb[COLOR=BLACK].[/COLOR]display[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]void[/COLOR] createBuffer[COLOR=BLACK]([/COLOR]List[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK]>[/COLOR] list[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        map [COLOR=BLACK]=[/COLOR] [COLOR=NAVY]new[/COLOR] HashMap[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK],[/COLOR]StringBuilder[COLOR=BLACK]>[/COLOR][COLOR=BLACK]([/COLOR]list[COLOR=BLACK].[/COLOR]size[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]*[/COLOR][COLOR=ORANGE][B]2[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]for[/COLOR][COLOR=BLACK]([/COLOR][COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR] i[COLOR=BLACK]<[/COLOR]list[COLOR=BLACK].[/COLOR]size[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR] i[COLOR=BLACK]++[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          map[COLOR=BLACK].[/COLOR]put[COLOR=BLACK]([/COLOR]list[COLOR=BLACK].[/COLOR]get[COLOR=BLACK]([/COLOR]i[COLOR=BLACK])[/COLOR][COLOR=BLACK],[/COLOR][COLOR=NAVY]new[/COLOR] StringBuilder[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]void[/COLOR] filter[COLOR=BLACK]([/COLOR]String fileData[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        Scanner sc [COLOR=BLACK]=[/COLOR] [COLOR=NAVY]new[/COLOR] Scanner[COLOR=BLACK]([/COLOR]fileData[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] s_arr[COLOR=BLACK];[/COLOR]
        
        [COLOR=NAVY]while[/COLOR][COLOR=BLACK]([/COLOR]sc[COLOR=BLACK].[/COLOR]hasNext[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          s_arr [COLOR=BLACK]=[/COLOR] sc[COLOR=BLACK].[/COLOR]nextLine[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]split[COLOR=BLACK]([/COLOR][COLOR=SILVER]"="[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
          [COLOR=NAVY]if[/COLOR][COLOR=BLACK]([/COLOR]map[COLOR=BLACK].[/COLOR]containsKey[COLOR=BLACK]([/COLOR]s_arr[COLOR=BLACK][[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK]][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
            map[COLOR=BLACK].[/COLOR]get[COLOR=BLACK]([/COLOR]s_arr[COLOR=BLACK][[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK]][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]append[COLOR=BLACK]([/COLOR]s_arr[COLOR=BLACK][[/COLOR][COLOR=ORANGE][B]1[/B][/COLOR][COLOR=BLACK]][/COLOR][COLOR=BLACK]+[/COLOR][COLOR=SILVER]";"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
          [COLOR=BLACK]}[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]void[/COLOR] display[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]for[/COLOR] [COLOR=BLACK]([/COLOR]Map[COLOR=BLACK].[/COLOR]Entry[COLOR=BLACK]<[/COLOR]String[COLOR=BLACK],[/COLOR] StringBuilder[COLOR=BLACK]>[/COLOR] entry [COLOR=BLACK]:[/COLOR] map[COLOR=BLACK].[/COLOR]entrySet[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]  
          System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"key="[/COLOR] [COLOR=BLACK]+[/COLOR] entry[COLOR=BLACK].[/COLOR]getKey[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR] [COLOR=BLACK]+[/COLOR] 
              [COLOR=SILVER]", value="[/COLOR] [COLOR=BLACK]+[/COLOR] entry[COLOR=BLACK].[/COLOR]getValue[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]toString[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    Hi Ranjan,
    One small suggestion.
    If you are not having duplicate keys,then you can use "Properties" load method which reads a property list (key and element pairs) from the input stream.Please gothru the below code.

    Java Code:
    import java.util.*;
    import java.io.*;
    public class Test
    {    
      public static void main(String[] args)
      {
    	  try
    	  {
    		Properties p = new Properties();
    
    		p.load(new FileInputStream(new File("data.txt")));
    
    		System.out.println(p);
    
    	  }catch(Exception ex)
    	  {
    		  System.out.println("Problem in main "+ ex);
    	  }
    	 
      }//main
    }//class
    Ramya:cool:

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    He, however, explicitly stated that there will be duplicate keys.

    As imple HashMap with the name as the Key and an ArrayList of the positions as the value will do the job.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    in this case, Properties are not gona work if you have multiple keys with the same name...

    edit:
    If you are not having duplicate keys,...
    Oops, didn't see that
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    Yes I do have multiple keys with the same name....Thanks to all of you for your suggestion,special thanks to angry boy your idea solved my problem..

    Thanks again

Similar Threads

  1. Replies: 0
    Last Post: 04-07-2009, 10:35 PM
  2. Replies: 0
    Last Post: 03-03-2009, 08:46 AM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  5. Replies: 4
    Last Post: 10-02-2008, 10:05 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
  •