Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: What is wrong in My code. Going to Else part..Can you please suggest me.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question What is wrong in My code. Going to Else part..Can you please suggest me.

    import java.io.FileInputStream;
    import java.util.Iterator;
    import java.util.Vector;
    import java.lang.String;

    import javax.swing.JOptionPane;

    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ExcelXLSX {
    public static Object TestType;
    public static Object TargetEnvironment;
    public static Object TargetSystem;
    public static Object TargetRegion;
    public static Object TargetKeyword;
    public static Object name1 = "Shakedown";
    private Vector DataExcel = new Vector();

    public ExcelXLSX() {
    String FileName = "C:\\Users\\u304081\\Desktop\\Java\\FrameWork.xlsx ";
    DataExcel = ReadExcel(FileName);
    displayDataExcelXLSX(DataExcel);
    }

    public static Vector ReadExcel(String fileName) {
    Vector vectorData = new Vector();

    try {
    FileInputStream fileInputStream = new FileInputStream(fileName);

    XSSFWorkbook ObjWorkBook = new XSSFWorkbook(fileInputStream);

    // Read data at sheet 0
    XSSFSheet ObjSheet = ObjWorkBook.getSheetAt(0);
    Iterator rowIteration = ObjSheet.rowIterator();
    // Looping every row at sheet 0
    while (rowIteration.hasNext()) {
    XSSFRow Row = (XSSFRow) rowIteration.next();
    Iterator cellIteration = Row.cellIterator();
    Vector CellEachRowData = new Vector();

    // Looping every cell in each row at sheet 0
    while (cellIteration.hasNext()) {
    XSSFCell Cell = (XSSFCell) cellIteration.next();
    CellEachRowData.addElement(Cell);
    }

    vectorData.addElement(CellEachRowData);
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }

    return vectorData;
    }


    public static void displayDataExcelXLSX(Vector vectorData) {
    // Looping every row data in vector
    Vector TestType1 = (Vector) vectorData.get(1);
    Vector TargetEnvironment1 = (Vector) vectorData.get(2);
    Vector TargetSystem1 = (Vector) vectorData.get(3);
    Vector TargetRegion1 = (Vector) vectorData.get(4);
    Vector TargetKeyword1 = (Vector) vectorData.get(5);


    // looping every cell in each row
    //System.out.print(TestType.get(1).toString()+" \t");
    //JOptionPane.showMessageDialog(null,TestType1.get(1 ));

    TestType = TestType1.get(1);
    //TestType.toString();
    //String Testtype2 = (String) TestType;
    System.out.println("TestTYpe " + TestType);
    TargetEnvironment = TargetEnvironment1.get(1);
    System.out.println("TargetEnvironment " + TargetEnvironment);
    TargetSystem = TargetSystem1.get(1);
    System.out.println("TargetSystem " + TargetSystem);
    TargetRegion = TargetRegion1.get(1);
    System.out.println("TargetRegion " + TargetRegion);
    TargetKeyword = TargetKeyword1.get(1);
    System.out.println("TargetKeyword " + TargetKeyword);
    //Script to display the options selected in Framework

    // switch (RValue) {
    // case 0 :
    // case 1 : break;
    // }

    }

    public static void main(String[] args) {
    new ExcelXLSX();
    String StrMsg="You have choosen " + "\t" + TestType + "-" + TargetEnvironment + "-" + TargetSystem + "-" + TargetRegion + "\t" + "Do you want to continue..";
    int RValue=JOptionPane.showConfirmDialog(null, StrMsg);
    System.out.println("RValue: "+RValue);
    switch (RValue) {
    //For selecting Yes Button
    case 0 :
    System.out.println("Yes is Selected");
    System.out.println("Test type_sheet:" + TestType);
    TestType.toString();
    name1.toString();
    //Object name2 = name1;
    System.out.println("Test type_comparison:" + name1);
    Boolean y = TestType.equals(name1);
    System.out.println("Y:" + y);
    if (TestType.equals(name1)) { //I am comparing two objects here.But it returning False here.
    System.out.println("Testtype is SD/Reg/Keyword");
    }
    else {
    System.out.println("Not SD/Reg/Keyboard");
    }
    // Here is my problem
    break;
    // For selecting NO Button
    case 1 :
    System.out.println("NO is selected");
    break;
    }
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    Your code is very difficult to read. It's unformatted, posted wrong, and doesn't follow Java's naming conventions. For the first two, read the Announcement topic at the top of the sub forum. For the naming issues, modify your code to respect Java's naming conventions.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Umasri (October 4th, 2013)

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    It is going into the else clause because your TestType object is not the same as name1. This is expected behaviour.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    Umasri (October 4th, 2013)

  6. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    public static Object TestType;
    public static Object name1 = "Shakedown";
    //and in display method got the values
    public static void displayDataExcelXLSX(Vector vectorData) {
    Vector TestType1 = (Vector) vectorData.get(1);
    TestType = TestType1.get(1);
    }
    //In Main function changed the object into Strings
    public static void main(String[] args) {
    TestType.toString();
    name1.toString();
    //Here both the values are giving Same result but Boolean value is returned as "Flase"
    if (TestType.equals(name1)) {
    System.out.println("Testtype is SD/Reg/Keyword");
    // DriverTableArry[ArrayInd][]= TargetEnvironment;
    }
    else {
    System.out.println("Not SD/Reg/Keyboard");
    }
    }[COLOR="Silver"]

  7. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    Until you show us the exact values both variables hold before you do the equality test, we cannot help you.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    I am having the doubt about that"can't we compare the String and Object?"
    I tried with all the conversions but still it is not working.
    In Output I am getting the value TestType = "Shakedown"
    name1 = "Shakedown" (I assign this value while declaring)
    But while comparing it showing as both are not equal.

  9. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    Comparing two Objects which have been assigned String values work fine. The problem is reading your code.

    public static void main(String[] args) {
        TestType.toString();
        name1.toString();
    }

    How can you say both values return the same value when at this point, TestType has not been given a value? i.e the only place you give it a value is inside displayDataExcelXLSX() which you do not call.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    For this what i have to do?This is my first program.So i am not able to find the mistake.
    Can you please tell me what are the changes i have to do in my program.

  11. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    Somewhere inside your MAIN method, you need to say TestType = something, as the code you've shown us means its null.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  12. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong in My code. Going to Else part..Can you please suggest me.

    "TestType" Value i will get From Excel sheet.It is a cell value.
    Excel.jpg
    It is giving the exact value as "Shakedown"
    For name1 i gave a value "Shakedown"
    And comparing these values.
    Output:
    =====
    Yes is Selected
    Test type_sheet:Shakedown
    Test type_comparision:Shakedown
    Y:false

Similar Threads

  1. Rate this code on a scale from 1 - 10 and please suggest some improvements.
    By BurningCa007 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: March 14th, 2013, 10:31 PM
  2. What does this part of the code mean?
    By jean28 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 23rd, 2012, 11:47 PM
  3. What does this short part of a code do?
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2012, 11:31 AM
  4. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM
  5. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM