I am writing a program in Java which accepts user-inputted String in one class.
我正在用Java编写一个程序,它在一个类中接受用户输入的String。
On a separate class, I have an array-list of class type 'Item' which contains elements of type String (itemName), int, and double. I was wondering if there was a way to either convert the user-inputted String to an object of type 'Item' (I've heard it's difficult), or if there was a way to access the individual String element itemName of the array-list to compare it to the user-inputted String.
在一个单独的类中,我有一个类类型'Item'的数组列表,它包含String(itemName),int和double类型的元素。我想知道是否有办法将用户输入的String转换为'Item'类型的对象(我听说很难),或者如果有办法访问数组的单个String元素itemName-列表将其与用户输入的字符串进行比较。
Item.java
public class Item {
private String name;
private int monetaryValue;
private double weight;
// Getters and Setters
// ...
// Other methods
// ...
}
3 个解决方案
#1
3
I would not use Reflection here: it's using a bazooka for killing a mosquito. I'd rather use plain Java.
我不会在这里使用反射:它使用火箭筒杀死蚊子。我宁愿使用普通的Java。
Check this example below:
请查看以下示例:
List<Item> myList = new ArrayList<Item>();
String userInputValue;
// * Add some items to myList
// ...
// * Get user input value
// ...
// * Access the array list
int len=myList.size();
for(int i=0; i<len; i++) {
if (myList.get(i).getItemName().equals(userInputValue)) {
// Do something ...
}
}
#2
1
To create an Item
from user input, you can do:
要从用户输入创建项目,您可以执行以下操作:
String input1;
String input2;
String input3;
// Assign user input to input1, input2, input3
String itemName = input1;
int data2 = Integer.parseInt(input2);
double data3 = Double.parseDouble(input3);
Item myItem = new Item(itemName, data2, data3);
To access elements from array list, you can do:
要从数组列表中访问元素,您可以执行以下操作:
List<Item> items;
String input;
// Populate items
// Assignment user input to "input" variable.
for (Item item : items) {
if (item.getItemName().equals(input)) {
// Do something...
}
}
#3
0
You can of course build Item
objects from user input if you define an input format like [name:string] [i:int] [d:double]
(example : john 5 3.4
). You then just have to split this String and use Integer.parseInt and Double.parseDouble to parse the two last arguments.
如果您定义输入格式,如[name:string] [i:int] [d:double](例如:john 5 3.4),您当然可以从用户输入构建Item对象。然后,您只需要拆分此String并使用Integer.parseInt和Double.parseDouble来解析最后两个参数。
#1
3
I would not use Reflection here: it's using a bazooka for killing a mosquito. I'd rather use plain Java.
我不会在这里使用反射:它使用火箭筒杀死蚊子。我宁愿使用普通的Java。
Check this example below:
请查看以下示例:
List<Item> myList = new ArrayList<Item>();
String userInputValue;
// * Add some items to myList
// ...
// * Get user input value
// ...
// * Access the array list
int len=myList.size();
for(int i=0; i<len; i++) {
if (myList.get(i).getItemName().equals(userInputValue)) {
// Do something ...
}
}
#2
1
To create an Item
from user input, you can do:
要从用户输入创建项目,您可以执行以下操作:
String input1;
String input2;
String input3;
// Assign user input to input1, input2, input3
String itemName = input1;
int data2 = Integer.parseInt(input2);
double data3 = Double.parseDouble(input3);
Item myItem = new Item(itemName, data2, data3);
To access elements from array list, you can do:
要从数组列表中访问元素,您可以执行以下操作:
List<Item> items;
String input;
// Populate items
// Assignment user input to "input" variable.
for (Item item : items) {
if (item.getItemName().equals(input)) {
// Do something...
}
}
#3
0
You can of course build Item
objects from user input if you define an input format like [name:string] [i:int] [d:double]
(example : john 5 3.4
). You then just have to split this String and use Integer.parseInt and Double.parseDouble to parse the two last arguments.
如果您定义输入格式,如[name:string] [i:int] [d:double](例如:john 5 3.4),您当然可以从用户输入构建Item对象。然后,您只需要拆分此String并使用Integer.parseInt和Double.parseDouble来解析最后两个参数。