Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: cannot enter a floating point number

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default cannot enter a floating point number

    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.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: cannot enter a floating point number

    If you're expecting a floating point numeral use readDouble(), not readInt(). The latter method will throw a runtime exception if the string it is reading includes a decimal point.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Tedstriker (July 5th, 2013)

  4. #3
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: cannot enter a floating point number

    Worked like a charm, thanks alot.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: cannot enter a floating point number

    You're welcome.

Similar Threads

  1. Replies: 1
    Last Post: May 13th, 2013, 06:09 PM
  2. Problems getting floating point number to show in GUI...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2012, 12:48 AM
  3. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 11:18 AM
  4. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  5. Declaring floating point number depending on an if statement
    By Keys767 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2011, 02:50 PM