Solved

Need two option values from a single method.

Posted on 2006-10-19
4
213 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now