Java Help

Discussion in 'OT Technology' started by jpatter2, Oct 3, 2006.

  1. jpatter2

    jpatter2 Member

    Joined:
    May 23, 2006
    Messages:
    46
    Likes Received:
    0
    Location:
    Oregon
    Im writting a program that converts a linear value into a metric value. Ive got the whole program wrote, but I cant get the input to accept values with decimals in them

    Here is the program:

    import java.util.*; // For Scanner class
    public class Metric {
    //---------------------------------------------------------------------------------------------------------------------------------
    // Converts linear values into centimeters, meters, and kilometers based on the linear value entered by the user.
    //
    //---------------------------------------------------------------------------------------------------------------------------------
    public static void main (String[] args)
    {
    Scanner input = new Scanner(System.in);
    // Prompt user to input a linear value
    System.out.print("Enter a linear value to convert to metric: ");
    int value = input.nextInt();
    // Converts the linear value to centimeters
    double centimeters = (value*2.54);
    System.out.println( value + " inches is " + centimeters + " centimeters ");
    // Converts the linear value to meters
    double meters = (value*0.3048);
    System.out.println( value + " feet is " + meters + " meters ");
    // Converts the linear value to kilometers
    double kilometers = (value*1.609344);
    System.out.println( value + " miles is " + kilometers + " kilometers ");
    }
    }


    The program works good to whole numbers, but I cant get it to accept user input values with decimals.


    Any help greatly appreciated!:bowdown:
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use float instead of int
     
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    float value = input.nextFloat();
     
  4. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
  5. jpatter2

    jpatter2 Member

    Joined:
    May 23, 2006
    Messages:
    46
    Likes Received:
    0
    Location:
    Oregon
    thanks guys, i ended up figuring it out

    did
    double value = input.nextDouble();
     
  6. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    for future reference put any code you want people to look at in code tags
     

Share This Page