@JanaDbouk
Aspiring developer
Profile
Posts: 20
Reg: May 27, 2013
Toronto
200
07/07/13 04:54 PM (12 years ago)

Having trouble with input/ output in Java?

The program is simply supposed to do calculations based on the users answer (but NOT if answer is >0 then do this, else do that), the users answer must be specific. What I have so far to ask the question is: import java.io.*; public class VolumeCalculator { 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(); _________________________ What I'm trying to make a simple math calculator for the volume of a sphere but I'm having trouble because I can't seem to find out which code uses the users answer and puts in in the formula volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); I know how to put the ending of the code together and add in the formula, but what is the code to use the users input because I'm getting an error saying that "keyboard.readLine" doesn't read integers or something of the sort. Maybe a little confusing... Sorry!
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
07/07/13 05:12 PM (12 years ago)
I can't say with authority. But mis-typed numerical values seems plausible. http://stackoverflow.com/questions/14526157/java-sphere-volume-calculation Hope this helps a bit! :) Cheers! -- Smug
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
07/07/13 11:13 PM (12 years ago)
I think you're over-complicating this a bit. Try adding an EditText field to the XML file for your plugin/activity. Then, in your java file, add this after you've inflated the XML file: //////////// EditText userInput = thisScreensView.findViewById(R.id.editTextId); Int userInputAsInteger = Integer.parseInteger(userInput); //////////// Make sure you've set the EditText "id" property in the XML file to "editTextId" (or change the code above to reflect your actual id). Also, make sure you've inflated the XML file into a View object with the name "thisScreensView". You can then manipulate "userInputAsInteger" however you need.
 
@JanaDbouk
Aspiring developer
Profile
Posts: 20
Reg: May 27, 2013
Toronto
200
like
07/08/13 05:14 AM (12 years ago)
Ah I figured it out! I changed the string radius; to string inputradius; and added this after keyboard.readline to change the answer to a integer and double value: radius = Double.valueOf(inputradius).doubleValue(); Thanks everyone!
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
07/08/13 06:30 AM (12 years ago)
Glad u got it figured out!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.