我正在制作一个交换应用程序,但我变得沮丧并且遇到了一些麻烦。我知道我需要做什么,但为了到达那里我迷失了,如果这是有道理的。我只需要帮助我们获取结果的步骤,没有直接的代码。方向说"从提供的文本文件[currency.txt]中搜索textFields [CurrencyTo和CurrencyFrom]中输入的国家/地区的三个字母的货币代码。例如,如果CurrencyFrom为“United States”,则您的代码应从currency.txt文件中找到货币代码“USD”。"
我知道我需要(1)将文本框中的国家/地区与inputFile中的国家/地区进行比较 (2)如果相同,则获取国家代码并将其保存在变量中。 我需要采取的任何建议或细分都是我正在寻找的。请不要直接代码!谢谢
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);
}
}