Search code examples
javainputjoptionpane

How would i go about adding to my array through JOptionpanes?


At the moment I have to manually add items to my array but I would like to enable users to do this themselves perhaps through JOptionPanes, what would be the best way to go about this? Here is my current code.

public class Main {
  public static void main(String[] args){

  //Create new Person objects

 Address p[] = new Address[3];
 p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
 p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
 p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
 Details c[] = new Details[3];
 c[0] = new Details ("3", "175,000", "Terraced");
 c[1] = new Details  ("6", "300,000", "Bungalow");
 c[2] = new Details ("4", "250,000", "Detached");

  //Send some messages to the  objects
  c[0].setBeds("3 ");
  c[1].setBeds("6");
  c[2].setBeds("4");
  c[0].setPrice("175,000");
  c[1].setPrice("300,000");
  c[2].setPrice("250,000");
  c[0].setType("Terraced");
  c[1].setType("Bungalow");
  c[2].setType("Detached");


  //Set up the association
  p[0].ownsDetails(c[0]);
  p[1].ownsDetails(c[1]);
  p[2].ownsDetails(c[2]);

  //print details
  p[1].printDetails();
  p[2].printDetails();
  p[3].printDetails();


}
  System.exit(0);
}
}

Solution

  • You could use the showXXXX methods on JOptionPane and keep prompting the user just as you would on the console.

    However, I suggest just creating a simple JFrame that would have controls that allows the user to enter multiple items instead of showing one dialog after another.