Results 1 to 6 of 6

Thread: Store integer from a file into an ArrayList

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Store integer from a file into an ArrayList

    Hello,
    I am beginner in Java, my work is to extract data from a text-only files file this form:
    0 1 1 1 0 0 0
    1 0 1 0 1 0 1
    etc.....
    I store each line of the file in a ArrayList
    Here is my code.
    Code:
    for(int i =0; i <= NUMBER_OF_LINES; i + +){
    ArrayList <Integer> v = new ArrayList <Integer>();
    ln = br.readLine();
    ln = ln.trim();
    StringTokenizer st = new StringTokenizer(row," ");
    while(st.hasMoreTokens()) 
    {
      v.add(Integer.parseInt(st.nextToken()));
      System.out.System.out.println(v);
    }
    This code takes me back the following exception:
    Code:
    java.lang.NullPointerException
    Does someone can help me? I want to know if I'm on the right track.

  2. #2
    Join Date
    Dec 2009
    Posts
    292

    Re: Store integer from a file into an ArrayList

    Hello,
    I had a similar kind of a problem in my code. I noticed that the fault is that the loop "for (int i = 0; i <= NUMBER_OF_LINES; i + +)" NUMBER_OF_LINES performs iterations 1 and that is why it brings exception, one just want to know how I could manipulate the contents of ArrayListe. The file handle contains NUMBER_OF_LINES I 0 1 0 0 1 0 and the width of each line that I recover these two parameters to the first and second line of the file, I am now to what remains of file that is the result of the 0 1 0 1 etc.. I want to extract all the 1 in a structure knowing that the file contains. Here is the code which I was trying to do.
    Code:
    Public class pretreatment {
     
    	Public static void hand(String[] args) throws NumberFormatException {
    		try{
    			InputStream in = new FileInputStream("c: / / some file");
    InputStreamReader insr = new InputStreamReader(in);
    BufferedReader br = new BufferedReader(insr);
    String str1 = br.readLine();
    str1 = str1.trim();
    			int NUMBER_OF_LINES = Integer.parseInt(str1);
    System.out.System.out.println(NUMBER_OF_LINES);
    String str2 = br.readLine();
    str2 = str2.trim();
    			int nb_colonne = Integer.parseInt(str2);
    System.out.System.out.println(nb_colonne);
    String ln =null;
    			
    			for(int i =0; i <NUMBER_OF_LINES; i + +){
    				ArrayList <Integer> var = new ArrayList <Integer>();
    ln = br.readLine();
    ln = ln.trim();
    				int j =0;
    StringTokenizer val = new StringTokenizer(row," ");
    				int n =0;
    				while(val.hasMoreTokens()){ 
    				var.add(Integer.parseInt(val.nextToken()));
    				}
    				j + +;
    System.out.System.out.println(nbr);
    				}
    			br.close(); 
    		} 	catch (Exception e){ 
    			System.out.System.out.println(e.function toString() {
        [native code]
    }()); 
    			} 
     	}
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Store integer from a file into an ArrayList

    Hello,
    I have a part of code with me, if you need you can check it out.
    Code:
    	[...]		 
            ln = ln.trim();
     
            / / Hello, I tend to do more basic:
            String[] lnParts = ln.split(" ");
            
    	for (int k = 0K <lnParts.lengthK + +) 
    	{
    	   var.add(new Integer(lnParts[k]));
    	}  
    		
    	[...]
    Try this and if you have any problem then do post back.

  4. #4
    Join Date
    Dec 2009
    Posts
    202

    Re: Store integer from a file into an ArrayList

    Hello,
    Thank you for reply, I think it's better that is I want to store indices related to "1" in a structure.Comment can we declare a two dimensional ArrayList? Is this possible? If yes how to handle ? Or if you know any alternative method for doing this you can suggest me. Or if someone has an sample code for this then please post back, it will help me a lot. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Store integer from a file into an ArrayList

    Hello,
    I have never tried such an example but still, if you need some help here is my code, thought it may not satisfy your needs. Just have a look at it.
    Code:
    List <List <Integer>> ls2 = new ArrayList <List <Integer>>();
    
    ls2.add(new ArrayList <Integer>());
    
    List sbls = <Integer> ls2.get(0);
    Hope this may help you and you write a code which will satisfy your needs.

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Store integer from a file into an ArrayList

    Hello,
    The following code may help you. If you like it you can use it for your program. Best of Luck
    Code:
    ArrayList <Integer> mdiv = new ArrayList <Integer>();
    		
            / / Watch your indentations, alignment without parentheses, the code is hard to read
    	/ / NUMBER_OF_LINES in java that makes nombreLignes
    	for(int i = 0; I <nombreLignes; i + +)
    	{
    		/ / This is your "var" but as this was not very speaking ...
    		ArrayList <Integer> ldvu = new ArrayList <Integer>();
    			
    		/ / This is your "br" 
    		ln = myBufferedReader.readLine();
    ln = ln.trim();
    			
    		/ / More basic:
    		String[] str = ln.split(" ");
    		        
    		for (int k = 0K <str.lengthK + +) 
    		{
    			/ / We test if the figure is 1.
    			if (new Integer(str[k]).intValue() == 1)					
    			{   / / We store the index (and 1). I understood?
    			    ldvu.add(new Integer(k));
    			}				
    		}
    		mdiv.add(ldvu);
    	}

Similar Threads

  1. Integer range of CPU
    By Udyan in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 31-07-2010, 06:07 AM
  2. How to know the length of an integer in C++
    By troop in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 09:32 PM
  3. Store content of file in array of bytes
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 01:25 PM
  4. How to store file in Cloud?
    By Cisco-s in forum Technology & Internet
    Replies: 5
    Last Post: 06-01-2010, 08:01 PM
  5. Verillo's integer and reg?
    By Fason in forum Networking & Security
    Replies: 3
    Last Post: 25-10-2008, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,952,469.54160 seconds with 17 queries