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 8 of 8

Thread: User Input/Output for Math Calculations - Java

  1. #1
    Junior Member Infinite's Avatar
    Join Date
    Jul 2013
    Posts
    3
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default User Input/Output for Math Calculations - Java

    Hey everyone- I'm new here and this is my first forum post so I hope this is in the right place!
    I would appreciate all the help I can get:

    I'm trying to make a Java code to find the volume of a sphere but can't seem to use the user's input because I'm getting an error that "keyboard.readLine()" does not represent a real integer. I've attached my code below - I'm sure it's poorly written but I wouldn't know how to properly write it until I fix the input problem- I'd like to use the user's input in the volume equation which follows- thanks everyone!

    import java.io.*; // Stores information between user and computer.
    public class VolumeOfSphere
    {
    public static void main (String[] args) throws IOException

    {

    String radius;
    DataInputStream keyboard = new DataInputStream(System.in);
    System.out.println("What is the radius of the sphere: ");
    radius = keyboard.readLine();
    volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );

    System.out.println("The volume of the sphere is: " + volume);


    }
    }


  2. #2
    Junior Member Infinite's Avatar
    Join Date
    Jul 2013
    Posts
    3
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple error with math calculation code. Thanks!

    Hey everyone- I'm new here and this is my first forum post so I hope this is in the right place!
    I would appreciate all the help I can get:

    I'm trying to make a Java code to find the volume of a sphere but can't seem to use the user's input because I'm getting an error that "keyboard.readLine()" does not represent a real integer. I've attached my code below - I'm sure it's poorly written but I wouldn't know how to properly write it until I fix the input problem- I'd like to use the user's input in the volume equation which follows- thanks everyone!

    import java.io.*; // Stores information between user and computer.
    public class VolumeOfSphere
    {
    public static void main (String[] args) throws IOException

    {

    String radius;
    DataInputStream keyboard = new DataInputStream(System.in);
    System.out.println("What is the radius of the sphere: ");
    radius = keyboard.readLine();
    volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );

    System.out.println("The volume of the sphere is: " + volume);


    }
    }

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: User Input/Output for Math Calculations - Java

    Welcome Infinite

    Please see the Announcements page for the use of code tags

    Look at the Scanner (Java 2 Platform SE 5.0) for getting keyboard input and the tutorials on basic IO and the sub-section on scanner

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple error with math calculation code. Thanks!

    Please do not waste everyone's time double posting questions.

    Threads merged

  5. #5
    Junior Member Infinite's Avatar
    Join Date
    Jul 2013
    Posts
    3
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple error with I/O calculation code.

    Hey everyone- I'm new here and this is my first forum post so I hope this is in the right place!
    I would appreciate all the help I can get:

    I'm trying to make a Java code to find the volume of a sphere but can't seem to use the user's input because I'm getting an error that "keyboard.readLine()" does not represent a real integer. I've attached my code below - I'm sure it's poorly written but I wouldn't know how to properly write it until I fix the input problem- I'd like to use the user's input in the volume equation which follows- thanks everyone!

    import java.io.*; // Stores information between user and computer.
    public class VolumeOfSphere
    {
    public static void main (String[] args) throws IOException
     
    {
     
    String radius;
    DataInputStream keyboard = new DataInputStream(System.in);
    System.out.println("What is the radius of the sphere: ");
    radius = keyboard.readLine();
    volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); 
     
    System.out.println("The volume of the sphere is: " + volume);
     
     
    }
    }

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: User Input/Output for Math Calculations - Java

    Again with the duplicate posts.
    threads merged
    again

  7. #7
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: User Input/Output for Math Calculations - Java

    "I'm getting an error that "keyboard.readLine()" does not represent a real integer."

    its telling you your error is with that line so look at the scanner link jps provided and see if there is one that deals with integers

    also just a little bit of info when you are dealing with number variables you will use (int, long, float, double...etc)

  8. #8
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    To read an integer you should use .readInt.. There are different methods for each datatype..

    For a string = readline
    For a integer = readInt

    And so on..

    Hope this helps

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. Math calculations
    By mikem1034 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2013, 10:13 AM
  3. Need help with user input/output for GUI.
    By Tctwins in forum AWT / Java Swing
    Replies: 6
    Last Post: February 20th, 2012, 04:13 PM
  4. User input and calculations problems
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 7th, 2011, 07:32 PM
  5. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM