DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 14 of 14

Thread: array confusion

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    23

    array confusion

    Question about two peices of code
    1.
    Code:
    {
    		int i=0;
    		int j=0;
    		int total=0;
    		while(Character.isWhitespace(lines[i].charAt(j)))
    		{
    			total++;
    			i++;
    			j++;
    		}
    		return total;
    this should show the number of characters in an already loadaded file but it doesnt

    2How do you take a file with blank lines in it and crunch it up for example
    this is

    a file

    now
    should turn into

    this
    is
    a file
    now

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    I)

    Code:
    int total=0;
    for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
    	String str = lines[lineIndex];	
    	if ( int j = 0; j < str.length; j++ ) {
    		if (!Character.isWhitespace(str.charAt(j))) { // if you do not want to calculate whitespaces 
    			total++;
    		}
    	}
    }

    II)
    Code:
    BufferedReader br = new BufferedReader(XXX); // XXX - your input stream e.g. FileInputStream
     String line;
     while ((line = br.readLine()) != null) {
          if (line.trim().size()str == 0) {
          	continue; // skip empty string
          }
          ....      
     }

  3. #3
    Join Date
    Sep 2006
    Posts
    23
    Can you show it without continue and also your the first one doesnt work
    Last edited by torrinment; 10-09-2006 at 04:21 PM.

  4. #4
    Join Date
    Sep 2006
    Posts
    68

    Thumbs down

    Code:
    for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
    	 String line = lines[lineIndex];	
    	 if (!line.trim().size() == 0) {
            	System.out.println(line);
    ...
    
       }
    }

  5. #5
    Join Date
    Sep 2006
    Posts
    23
    the character count one doesnt work either

    Code:
    {
    		int i=0;
    		int j=0;
    		int total=0;
    		while(Character.isWhitespace(lines[i].charAt(j)))
    		{
    			total++;
    			i++;
    			j++;
    		}
    		return total;
    Last edited by torrinment; 10-09-2006 at 04:34 PM.

  6. #6
    Join Date
    Sep 2006
    Posts
    68
    The character count:

    Code:
     int total = 0;
            String[] lines = new String[] {
                    "abcdef", "123", "qwer ty" // 15 chars + 1 space
            };
            for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
                String str = lines[lineIndex];
                for (int j = 0; j < str.length(); j++ ) {
                    if (!Character.isWhitespace(str.charAt(j))) { // if you do not want to calculate whitespaces
                        total++;
                    }
                }
            }
            System.out.println(total);

  7. #7
    Join Date
    Sep 2006
    Posts
    23
    do you know what a nullpointer exception is

  8. #8
    Join Date
    Sep 2006
    Posts
    68
    Quote Originally Posted by torrinment
    do you know what a nullpointer exception is
    Yes I know. It happens when you try to do with something that don't exsists.

    For example:
    String s = null;
    s.trim(); // throws NullPointerException

  9. #9
    Join Date
    Sep 2006
    Posts
    68
    Second example:

    Code:
    String[] lines = new String[] {
                    "this", "   ", "is", "a" , " ",  "test" };
            for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
    	    String line = lines[lineIndex];
    	    if (!(line.trim().length() == 0)) {
            	System.out.println(line);
                }
            }

    PS: I often post a programs which I don't check. I think small errors like misprints you can correct yourself.

  10. #10
    Join Date
    Sep 2006
    Posts
    23
    that works but gives me a nullpointerexception how do i fix this like it does what it supposed to but then i get that exceptio n

  11. #11
    Join Date
    Sep 2006
    Posts
    68
    Quote Originally Posted by torrinment
    that works but gives me a nullpointerexception how do i fix this like it does what it supposed to but then i get that exceptio n
    It's difficult to answer because I don't know your code. Please attach a whole your application or am exception stack trace.

    BTW: check that you nowhere set empty values to variables.

  12. #12
    Join Date
    Sep 2006
    Posts
    23
    no im just talkin about both the character count you showed me and the eliminate vspace
    Code:
    public static int charCount(String[]lines)
    	{
    		 int total = 0;
    	       for (int i=0; i < lines.length; i++) 
    	       {
    	    	   for (int j = 0; j < lines[i].length(); j++ ) 
    	       {
    	    	   if (!Character.isWhitespace(lines[i].charAt(j))) {
    	    		   total++;
    	                }
    	            }
    	        }
    	        System.out.println(total);
    		return total;
    	}
    and
    Code:
    public static void EliminateVSpace(String[]lines)
    	{
            for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
    	    String line = lines[lineIndex];
    	    if (!(line.trim().length() == 0)) {
            	System.out.println(line);
                }
            }
    }

  13. #13
    Join Date
    Sep 2006
    Posts
    68
    I understand. The exception happens because you pass lines with empty values.
    For example
    if you pass such array:
    String[] lines = new String[] {"this", " ", "is", null, "a", "test" };
    it will produce an exception.

  14. #14
    Join Date
    Sep 2006
    Posts
    23
    so i fix it by...

Similar Threads

  1. Replies: 7
    Last Post: 03-07-2006, 04:34 PM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  4. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  5. Accessing jagged array values
    By Jason Salas in forum ASP.NET
    Replies: 2
    Last Post: 04-20-2003, 11:39 PM

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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links