将两个字符串发送到JOption列表菜单

时间:2016-11-15 01:17:24

标签: java arrays user-interface jdbc joptionpane

所以我有这个:

#CONSTANTS 
CON_NUM = int(5)

def correctInteger(row):
    #get valid row number:  
    while row < CON_NUM:
        row = int(input("Please enter a number bigger than or equal to 5: "))
        if row >= CON_NUM:
            return(row)
    if row >= CON_NUM:
        return(row)

def correctNum(cols):
    #get valid column number:  
    while cols < CON_NUM:
        cols = int(input("Enter a number bigger than / equal to 5: "))
        if cols >= CON_NUM:
            return(cols)
    if cols >= CON_NUM:
        return(cols)

def main():
    #ask for number of rows:
    print("Choose the number of rows:")
    rows = int(input("Please enter a number bigger/ equal to 5: "))

    #ask for columns:
    print("Please choose the number of columns:")
    columns = int(input("Please enter a number bigger/ equal to 5: "))
    validRow = correctInteger(rows)
    validColumn = correctNum(columns)
    print(validRow)
    print(validColumn)

main()

它将阵列的两个医院打印到控制台,所以我知道它找到了它们。但是,当我尝试通过JOption将它们显示给列表框中的用户时,它只显示最新的(第二个)医院,而不是第一个。

我是否正在跳过第一个字符串?

2 个答案:

答案 0 :(得分:0)

你跳过第一个hospital的原因是因为这一行:

JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals",
                      JOptionPane.QUESTION_MESSAGE, null, new Object[] 
                      {hospital}, a);

在这里,您要添加一个新的Object[],其中只有hospital选项设置为第二个选项。你要找的是:

JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals",
                      JOptionPane.QUESTION_MESSAGE, null, a, a[0]);

这会为所有a设置选项,并将初始值设置为hospital中的第一个a

答案 1 :(得分:0)

修正了这个:

List<String> a = new ArrayList<String>();
            String hospital = null;

            while (results.next()) 
            {
                // Put into interactive list    
                hospital = results.getString("hospitalName"); 
                {
                    // Loops each hospital via popup, needs to be added to a selection menu
                    //JOptionPane.showMessageDialog(null, hospital, "Hospital List", JOptionPane.INFORMATION_MESSAGE);
                    System.out.println(hospital);
                a.add(hospital);
                }
            }

            {
                // Add the hospital to the array of hospitals found
                Object[] options = a.toArray();

                // Give operator the choice of hospital suited for the patient             
                JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals", 
                         JOptionPane.QUESTION_MESSAGE,  null, options, options[0]);