Reputation: 3
I am creating a project in which we do a currency exchange. One of the steps is I have to get the user to input what type of currency they want to convert to from a us dollar. I have to do a while loop to check and make sure the input is correct against a text file.
The text file just has this
CD 1.05 0.93
MP 0.11 0.095
EU 1.554 1.429
So the person would enter CD MP or EU just not sure how to check it. Using eclipse and the txt file is in the project folder.
public class Communication {
public String askForCode(){
String currencyCode = JOptionPane.showInputDialog(null, "We exchange the following Currencies: \n "
+ "CD (Canadian Dollar)\n"
+ "MP (Mexican Peso) \n"
+ "EU (Euro) \n"
+ "Enter the Currency Code");
File myFile = new File("dailyrates.txt");
while (!currencyCode.equals){
}
return currencyCode;
}
Would I use the file.next line to validate it?
Upvotes: 0
Views: 490
Reputation: 2690
Files.readAllLines(Path, Charset)
will read the lines from the file to an ArrayList, which you can change into an array of strings with toArray()
. You can change your file into a path using toPath()
. You can then compare the beginning of each line with the string entered until you find the right line.
Upvotes: 0
Reputation: 347184
You could use showOptionDialog
...
Object[] options = {"CD",
"MP",
"EU"};
int n = JOptionPane.showOptionDialog(null,
"Enter the FROM Currency Code",
"From Currency",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
or even a more restrictive showInputDialog
...
Object[] possibilities = {"CD", "MP", "EU"};
String s = (String) JOptionPane.showInputDialog(
null,
"Enter the FROM Currency Code",
"From Currency",
JOptionPane.PLAIN_MESSAGE,
(Icon)null,
possibilities,
"CD");
That would restrict the users available options to what you wanted them to use
See How to Make Dialogs for more details
You could read the text file, storing the values in an array, List
or Map
and use the codes directly within the dialog to add more flexibility...
Upvotes: 1