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: How to input a decimal via JOptionPane and echo it as a Integer

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to input a decimal via JOptionPane and echo it as a Integer

    I have been assigned to write a program which accepts a certain amount of money in standard form such as "7.53" and echo it as " You have entered 7 dollars and 53 cents", Then before any calculations I am supposed to convert that into an integer amount of cents (753). I wrote this program (shown below) but it only accepts an whole number as my input. What should I do to accept decimals and output successfully as integers?

     
    import java.util.*;
    import javax.swing.*;
     public class CoinCounter {
     public static void main (String [] args){
     Scanner sc = new Scanner(System.in);
     
     String userInput;
     int amount;
     int tooNie = 200;
     int looNie = 100;
     int quaRter = 25;
     int diMe = 10;
     int nickLes = 5;
     int peNNies = 1;
     int totalCoins = 0; 
     
     userInput = JOptionPane.showInputDialog("Please enter an amount");
     amount = Integer.parseInt(userInput);
     System.out.println("Enter amount in cents:");
     amount=sc.nextInt();
     
     
       System.out.println((amount / tooNie) + " – Toonies");
       amount = amount % 200;
       System.out.println((amount / looNie) + " – Loonies");
       amount = amount % 100;
       System.out.println((amount / quaRter) + " – Quarters");
       amount = amount % 25;
       System.out.println((amount / diMe) + " – Dimes");
       amount = amount % 10;
       System.out.println((amount / nickLes) + " – Nickles");
       amount = amount % 5;  
       System.out.println(amount + " – Pennies");
     
     }
     }


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: How to input a decimal via JOptionPane and echo it as a Integer


  3. The Following 2 Users Say Thank You to Kewish For This Useful Post:

    GregBrannon (October 2nd, 2013), quantamphysics (October 2nd, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to input a decimal via JOptionPane and echo it as a Integer

    Sorry I'm new to this posting threads stuff, I posted the previous threads without actually looking at the announcements for beginners, and I thought this one is more easier to read for viewers.

  5. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: How to input a decimal via JOptionPane and echo it as a Integer

    Okay, you're finding your feet. Try and pick up forum etiquette quickly. Not replying to people that take the time to review and respond to your thread comes across as poor etiquette.

    In regards to your problem, you say that you cannot take floating point inputs.
    Why do you think that is?
    What data type are you parsing after input?
    What happens when you change that input type to the appropriate one?
    Can you manipulate that value using math to make that floating point input represent the integer value you are after? Ie, how do you make 7.53 = 753?

Similar Threads

  1. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  2. Confirming if a User Input is an Integer/Double
    By bgroenks96 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 8th, 2011, 06:35 AM
  3. [SOLVED] Simple server client echo issues
    By Kakashi in forum Java Networking
    Replies: 4
    Last Post: March 3rd, 2011, 10:54 AM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM

Tags for this Thread