У меня в основном есть программа, которая читает элементы бакалеи из файла & хранит их в массиве. Он позволяет добавлять элементы пользователем (имя элемента, количество элементов) и удаление элементов из массива.Как отредактировать элемент в массиве?
Однако у меня возникли проблемы с Редактировать Элемент в массиве? Таким образом, меняем свое имя и количество элементов.
Вот три из моих кодов методов для добавления, редактирования (нужна помощь) и удалять:
/**
* 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 ;
}
Любая помощь будет очень высоко: о!
у вас возникли проблемы? Какие проблемы? .. – Maroun
SO не сервисный центр ':)' – ADTC
вы можете использовать ArrayList вместо String Array –