Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I created an Excel file name "readdata.xls" and entered some inputs like

UserName    Password
Akshit          Ak12345
Ashwika         AS12345

I've written a code like this ....

package seleniumscripts;

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;

public class Excelreaddata {

    private static String husername;
    private static String hpassword;
    private static String username;
    private static String password;

    public static void main(String[] args) throws Exception {
        FileInputStream fis =new FileInputStream("C:\\Documents and Settings\\Home\\Desktop\\Readdata.xls");
        Workbook wb = Workbook .getWorkbook(fis);
        Sheet s= wb.getSheet(0);

        Excelreaddata.husername =s.getCell(0,0).getContents();
        Excelreaddata.hpassword =s.getCell(1,0).getContents();
        System.out.println(Excelreaddata.husername);
        System.out.println(Excelreaddata.hpassword);

        Excelreaddata.username =s.getCell(0,1).getContents();
        Excelreaddata.password =s.getCell(1,1).getContents();
        System.out.println(Excelreaddata.username);
        System.out.println(Excelreaddata.password);

        Excelreaddata.username =s.getCell(0,2).getContents();
        Excelreaddata.password =s.getCell(1,2).getContents();
        System.out.println(Excelreaddata.username);
        System.out.println(Excelreaddata.password);
    }
}

My output result is ok.

My question is I want to shorten my code by using "for" or any other loop structure" i.e., I want to call the Excel data by writing in loop structure which should display all the usernames and passwords.

can any one help me this iam very much new to write code.

share|improve this question
1  
Here is a list of Java tutorials. Here is Google search engine. What have you tried so far? –  dzieciou Jan 22 '13 at 11:14
    
Downvoting. That's a programming question, nothing to test/debug even here ;-) –  dzieciou Jan 22 '13 at 13:20
    
I see it very differently, the question is about refactoring test automation. –  Bj Rollison Jan 22 '13 at 16:38
    
Right, read in this way it is on-topic. I removed my downvote. –  dzieciou Jan 22 '13 at 17:09
    
-1 - Down voted as there are many resources to learn a basic for loop, and to encourage users to put some effort for easily available solutions. –  Suchit Parikh Jan 22 '13 at 17:46
add comment

closed as off topic by dzieciou, bytebuster, Sam Woods, Dan Snell, corsiKa Mar 10 '13 at 19:03

Questions on Software Quality Assurance & Testing Stack Exchange are expected to relate to software quality assurance or testing within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

jyotsna, the answer to your question is right in front of you. :-)

Look at these 2 statements compared to the same statements in your code above and identify the variable that is changing.

Excelreaddata.username =s.getCell(0,1).getContents();
Excelreaddata.password =s.getCell(1,1).getContents();

Instead of hard coding the row and column values, create and initialize a variables, put these statements in a for loop and increment appropriate variable as your code iterates through the loop.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.