Solved

Need two option values from a single method.

Posted on 2006-10-19
4
250 Views
Last Modified: 2010-03-31
Hello All,
I'm trying to get two option values in single method through if else condition. i.e. if one condition is satisfied, then show the first option values or else the second option values needs to be shown. But when I try the return the string, it returns me with the both options appended.

i.e. I have one vector named Project Name, from which I generate freezed Project name and print those values in the dropdown list. Then I want to create another option which would generate unfreezed project i.e. Project Name-Freeze Project = UnFreeze Project.

Here is the code:

public String getProjectNameList(int index) {
    StringBuffer stringBuffer = new StringBuffer();
    String newProjectName;
    // if (vectorFreezeProject != null) {
    for (int i = 0; i < vectorForProjectName.size(); i++) {
        String projectAbbrevation = "";
        String pname = vectorForProjectName.elementAt(i).toString().trim();
        for (int a = 0; a < vectorFreezeProject.size(); a++) {
            String vFProject = vectorFreezeProject.elementAt(a).toString().trim();
            if (vectorFreezeProject.indexOf(pname) != -1) {
                stringBuffer.append(
                    "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + pname
                        + "\">"
                        + pname
                        + "</option>\n");
 
            } else {
 
                try {
                    projectAbbrevation =
                        this.vectorForProjectAbbrevation.elementAt(i).toString().trim();
                } catch (ArrayIndexOutOfBoundsException aioobe) {
                    projectAbbrevation = "DEFAULT";
                }
                newProjectName = this.vectorForProjectName.elementAt(i).toString();
                // newProjectName = this.vectorForTrackerDate.elementAt(index).toString().trim();
 
                stringBuffer.append(
                    "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + newProjectName
                        + "\">"
                        + newProjectName
                        + "</option>\n");
 
                System.out.println("print:::: " + newProjectName);
 
            }
 
            break;
        }
 
    }
    return stringBuffer.toString();
}

What is the best way of doing it? Is there any other solution?

Please help.

Thnx & Rgds,

P.S. I want to display this option values, which are dynamically generated through java, in JSP
0
Comment
Question by:jas99
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 17764947
OK... What is the exact problem because you lost me somewhere in the explanations :)And what you need this "break;" for? BEcause from what I see this means that 'a' will be only 0 and then you will get out of the cycle.

So what I miss?
0
 

Author Comment

by:jas99
ID: 17771558
Hi,
Sorry. I have posted a right code (see below). O.k. I will explain it again.

Just go thru the code comments for better understanding.

........................................................................................................................................

/**
* This method return the unfreeze project name drop down and freeze project name drop down only if there were any previous entires
* otherwise it should remove the freeze project name from the drop down list and show me the unfreeze project name.
* @return String
* @param selectedIndex int
*/
public String getProjectNameList(int index) {
    StringBuffer stringBuffer = new StringBuffer();
    String newProjectName;
    for (int i = 0; i < vectorForProjectName.size(); i++) {
        String projectAbbrevation = "";
        String pname = vectorForProjectName.elementAt(i).toString().trim();
          for (int a = 0; a < vectorFreezeProject.size(); a++) {
            String vFProject = vectorFreezeProject.elementAt(a).toString().trim();
            if (vectorFreezeProject.indexOf(pname) != -1) {
                stringBuffer.append(
                    "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + pname
                        + "\">"
                        + pname
                        + "</option>\n");        
 
            }
            else {

                try {
                    projectAbbrevation =
                        this.vectorForProjectAbbrevation.elementAt(i).toString().trim();
                } catch (ArrayIndexOutOfBoundsException aioobe) {
                    projectAbbrevation = "DEFAULT";
                }
                newProjectName = this.vectorForProjectName.elementAt(i).toString();
                     stringBuffer.append(
                    "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + newProjectName
                        + "\">"
                        + newProjectName
                        + "</option>\n")                       
            }
           
   break;
        }

    }
    return stringBuffer.toString();
      // some how this is displaying options with both the values.
}
0
 
LVL 20

Accepted Solution

by:
Venabili earned 250 total points
ID: 17773295
Something is wrong/unexpected with the parameters you give. Let's try this code and let me know what it prints:
 public String getProjectNameList(int index) {
        StringBuffer stringBuffer = new StringBuffer();
        String newProjectName;
            for (int i = 0; i < vectorForProjectName.size(); i++) {
                String projectAbbrevation = "";
                String pname = vectorForProjectName.elementAt(i).toString().trim();
                System.out.println("pname[i]=" + pname);
                for (int a = 0; a < vectorFreezeProject.size(); a++) {
                    String vFProject = vectorFreezeProject.elementAt(a).toString().trim();
                    System.out.println("vFProject[a]=" + vFProject);
                    if (vectorFreezeProject.indexOf(pname) != -1) {
                        System.out.println("It contains");
                        stringBuffer.append(
                        "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + pname
                        + "\">"
                        + pname
                        + "</option>\n");        
                   
                    }
                    else {
                   
                        try {
                            projectAbbrevation =
                            vectorForProjectAbbrevation.elementAt(i).toString().trim();
                        } catch (ArrayIndexOutOfBoundsException aioobe) {
                            projectAbbrevation = "DEFAULT";
                        }
                        newProjectName = vectorForProjectName.elementAt(i).toString();
                        stringBuffer.append(
                        "<option PROOJECT_ABBREVATION=\""
                        + projectAbbrevation
                        + "\" value=\""
                        + newProjectName
                        + "\">"
                        + newProjectName
                        + "</option>\n");
                        System.out.println("It does not contain");                    
                    }
                   
                    break;
                }
           
            }
        return stringBuffer.toString();
        // some how this is displaying options with both the values.
    }

Also:

This method receives index as a parameter which is used nowhere? Had you missed something?
0
 

Author Comment

by:jas99
ID: 17817369
Hi,
This got solved thru other method, so i didn't check this up.... Anyways thanks for your help.

Regards,
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

679 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