Results 1 to 2 of 2
  1. #1
    frank1234 is offline Member
    Join Date
    Nov 2015
    Posts
    1
    Rep Power
    0

    Default Remove string from multidimensional array

    I am creating a program that is supposed to hold information entered by the user. You should be able to add, remove, and display the information. I believe I have the adding and displaying portion completed, but I am stuck on how the user is to remove a row from the array. Any help is greatly appreciated! Also does anyone have a recommendation for a book or site for beginners. I am using the textbook "Intro to Java Programming, Brief Version (10th Edition) [Y. Daniel Liang]" and am not a fan at all!!

    Java Code:
    public class Lab2Driver
    {
       public static void main(String[][] args) {
       new Lab2Driver();
       
    }
    
    public Lab2Driver(){
        Scanner input = new Scanner(System.in);
         
     String [][] member = new String [1][4];  //left at 1x4 for quick testing input. Will change to 10x4 before submission.
     
     System.out.println("Welcome to Lab 2 interface.");
     System.out.println("Please select an option from below");
     System.out.println("");
     
     while(true){
                // Give the user a list of their options
                System.out.println("1: Add member information.");
                System.out.println("2: Remove an item from the pack.");
                System.out.println("3: Display the user information.");
                System.out.println("0: Exit the Lab 2 interface.");
                
                // Get the user input
                int userChoice = input.nextInt();
                    
                switch(userChoice){
                    case 1: 
                        addMember(member);
                        break;
                    case 2: 
                        removeMember(member);
                        break;
                    
                    case 3: 
                        displayMember(member);
                        break;
                    case 0: 
                        System.out.println("Thank you for using the Lab 2 interface.");
                        System.exit(0);
                }
            }
            
        }
     
        private void addMember(String member[][]) {
            Scanner input = new Scanner(System.in);
            
            System.out.println("Enter a first name, last name, phone number, and age of the contact");
            for (int row = 0; row < member.length; row++) {
                  for (int column = 0; column < member[row].length; column++) {
                     member[row][column] = input.nextLine(); 
                  }
            }
    }
    
    private void removeMember(String member[][]) {
            // TODO: Remove a flower that is specified by the user
            
           String removeMember;
          Scanner remove = new Scanner(System.in);
          System.out.println("\nEnter the name of the member to remove: ");
    
          removeMember = remove.nextLine();
           
           }
       
    
    private void displayMember(String member[][]) {
    //Displays the entire array
            for (int row=0; row<member.length; row++){
                for (int col=0; col <member[row].length; col++){
                System.out.print(member[row][col]+" ");
            }
            System.out.println();
    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Remove string from multidimensional array

    You cannot actually remove something from an array. You can change its nature (mark as though it was deleted) but something will still be there. The only way
    to actually remove something from say a five element array resulting in a four element array is to create a new array of proper size and copy the elements to it. This is done for you in some List implementations (which is one of the reasons Lists are preferred over arrays).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. multidimensional array prob T_T
    By raffs03 in forum New To Java
    Replies: 3
    Last Post: 06-15-2013, 12:14 AM
  2. .txt file to multidimensional array
    By Hazza in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 04:37 PM
  3. Multidimensional Array
    By TyCox94 in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 11:00 PM
  4. Multidimensional Array help
    By Gaebril in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 08:36 PM
  5. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 07:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •