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

    Default How to get null values stored in array

    I have a file data.txt of this format in which each name has one value,but there are names with no values in it.Like the one in Harry
    Robin=CS897
    Steve=education
    Harry=
    Tom=Alternate doctor

    My code reads the file and split each line into array.What i actually want is if the name i am looking for has a blank value,i want it to be printed as null.but what i get is

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at mapsample.main(mapsample.java:56)
    Java Code:
    public static void main(String args[])
    {
    String st1;
    BufferedReader br=new BufferedReader(new FileReader("src/data.txt"));
    	  try {
    		while((st1=br.readLine())!=null)
    		  {
    		 String attributevalue=null;
                               String[] temp=st1.split("=");	          
    	        	  if(temp[0].equalsIgnoreCase("Harry") )
    	        	  {
    	        		  if((temp[1]!=null))
    	        		  {
    		        	  attributevalue=temp[1];
    	        		  }
    	        		  else
    	        		  {
    	        		    attributevalue=null;
    	        		  }
    		        	  System.out.println(attributevalue);
    	        	  }	
    	        	  
    		  }
    	   } catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    any suggestions plz..

    Thanks

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    your issue isn't with the null, it's with your temp array going too high. Whenever you set String[] temp = st1.split("=") it only returns an array with object. Try printing out temp.length() and seeing if it's what you expect
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

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

    Default

    Robin=CS897
    Steve=education
    Tom=Alternate doctor

    They should each return an array of length=2 after splitting.

    Harry=
    This should return an array of length=1 after splitting.
    Then just check the length, if 2 then do nothing.
    if 1, then create a new array.

    Java Code:
    String[] newStr = new String[2];
    newStr[0] = oldStr[0];
    newStr[1] = null;
    etc...
    Last edited by angryboy; 05-21-2009 at 11:11 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Instead of (temp[1]!=null), use (temp.length>=2). split() will only create array elements to contain string portions, and will not create null entries.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

Similar Threads

  1. Replies: 0
    Last Post: 02-28-2009, 12:16 PM
  2. How to insert a Value into a stored array ?
    By A.M.S in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 03:21 PM
  3. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 09:15 PM
  4. Default values of stored procedures
    By vinayakshukre in forum JDBC
    Replies: 2
    Last Post: 01-13-2009, 01:05 PM
  5. How to access array stored in database through JSTL
    By saurabh.joshi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2007, 02:46 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
  •