-3

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.

  • 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
2

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.

| improve this answer | |

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