Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counter issue

Posted on 2006-11-13
7
Medium Priority
?
281 Views
Last Modified: 2006-11-30
hi,

this class method writes row into excel file. Is there a way I can allow only to write 10 rows and ignore rest if any.?????

-----------------------------------------
........
public void addRow(Object[] theValues) throws Exception {
       
        try {
            mCurrentRow++;    
            mSheet.insertRow(mCurrentRow);
            for (int i = 0; i < theValues.length; i++) {
                Object v = theValues[i];
                if (v != null) {
                    WritableCell cell = new Label(i, mCurrentRow, v.toString());
                    mSheet.addCell(cell);
                }                
            }
        }
        catch (Exception e) {
           ...
        }        
    }
........
----------------------------------------------


I tried this but didnt worked......still wrote all rows.
while (counter < 10)
{
 try { ...}
}

thx.
PH
0
Comment
Question by:princehyderabad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17935118
Might be

            for (int i = 0; i < theValues.length; i++) {
                if (i <= 10) {
                    Object v = theValues[i];
                    if (v != null) {
                        WritableCell cell = new Label(i, mCurrentRow, v.toString());
                        mSheet.addCell(cell);
                    }        
                }      
            }
0
 

Author Comment

by:princehyderabad
ID: 17935130
nope I already tried that as well didnt worked.

Someone wht the knowledge how xls file would be created using java will have good idea to solve this...
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17935250
Then can't help you much. Sorry.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17939215
for (int i = 0; i < theValues.length; i++) {
                    Object v = theValues[i];
                    if (v != null) {
                        WritableCell cell = new Label(i, mCurrentRow, v.toString());
                        mSheet.addCell(cell);
                    }        
                if(i==9)
       break;
            }
it will break on the 10th row after writing.

now you need to keep in mind that it will not stop my making extra cells in the excel sheet. ( its still gonna default to 65556 rows in Excel because thats what excel do when it creates a empty sheet)
0
 
LVL 12

Accepted Solution

by:
jessegivy earned 500 total points
ID: 17941359
       try {
            mCurrentRow++;    
            mSheet.insertRow(mCurrentRow);
            for (int i = 0; i < theValues.length && i<10; i++) {
                Object v = theValues[i];
                if (v != null) {
                    WritableCell cell = new Label(i, mCurrentRow, v.toString());
                    mSheet.addCell(cell);
                }                
            }
        }
        catch (Exception e) {
           ...
        }  

...the second argument of the for loop is a condition, you can specify additional conditions using && or ||

the break statement will work, but I find this solution a bit cleaner and may be ever so slightly beneficial for performance.

~Jesse
0
 

Author Comment

by:princehyderabad
ID: 17941541
I tried this and work. But tell me the most beneficial for performance wise.

 try {
            mCurrentRow++;    
if (mCurrentRow < 10)
            {
            mSheet.insertRow(mCurrentRow);
             for loop { ..}
            }//end if
        }
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 17963228
if it works, it works, you're talking about iterating ten times so performance isn't an issue, I believe including a second condition would slow slightly as opposed to iteration within because it's using an and statement which in essence is running two conditions simultainiously, and requires less code, how the processor interprets this code is dbatable, I doubt anyone knows?  Ultimatly though, this would only be a consideration if you were doing hundreds or thousands of iterations or calling the function that contains this loop quite a few times.

make sense?  Hope I didn't confuse, just showing a different, more condensed way to accomplish the same thing.

Cheers,

Jesse
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question