我开发了一款应用程序,下周我将进行演示。除了一个小细节外,一切准备就绪:我使用了一个条形码扫描仪,但我只返回它扫描的数字。我希望将此数字转换为产品名称。在寻找解决方案后,我没有找到合适的方法。有谁知道如何完成这个任务吗?
我开发了一款应用程序,下周我将进行演示。除了一个小细节外,一切准备就绪:我使用了一个条形码扫描仪,但我只返回它扫描的数字。我希望将此数字转换为产品名称。在寻找解决方案后,我没有找到合适的方法。有谁知道如何完成这个任务吗?
import java.io.IOException;
import javax.swing.JOptionPane;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
public static void main(final String[] args) throws IOException {
String inputValue = JOptionPane.showInputDialog("Please input barcode");
if (inputValue != null && inputValue.length() > 0) {
String url = "https://www.barcodable.com/ean/" + inputValue;
Document doc = Jsoup.connect(url).get();
JOptionPane.showMessageDialog(null, doc.title(), "result", JOptionPane.INFORMATION_MESSAGE);
}
}
所以你有一个数字,需要一个名称。这意味着你需要在某个地方建立数字和名称之间的映射关系。你没有说出这个映射关系存在于哪里,甚至是否存在。假设它不存在,你可以在你的应用程序中硬编码它,例如:
static final SparseArray<String> map;
static {
map.put(1, "Foo");
map.put(103, "Bar");
map.put(529, "Baz");
}
然后使用它,
String name = map.get(number);
SparseArray
是一种优化稀疏int / string映射的Android类。如果您知道可能数字的边界,还可以使用Map
甚至String[]
。