Search code examples
javaswingjoptionpane

Javax.swing.JOptionPane can't print out PrintStream variable


I don't know how to print out a string and a variable in the joptionpane. is it possible? If not then what should i do?

String peopleadded = JOptionPane.showInputDialog("How many people do you     want to add");
    int peopleadded1 = Integer.parseInt(peopleadded);
    String[][] People = new String[peopleadded1][2];
    System.out.println("put your name in the array");
    People[0][0] = JOptionPane.showInputDialog("Put your Name");
    System.out.println("put your password in the array");
    People[0][1] = JOptionPane.showInputDialog("Put your Password");
    PrintStream person = System.out.printf("Your array num [%s] and name is %s and your password is %s", 0, People[0][0], People[0][1]);
    JOptionPane.showMessageDialog(null, person);

Solution

  • Use "Text"+varName+"moretext" etc. (Without printstream, directly in the optionpane)

    EDIT: You can use this:

    int peopleCount = Integer.parseInt(JOptionPane.showInputDialog(null,"How many people do you want to add?", "Add People", JOptionPane.QUESTION_MESSAGE));
    String[][] addPeople = new String[peopleCount][2];
    for (int i=0; i<peopleCount; i++) {
        addPeople[i][0] = JOptionPane.showInputDialog(null,"Person "+(i+1)+"\'s name:","Person "+(i+1),JOptionPane.PLAIN_MESSAGE);
        addPeople[i][1] = JOptionPane.showInputDialog(null,"Person "+(i+1)+"\'s password:","Person "+(i+1),JOptionPane.PLAIN_MESSAGE);
    }
    String people="";
    for (int i=0; i<peopleCount; i++)
        people+="Person "+(i+1)+":\n     Name: "+addPeople[i][0]+"\n     Password: "+addPeople[i][1]+"\n";
    JOptionPane.showMessageDialog(null,people,"People added",JOptionPane.INFORMATION_MESSAGE);
    

    Or, if you want to display each person in seperate dialogs:

    int peopleCount = Integer.parseInt(JOptionPane.showInputDialog(null,"How many people do you want to add?", "Add People", JOptionPane.QUESTION_MESSAGE));
    String[][] addPeople = new String[peopleCount][2];
    for (int i=0; i<peopleCount; i++) {
        addPeople[i][0] = JOptionPane.showInputDialog(null,"Person "+(i+1)+"\'s name:","Person "+(i+1),JOptionPane.PLAIN_MESSAGE);
        addPeople[i][1] = JOptionPane.showInputDialog(null,"Person "+(i+1)+"\'s password:","Person "+(i+1),JOptionPane.PLAIN_MESSAGE);
    }
    for (int i=0; i<peopleCount; i++)
        JOptionPane.showMessageDialog(null,"Person "+(i+1)+":\n  Name: "+addPeople[i][0]+"\n  Password: "+addPeople[i][1],"Person "+(i+1),JOptionPane.INFORMATION_MESSAGE);