r/StackoverReddit • u/Polixa12 • Jul 08 '24
Java First Mini Java "Project".
package Currencies;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class converterInterface {
private String input;
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
converterInterface c = new converterInterface();
c.myInterface();
}
protected String ThrowInvalidValueException() {
String error = "Invalid value!!!";
System.out.println(error);
return error;
}
public void RequestUserInput() {
System.out.print("There are 5 currencies that can be converted: Naira, Euros, Cedis, Pounds, Dollars.");
System.out.print(
"\nType in the individual name of the currency you want to convert, then type in the individual name of the currency you want to convert it to.");
System.out
.print("\nNOTE: You can not convert a currency to itself!! You can type in \"exit\" to stop the application");
while(___){
System.out.println("\nWhat currency would you like to convert: ");
input = scanner.nextLine().toLowerCase();
myInterface();
}
}
public void myInterface() {
String sInput = null;
switch (input) {
case "naira":
Naira naira = new Naira();
System.out.print("To what currency would you like convert Naira: ");
sInput = scanner.nextLine().toLowerCase();
switch (sInput) {
case "euros":
naira.NairaToEuros();
break;
case "dollars":
naira.NairaToDollars();
break;
case "pounds":
naira.NairaToPounds();
break;
case "cedis":
naira.NairaToCedis();
break;
default:
ThrowInvalidValueException();
}
break;
case "cedis":
Cedis cedis = new Cedis();
System.out.print("To what currency would you like convert Cedis: ");
sInput = scanner.nextLine().toLowerCase();
switch (sInput) {
case "euros":
cedis.CedisToEuros();
break;
case "dollars":
cedis.CedisToDollars();
break;
case "pounds":
cedis.CedisToPounds();
break;
case "naira":
cedis.CedisToNaira();
break;
default:
ThrowInvalidValueException();
}
break;
case "pounds":
Pounds pounds = new Pounds();
System.out.print("To what currency would you like convert Pounds: ");
sInput = scanner.nextLine().toLowerCase();
switch (sInput) {
case "euros":
pounds.PoundsToEuros();
break;
case "dollars":
pounds.PoundsToDollars();
break;
case "naira":
pounds.PoundsToNaira();
break;
case "cedis":
pounds.PoundsToCedis();
break;
default:
ThrowInvalidValueException();
}
break;
case "euros":
Euros euros = new Euros();
System.out.print("To what currency would you like convert Euros: ");
sInput = scanner.nextLine().toLowerCase();
switch (sInput) {
case "pounds":
euros.EurosToPounds();
break;
case "dollars":
euros.EurosToDollars();
break;
case "naira":
euros.EurosToNaira();
break;
case "cedis":
euros.EurosToCedis();
break;
default:
ThrowInvalidValueException();
}
break;
case "dollars":
Dollars dollars = new Dollars();
System.out.print("To what currency would you like convert Dollars: ");
sInput = scanner.nextLine().toLowerCase();
switch (sInput) {
case "pounds":
dollars.DollarsToPounds();
break;
case "euro":
dollars.DollarsToEuros();
break;
case "naira":
dollars.DollarsToNaira();
break;
case "cedis":
dollars.DollarsToCedis();
break;
default:
ThrowInvalidValueException();
}
break;
default:
ThrowInvalidValueException();
}
}
}This is a mini currency converter app I made. All the values for conversion were already predetermined by me. I'm having some trouble trying to loop through the code in the RequestUserInput method cause it keeps throwing this error {"What currency would you like to convert: Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at Currencies.converterInterface.myInterface(converterInterface.java:31) at Currencies.converterInterface.main(converterInterface.java:8)"} at me and idk what to do. This code is also pretty crude as I am new to java.
4
Upvotes
View all comments
Show parent comment
1
u/Grizzly_Addams Jul 08 '24
Naming convention. converterInterface should be ConverterInterface since it is a class name. Also, I would say definitely don't put "interface" in the name especially when this class isn't an interface, and even if it was an interface I wouldn't recommend putting it in the name, the implementation itself can speak to what it is.
The switch statements would make more sense as else/if. Also, that method can be broken apart into several different methods based the type of conversion. You'll see these sort things easier once you start trying to unit test this functionality. Single purpose methods are key to reusability.