Hallo zusammen!
Ich bin erst Anfänger in Java. Ich muss als Projekt einen Währungsumrechner entwickeln. In meiner Klasse in der der Nutzer seine gewünschte Währung suchen soll, bekomme ich es nicht hin "invalid" auszugeben, falls es das Gesuchte nicht gibt. Danach soll der Nutzer nochmal etwas eingeben können, bis er eben eine der vorhandenen Währungen findet. (Ich weiß der Code ist nicht sehr schön - wurde mir schonmal gesagt - also bitte nicht meckern, wenn dann sagen, wie ich es besser machen kann ^^ )
Java:
package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class FindCurrency {
static String finalCurrency;
static double finalCurrencyValue;
public static void findTheSearchedCurrency() {
String[] currencyList = {"U.S. Dollar", "Euro", "Australian dollar", "Brunei dollar", "Canadian dollar",
"New Zealand dollar", "Singapore dollar", "Trinidadian dollar", "U.K. Pound", "Chinese yuan", "Japanese yen", "Algerian Dinar",
"Botswana Pula", "Brazilian real", "Chilean peso", "Czech koruna", "Danish krone",
"Indian rupee", "Israeli New Shekel", "Korean won", "Kuwaiti dinar", "Malaysian ringgit", "Mauritian rupee", "Mexican peso",
"Norwegian krone", "Omani rial", "Peruvian sol", "Philippine peso", "Polish zloty", "Qatari riyal",
"Russian ruble", "Saudi Arabian riyal", "South African rand", "Swedish krona", "Swiss franc", "Thai baht",
"U.A.E. dirham", "Uruguayan peso"};
double[] valueList = {194.555000, 1.928500, 16.301300, 6.837700, 1.853760, 1.711910, 1154.410000, 8.949990, 31.549400,
9.498300, 1.276790, 103.982000, 4.600090, 179.782000, 1684.980000, 0.408669, 5.885710, 58.824900, 27.525400,
2.135180, 13.261700, 0.512085, 4.953050, 70.950200, 5.920590, 4.847830, 75.464300, 4.994330, 1.853760, 21.323700,
13.499300, 1.329890, 46.718900, 9.001470, 4.891100, 1.101500, 1.331820, 53.384600};
Arrays.sort(currencyList); //Array alphabetisch sortiert
String[] upperCurrencyList = new String[currencyList.length];
for (int index1 = 0; index1 < upperCurrencyList.length; index1++) {
upperCurrencyList[index1] = currencyList[index1].toUpperCase(); //upperCurrency-Strings in einen String Array
}
Scanner scan = new Scanner(System.in);
boolean whole = true;
do {
System.out.print("Which currency do u want?: ");
String search = scan.nextLine(); //Wahl welche Währung man möchte
String searchToUpperCase = null;
for (int index = 0; index < search.length(); index++) {
if (Character.isLowerCase(search.charAt(index))) {
searchToUpperCase = search.toUpperCase();
}
}
int i;
for (i = 0; i < upperCurrencyList.length; i++) {
assert searchToUpperCase != null;
if (searchToUpperCase.equals(upperCurrencyList[i])) {
System.out.println(upperCurrencyList[i]);
whole = true;
} else if (upperCurrencyList[i].contains(searchToUpperCase)) {
System.out.print(i + ": " + upperCurrencyList[i] + "\n");
whole = true;
} else if (!upperCurrencyList[i].contains(searchToUpperCase)) {
System.err.println("invalid");
whole = false;
}
}
} while (!whole);
System.out.print("Type in number for the currency you want: ");
int currencyFinal = scan.nextInt();
finalCurrency = upperCurrencyList[currencyFinal];
finalCurrencyValue = valueList[currencyFinal];
}
}