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);

}

}

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);

}

}

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

Re: Simple error with math calculation code. Thanks!

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

Threads merged

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!

Code :

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);
}
}

Re: User Input/Output for Math Calculations - Java

Again with the duplicate posts.

threads merged

again

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)