質問

I basically have a program that reads grocery items from a file & stores them in a array. It allows for adding of items by the user (name of item, number of items) and deleting of items from the array.

However, I'm having issues trying to edit an item in the array? So changing its name and number of items.

Here's three of my method codes for add, edit(need help) and delete:

/**
   * ADDS a grocery item to an array                          
   */
   public static Integer add(String [] list, Integer listSize){
      //get item from user
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter name of item: ");
      String name = keyboard.nextLine();
      System.out.print("Enter number of items: ");
      String number = keyboard.nextLine();        
      //add to the end of the array
      list[listSize] = name + ", " + number;
      //add one to the size (one item to end of list)
      return listSize + 1;
   }

   /**
   * EDITS a grocery item to an array
   *
   * My steps to take
   *1: Prompt user for row number to be replaced
   *2: name of new item
   *3: number of new items
   *4: error checking: If the user enters a row number 
   *   that does not exist in the list- display error message
   */
   public static Integer edit(String [] list, Integer listSize){
      Scanner userInput = new Scanner(System.in);
      System.out.print("Enter the row number of the item you would like to edit: ");
      try{
         Integer row = userInput.nextInt();
         if(row <= 0){
            System.out.println("ERROR: The number can't be negative or zero!");
         }
         //check if int is too large
         else if(row > listSize-1){
            System.out.println("ERROR: The number is too big for the list.");
         }
         else{
            for(int i=row; i<listSize; i++){
               list[i] = list[i+1];
            } 
         }
         System.out.print("Enter name of item: ");
         String name = userInput.nextLine();
         System.out.print("Enter number of items: ");
         String number = userInput.nextLine();
         list[listSize] = name + ", " + number; 
      }
      catch(InputMismatchException exception){
         System.out.println("ERROR: You must enter a number to edit an item.");
      }
      return listSize ;
   }


   /**
   * DELETES a grocery item from an array                           
   */
   public static Integer delete(String [] list, Integer listSize){
       //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 integer cannot be negative or zero.");
         }
            //check for integer too big
         else if(row > listSize-1){
            System.out.println("ERROR: The integer is too big for the list.");
         }
         else{
            //delete item by shifting items on the right of the item to the left
            for(int i=row;i<listSize;i++){
               list[i] = list[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 ;
   }

Any help would be very much appreciated :o !

役に立ちましたか?

解決

Your 'edit' method should be:

public static Integer edit(String [] list, Integer listSize){
    Scanner userInput = new Scanner(System.in);
    System.out.print("Enter the row number of the item you would like to edit: ");
    try{
        Integer row = userInput.nextInt();
        if(row <= 0){
            System.out.println("ERROR: The number can't be negative or zero!");
        }
        //check if int is too large
        else if(row > listSize-1){
            System.out.println("ERROR: The number is too big for the list.");
        }
        else{
            /*
            for(int i=row; i<listSize; i++){
                list[i] = list[i+1];
            } 
            */
        }
        System.out.print("Enter name of item: ");
        String name = userInput.nextLine();
        System.out.print("Enter number of items: ");
        String number = userInput.nextLine();
        //list[listSize] = name + ", " + number; 
        list[row] = name + ", " + number; 
    }
    catch(InputMismatchException exception){
        System.out.println("ERROR: You must enter a number to edit an item.");
    }
    return listSize ;
}

他のヒント

Isn't this your issue?

list[listSize] = name + ", " + number; 

It should be:

list[row] = name + ", " + number; 

Also you should not have the for loop:

for(int i=row; i<listSize; i++){
   list[i] = list[i+1];
}

Next time please list all your issues, not make us find them.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top