Solved

Need two option values from a single method.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 33
mysql jsp example issue 32 48
Fast way to search item into Java Array (Rhino compatible) 2 31
Eclipse for Java EE development 2 27
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

856 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