Java 将文本字段与文件字段进行比较
我正在制作一个exchange应用程序,但我越来越沮丧,在几个步骤上遇到了麻烦。我知道我需要做什么,但为了到达那里,我迷失了方向,如果这有意义的话。我只需要帮助采取步骤,以获得我的结果,没有直接的代码。该指示显示“从提供的文本文件[currency.txt]中搜索文本字段[CurrencyTo和CurrencyFrom]中键入的国家的三字母货币代码。例如,如果CurrencyFrom为“美国”,则您的代码应在currency.txt文件中找到货币代码“USD” 我知道我需要将文本框中的国家与输入文件中的国家进行比较 (2)如果相同,则获取国家代码并将其保存在变量中。 任何关于我需要采取的步骤的建议或细分都是我所寻找的。请不要直接编码!多谢各位Java 将文本字段与文件字段进行比较,java,user-interface,Java,User Interface,我正在制作一个exchange应用程序,但我越来越沮丧,在几个步骤上遇到了麻烦。我知道我需要做什么,但为了到达那里,我迷失了方向,如果这有意义的话。我只需要帮助采取步骤,以获得我的结果,没有直接的代码。该指示显示“从提供的文本文件[currency.txt]中搜索文本字段[CurrencyTo和CurrencyFrom]中键入的国家的三字母货币代码。例如,如果CurrencyFrom为“美国”,则您的代码应在currency.txt文件中找到货币代码“USD” 我知道我需要将文本框中的国家与输入
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==calculateButton){
String from = currencyFrom.getText();
String to = currencyTo.getText();
try{
String line = "";
File inputFile = new File("currency.txt");
Scanner in = new Scanner(inputFile);
try{
while(in.hasNextLine()){
line = in.nextLine();
String[] lineContent = line.split("\t");
System.out.println(Arrays.toString(lineContent));
System.out.println(lineContent[0] + lineContent[1]);
//compare the country in the textbox with the country from the inputFile
//if they are the same, then get the country code and save it in a variable
}
}
finally{ in.close();
}
}
catch (FileNotFoundException ex) {
displayLabel.setText("File not found");}
//go to the website and get exchagne rate and do the calculation
String line = "";
String address = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=" + from + to + "=X";
try{
URL pageLocation = new URL(address);
Scanner in = new Scanner(pageLocation.openStream());
try{
while (in.hasNextLine()){
line = in.nextLine();
System.out.println(line);
String[] data = line.split(",");
System.out.println(Arrays.toString(data));
System.out.println(data[0] + data[1] + data[2] + data[3]);
//display calculation
displayLabel.setText("");
//display last update
updateLabel.setText("Last Updated: " + data[2] + data[3]);
}
}
finally{ in.close();
}
}
catch (MalformedURLException exception){
displayLabel.setText("Website not found!");}
catch (IOException ex) {}
if(event.getSource()==clearButton){
currencyTo.setText("");
currencyFrom.setText("");
amount.setText("100");
displayLabel.setText("");
updateLabel.setText("");
}
}
}
}
//add listener to the buttons
ActionListener listener = new ButtonListener();
clearButton.addActionListener(listener);
calculateButton.addActionListener(listener);
}
}
不要使用空的
catch
块,至少要使用ex.printStackTrace()
,这样您就可以知道什么时候出错以及为什么出错。请将问题分解为各个步骤。找出如何使每个步骤独立工作,可能使用固定查询,将每个解决方案组合在一起,生成一个全面的解决方案