Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree1Likes

Thread: Using a remove() method to remove a row of linked list objects from an input file?

  1. #1
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Using a remove() method to remove a row of linked list objects from an input file?

    So I am having an issue : how would I format my remove () method inside my code that will ask the user which row to remove from a input file that was stored from a linked list?
    The way my code goes like this:

    1.Read from an input file

    2.Store the input file objects into an linked list

    3.Display the list from the inputfile

    4.Ask the user which rows to delete

    5.Display it again with the deleted row of objects gone from the list

    Here is my code so far:
    Java Code:
    /**
    * This is a program that reads from an input file,
    * stores the data in a LinkedList of objects, and 
    * and output the LinkedList to the screen.
    * @author Desierto, Aldi Alfredo
    * ICS 211 Assignment 17 
    * Today's Date October 19, 2016
    */    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.List;
    import java.util.LinkedList;
    public class DesiertoAldi17 {
    /**
    * The main method starts the program
    *
    *@param args is the input file
       */
    
       public static void main(String[] args) {
       //error checking for commandline input
          if(args.length != 1){
             System.out.println("Please enter at least one input file into the argument.");
             //terminates the program if more than 1 is entered
             System.exit(1);
          }
       
       
          String csvFile = args[0];
          String line = "";
          String cvsSplitBy = ",";
       
          List<HawaiiNativeForestBirds>  listofBirds = new LinkedList<HawaiiNativeForestBirds>();
          try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
          
             while ((line = br.readLine()) != null) {
             
                // use comma as separator
                String[] bird = line.split(cvsSplitBy);
                HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
                listofBirds.add(Hawaiinbird);
                
             }
          
          } 
          catch (IOException e) {
             e.printStackTrace();
          }
       // Display the actual values
          HawaiiNativeForestBirds[]  hbirds=new        HawaiiNativeForestBirds[listofBirds.size()]; 
          int i=0;        
          hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
          System.out.println("Display HawaiiNativeForestBirds linked list after adding objects from the input file into a linked list:");
          System.out.println("index   " + "name   "+ "   Scientific Name     "+ "        Color     " +       "      Population");        
          i=0;
          for (HawaiiNativeForestBirds hbird:hbirds){
             i++;
             System.out.println(i+"   "+hbird.toString());
          }
       
       //remove method that asks the user for input on which row to delete
       
       
       
       // Display values again with the deleted row of data
                 
          hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
          System.out.println("Display HawaiiNativeForestBirds linked list after deleting a row of objects from the linked list:");
          System.out.println("index   " + "name   "+ "   Scientific Name     "+ "        Color     " +       "      Population");        
          i=0;
          for (HawaiiNativeForestBirds hbird:hbirds){
             i++;
             System.out.println(i+"   "+hbird.toString());
          }
       
       
       } // end of main 
    } //end of class 
    
    /**
       * Class HawaiianNativeForestBirds stores and displays the data for each HawaiianTheme object
       * 
       * @author Aldi Desierto
       */
      
     
    class HawaiiNativeForestBirds {
       private String name;
       private String scientificname;
       private String color;
       private Integer population;
       public HawaiiNativeForestBirds(){
       
       }
       //constructor - used to initialize the four data fields
          /**
       * Stores the name,scientific name, color and population of the Hawaiian Birds
        *  
        * 
        * @param name is the name of the birds from the linked list
        * @param scientificname is the scientific name of the birds in the linked list
        * @param color is the color of each of the birds in the linked list
        * @param population is the total number of birds in the linked list   
        */
    
       public HawaiiNativeForestBirds(String name, String scientificname,
            String color, Integer population) {
          super();
          this.name = name;
          this.scientificname = scientificname;
          this.color = color;
          this.population = population;
       }  
    /**
        * Gets each bird's name
        * 
        * @return the birds name
        */
    
    
       public String getName() {
          return name;
       }
        /**
        * Sets each birds name
        * 
        * @param name is the bird's name
        */
    
       public void setName(String name) {
          this.name = name;
       }
       /**
        * Gets the bird's scientific name
        * 
        * @return the bird's scientific name
        */
    
       public String getScientificname() {
          return scientificname;
       }
        /**
        * Sets the birds scientific name
        * 
        * @param scientificname is the bird's scientific name
        */
    
       public void setScientificname(String scientificname) {
          this.scientificname = scientificname;
       }
       /**
        * Gets the bird's color
        * 
        * @return the bird's color
        */
    
       public String getColor() {
          return color;
       }
        /**
        * Sets the bird's color
        * 
        * @param color is the bird's color
        */
    
       public void setColor(String color) {
          this.color = color;
       }
       /**
        * Gets the bird's population
        * 
        * @return total population of the bird
        */
    
       public Integer getPopulation() {
          return population;
       }
        /**
        * Sets the bird's population
        * 
        * @param population is the total population of the bird
        */
    
       public void setPopulation(Integer population) {
          this.population = population;
       }
      /**
        * Display the output 
        * 
        * @return the name, scientificname, color, and population of the bird
        */
    
       public String toString() {
          String output = name +"     "+      scientificname + "             "+ color +"           "+        population;
          return output;
       }//end of toString 
               
    }// end of class
    Here is my inputfile :
    Using a remove() method to remove a row of linked list objects from an input file?-94b22e23f1e87b5c3293ac93794786e7.png

    Here is what I need to do:
    //ask the user to enter the row number of the object to be removed from the linked list
    //include error checking for non-integers and integers out of range
    //display the object that will be deleted
    //and then remove the object from the linked list


    I had a method or a way I wanted to do but It is using Integer and array it seems instead of Linked List
    :
    Java Code:
    public static Integer delete(String[] itemNames4, 
       Integer[] itemNumbers4, Integer listSize, String[] StoreNames4) {
          // get user input
          System.out.print("Enter the row number of the item you wish to delete: ");
          Scanner keyboard = new Scanner(System.in);
          try {
             // throws an exception if not an integer
             Integer row = keyboard.nextInt();
             // check for negative integers
             if (row <= 0) {
                System.out.println("ERROR: The row number cannot be negative or zero.");
             }
              // check for row number too big
             else if (row > listSize + 1) {
                System.out.println("ERROR: The row number is too big for the list.");
             } 
             else {
             //convert row to an index in the array by subtracting one
                Integer index = row - 1;
                //give feedback to user to let them know what is being deleted
                System.out.println("Deleting row #" + row + ": " 
                   + itemNames4[index] + " " + itemNumbers4[index]+ " " + StoreNames4[index]); 
                
                // delete item by shifting items on the right of the item to the left
                for (int i = index; i < listSize; i++) {
                   itemNames4[i] = itemNames4[i + 1];
                   itemNumbers4[i] = itemNumbers4[i + 1];
                   StoreNames4[i] = StoreNames4[i + 1];
                }
                // subtract one from the size (one item deleted from list)
                --listSize;
             }
          } 
          catch (InputMismatchException exception) {
             System.out.println("ERROR: You must enter an integer to delete an item.");
          }
          return listSize;
       }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Where is the code for the remove() method? I don't see it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    I guess we are supposed to fill the blanks in rows 63 - 65.

    Row 58 points to the issue: You have to work with the index of the LinkedList. For the list itself one would sysout the index plus the value behind that index. You get the index with hBirds.indexOf(hBird).

    The remove method then takes the chosen index, so it would look something like hBirds.remove(yourChoiceAsInt).

  4. #4
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Quote Originally Posted by benji2505 View Post
    I guess we are supposed to fill the blanks in rows 63 - 65.

    Row 58 points to the issue: You have to work with the index of the LinkedList. For the list itself one would sysout the index plus the value behind that index. You get the index with hBirds.indexOf(hBird).

    The remove method then takes the chosen index, so it would look something like hBirds.remove(yourChoiceAsInt).
    Sorry! I just woke up . I've been trying to do this since last night as I don't how I would format my remove method and ask the user and also get from the index of my hbirds LinkedList.

    The format I was going for was the 2nd code I posted but since I am using LinkedList and not ArrayList.

    for the row 58 one that you refer to, do I do another sysout print that gets the index? How would I search each line of my index to find that specific row that I need to delete?

    I'll try to add them piece by piece of what I know to do so far, I don't know if this is the right format:

    /
    Java Code:
    / //ask the user to enter the row number of the object to be removed from the linked list (don't know how to start this part, Do i start by sysout"ing the index?)
    
    System.out.println("Index for HawaiianNativeForestbirds:" + hbirds.indexOf(hbird)); //currently giving me an error ,changing hbirds to listofbirds would give me an index of -1
    
    System.out.print("Enter the row number of the item you wish to delete: ");        //seems it's not initiating my scanner to ask the user. 
          Scanner keyboard = new Scanner(System.in);
    
    //include error checking for non-integers and integers out of range
    try {
        Integer row = keyboard.nextInt();  //this is my nextINt but Integer row should be change i think, what should I name my row or rather do I use hbirds.indexOf(hbird)?
        
        if (row <= 0) {
                System.out.println("ERROR: The row number cannot be negative or zero.");
        // check for row number too big
        
       else if (row > listofBirds.size + 1) {
                System.out.println("ERROR: The row number is too big for the list.");
       else {
        
       //display the object that will be deleted
    System.out.println("Deleting row #" + row + ": " 
                   + name[index] + " " + scientificname[index]+ " " + color[index]);+ " " + population[index]);
    
    //and then remove the object from the linked list
    hbirds.remove((yourChoiceAsInt).     //the user input
       
    
    // delete item by shifting items on the right of the item to the left     //this is pretty much from the 2nd code but this is what i think I'm going for
                for (int i = index; i < listSize; i++) {
                   itemNames4[i] = itemNames4[i + 1];
                   itemNumbers4[i] = itemNumbers4[i + 1];
                   StoreNames4[i] = StoreNames4[i + 1];
                }
                // subtract one from the size (one item deleted from list)
                --listSize;
             }
          } 
          catch (InputMismatchException exception) {
             System.out.println("ERROR: You must enter an integer to delete an item.");
          }
          return listSize;
    that is pretty much what I'm going for. Last part is just me using my tostring method to show it again with the deleted row gone.
    Last edited by aldz24; 10-20-2016 at 09:30 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    I suggest that lines 1 through 23 be placed in a method that returns the index of the item to be removed or -1 if there is not to be a removal.

    Then the method could easily be tested with a special purpose driver that called it and displayed what was returned.
    That would isolate the testing of the method from any problems in the rest of the code.
    benji2505 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Quote Originally Posted by Norm View Post
    I suggest that lines 1 through 23 be placed in a method that returns the index of the item to be removed or -1 if there is not to be a removal.

    Then the method could easily be tested with a special purpose driver that called it and displayed what was returned.
    That would isolate the testing of the method from any problems in the rest of the code.
    Would I be able to do this without using a method? or rather is there an easier way of doing this by just asking the user, check if the input of the user is between the range of the index, scans the index and deletes the row and just continue to the tostring method?

    If I had to do the method, what would the format be ?
    Last edited by aldz24; 10-20-2016 at 09:27 PM.

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    I like Norm's suggestion.

    Lazy as I am I would put the remove() method in a try-catch block and if the remove() method can't use the index it just returns "sorry-didn't work, better luck next time".

    Concerning the printout of the original collection: concatenate the index (see above), a column plus a space, and the existing hBird.toString(). Then the user gets to choose.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Would I be able to do this without using a method?
    It's time to learn how to write methods. The delete() method in post #1 was trying to do two things. It should be split into two methods: 1) get the index to delete, 2) delete the item at an index
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    would you show me an example of how it would look like without giving away too much, sorry if I try to ask too much out of it but a skeleton or a format would be great,

    like the get index and the delete item at an index?

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    check post #3

  11. #11
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Quote Originally Posted by benji2505 View Post
    check post #3
    I'm a visual kind of person so lemme just try and decipher this:

    change this:

    System.out.println("Index for HawaiianNativeForestbirds:" + hbirds.indexOf(hbird)); ??

    or
    hBirds.indexOf(hBird)

    or

    change line 58? and add the indexof?

  12. #12
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Mmmmh.

    The argument in your method in line 59 should be coded that way that you sysout exactly what you have in mind, so something like:

    0: Wooloomooloo bird
    1: Golf_birdie
    2: Birdman
    ...

    just the first letter in each line comes from the actual index, not some variable you introduced earlier.

    then the user gets choose which index,

    then you remove the element after checking whether selection is true (or try-catch block).

  13. #13
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Quote Originally Posted by benji2505 View Post
    Mmmmh.

    The argument in your method in line 59 should be coded that way that you sysout exactly what you have in mind, so something like:

    0: Wooloomooloo bird
    1: Golf_birdie
    2: Birdman
    ...

    just the first letter in each line comes from the actual index, not some variable you introduced earlier.

    then the user gets choose which index,

    then you remove the element after checking whether selection is true (or try-catch block).
    Do i start with a method then or do I just ask the user immediately?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    The code in post#4 from lines 5 to 23 are the bulk of the method.
    The method would:
    ask the user for input
    read the user's response
    verify the response
    if not valid, put out an error message and loop back to the first step
    if the response is valid, return it.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Quote Originally Posted by Norm View Post
    The code in post#4 from lines 5 to 23 are the bulk of the method.
    The method would:
    ask the user for input
    read the user's response
    verify the response
    if not valid, put out an error message and loop back to the first step
    if the response is valid, return it.

    Alrighty but I have a few sections of this part that needs editing, seems I'm almost done it seems like except for the key pieces.
    I'll add comments on the code that needs change
    Java Code:
    System.out.print("Enter the row number of the item you wish to delete: ");        //seems it's not initiating my scanner to ask the user. 
          Scanner keyboard = new Scanner(System.in);
    
    //include error checking for non-integers and integers out of range
    try {
        Integer row = keyboard.nextInt();  //how do I format this so it reads the input linked list  and reads the users response?
        
        if (row <= 0) {
                System.out.println("ERROR: The row number cannot be negative or zero.");
        // check for row number too big
        
       else if (row > listofBirds.size + 1) {
                System.out.println("ERROR: The row number is too big for the list.");         //these are my errors if ever user puts the wrong output or not in the range of that list so they should be good.
       else {
        
       //display the object that will be deleted
    System.out.println("Deleting row #" + row + ": " 
                   + name[index] + " " + scientificname[index]+ " " + color[index]);+ " " + population[index]); 
    } 
          catch (InputMismatchException exception) {
             System.out.println("ERROR: You must enter an integer to delete an item.");
          }
          return listSize;
    }
    where would implement the hBirds.remove(yourChoiceAsInt) method here?
    Last edited by aldz24; 10-20-2016 at 11:36 PM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    Where is the method's definition statement showing what it returns?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    sorry edited my code

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    The method definition statement is still missing.

    What value is the method supposed to return? What value is it returning on line 23?

    What happens when you compile that code?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    What happened to the simple method to get an int value from the user?

    This code with generics (<T> ) has gotten more complicated than what I thought you were working on earlier.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using a remove() method to remove a row of linked list objects from an input file

    I stepped on the sideline to reduce confusion for now, but quickly wanted to make one remark that is not relevant for the task on hand:
    The naming of the class in line 78 will bite you sooner or later.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2013, 12:21 PM
  2. Replies: 3
    Last Post: 05-06-2010, 07:10 AM
  3. Replies: 0
    Last Post: 09-21-2009, 11:33 AM
  4. How to remove Control Characters from an input file?
    By renjan in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2007, 03:33 PM

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
  •