Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2D array help plz

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2D array help plz

    hello smile.gif
    i am having trouble with the 2D array i am making.......it is supposed to be an address book of sorts

    Miller, Joseph, 910 Auburn Avenue, McLean, VA, 7035551234
    Frank, Michael, 1 York Road, Baltimore, MD, 4105551234
    Roland, Frank, 346 Bellona Avenue, Lutherville, MD, 4435551234
    Smith, Jan, 432 Burke Avenue, Towson, MD, 4105559876
    Laurel, Mary, 435 Saint Francis Avenue, Bowie, MD, 4435554567

    so basically the 2D array is [5][6] i think unless im counting wrong or including things wrong


    this is the information i wish to input into my array.........not real names n stuff of course lol
    and this is an example of how i want to run my program or wut it is supposed to be like

    Example
    System: What do you wish to replace? 1) Last Name, 2) First Name, … [list all options]
    User: 1
    System: Look up the user by… 1) Last Name, 2) First Name, … [list all options]
    User: 2
    System: Enter user’s first name
    User: Michael
    System: Enter user’s new last name
    User: Rogers
    System: Information for user Michael changed to: [output Michael’s record]
    System: ***** Address book **************************
    System: [Full output of the entire list after the change, formatted]

    this is what i have been working on so far of course it is not complete
    i got hung up on how to put all the information in......and also i think the way i am going to run the program looks really messy so far but i was trying to use functions for the questions to make it easier for me to read
    all i need to import and want to import is the javax.swing.*; that i always import

    p.s. i know this is prolly horribly wrong

    import javax.swing.*;
     
    public class xm1
    {
      public static void main(String[] args)
      {
        String[][] addressBook = new String[5][6];
        String[][] names = {{"Miller ", "Frank ", "Roland ", "Smith", "Laurel"},
                            {"Joseph", "Michael", "Frank", "Jan", "Mary"},
                            {"910 Auburn Avenue", "1 York Road", "346 Bellona Avenue", "432 Burke Avenue", "435 Saint Francis Avenue"},
                            {"McLean", "Baltimore", "Lutherville", "Towson", "Bowie"},
                            {"VA", "MD", "MD", "MD", "MD"},
                            {"7035551234", "4105551234", "4435551234", "4105559876", "4435554567"}}
     
     
     
     
        }
     
     
        questionOne();
     
        public static void questionOne()
        {
          Int firstQuestion = JOptionPane.showInputDialog("What do you wish to replace?\n1) First Name\n2) Last Name\n3) Address\n4 Zip Code");
          If(firstQuestion == 1)
               {
            System.out.println("Replace First Name");
            Int secondQuestion = JOptionPane.showInputDialog("Look up user by:\n1) First Name\n2) Last Name\n3) Address\n4 Zip Code");
            If(secondQuestion == 1)
            {
              String firstName = JOptionPane.showInputDialog("Enter User's First Name");


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: 2D array help plz

    If you're not bound by any certain restrictions to this piece of code, Id suggest creating yourself a new class that can hold each address record.

    public class AddressEntry {
     
        private String firstName;
     
        private String lastName;
     
        private String street;
     
        private String city;
     
        private String state;
     
        private String phoneNumber;
     
        // Other methods here such as getters and setter.
    }

    And then just have a List or Array if you like of these records.

        List<AddressEntry> addressBook = new ArrayList<AddressEntry>();
     
        addressBook.add(someAddressEntryYouHaveCreatedAndPopulated);

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array help plz

    ok here is the updated code now i just cant figure out how to make the method to replace the elements or variables in my array to work
    import javax.swing.*;
     
    public class xm3
    {
      public static void main(String[] args)
      {
     String[][] addressBook = 
        { {"Miller ",   "Frank ",  "Roland ",   "Smith",   "Laurel"},  
        {"Joseph",   "Michael",  "Frank",   "Jan",    "Mary"},  
        {"910 Auburn Avenue", "1 York Road", "346 Bellona Avenue", "432 Burke Avenue", "435 Saint Francis Avenue"},
        {"McLean",   "Baltimore",  "Lutherville",   "Towson",   "Bowie"},  
        {"VA",    "MD",   "MD",    "MD",    "MD"},   //state
        {"7035551234",   "4105551234",  "4435551234",   "4105559876",   "4435554567"}  
          };
     
     String inputStr = "";         
     String[] choices = {"first name", "last name", "address", "zip code"};
     int input = Integer.parseInt(JOptionPane.showInputDialog("What do you wish to replace?\n" +
            "1. " + choices[0] + "\n" +
            "2. " + choices[1] + "\n" +
            "3. " + choices[2] + "\n" +
            "4. " + choices[3] + "\n"));
     
     
     if(input == 1) {
     
      replace();
     
     }
     
     else if(input == 2) {
     
      replace();
     
     }
     
     else if(input == 3) {
     
       replace();
     
     }
     
     else if(input == 4) {
     
       replace();
     
     }
     
     
     
     else{
     
      System.out.println("error"); 
      System.exit(0);
     
     }
     
      }
     
      public static void replace() {
     
       String[] choices = {"first name", "last name", "address", "zip code"};
       int input = Integer.parseInt(JOptionPane.showInputDialog("Look up the user by: \n" +              
           "1. " + choices[0] + "\n" +
           "2. " + choices[1] + "\n" +
           "3. " + choices[2] + "\n" +
           "4. " + choices[3] + "\n"));
     
     
     if (input == 1) {
     
     
     
    for(int rows = 0; rows < 6; rows++) { 
     
     
     if(rows == input) { 
     
      for(int columns = 0; columns < 5; columns++) { 
     
      tempStr = addressBook[rows][columns]; 
     
       if(tempStr.equalsIgnoreCase(infoToBeReplaced)) {
     
        addressBook[rows][columns] = replacingInfo;  
     
       }  
     
      }
     
     }
     
    }
     
     
     
     
     }
     
      }
     
    }

Similar Threads

  1. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM