hello,
I have a little assignment where I have to create a primitive currency converter from CAD to NR. Below is the code I have so far:
import java.util.Random; public class BigDecisions { static public void main( String[] args) { System.out.println("Please enter the exchange rate: \n"); ConsoleReader console = new ConsoleReader(System.in); float rate = console.readInt(); System.out.println("\nThe rate you entered was: " + rate); System.out.println("\nPress '0' to convert to NR, Press 1 to convert to CAD: "); ConsoleReader console2 = new ConsoleReader(System.in); float num = console2.readInt(); System.out.println("Please enter the amount to be converted: "); ConsoleReader console3 = new ConsoleReader(System.in); float num2 = console3.readInt(); if ( num == 1 ) System.out.println(num2 + " Indian Rupies converts to " + rate * num2 + " Canadian Dollars"); else if ( num == 0 ) System.out.println(num2 + " Canadian Dollars converts to " + (1-rate) * num2 + " Indian Rupies"); } }
Even though my math calculations are incorrect, I can get it to work if I use whole numbers. My problem is that I need to use decimal points.
The error message I recieve refers to the ConsoleReader class I used which is written below:
import java.io.InputStreamReader; import java.io.InputStream; import java.io.BufferedReader; import java.io.IOException; public class ConsoleReader { private BufferedReader reader; public ConsoleReader(InputStream inStream) { reader = new BufferedReader(new InputStreamReader(inStream)); } public String readLine() { String inputLine = ""; try { inputLine = reader.readLine(); } catch (IOException e) { System.out.println(e); System.exit(1); } return inputLine; } public int readInt() { String inputString = readLine(); int n = Integer.parseInt(inputString); return n; } public double readDouble() { String inputString = readLine(); double x = Double.parseDouble(inputString); return x; } }
The error I get refers to the line "int n = Integer.parseInt(inputString);
The error I recieve is "java.lang.NumberFormatException: For input string: "1.1"
Any idea on how I can get decimals to work with this program? any help is much appreciated.