03-14-2005, 09:32 PM | #1 (permalink) |
Insane
Location: Padded Playhouse
|
[Java] Input line help
First off I'm a complete newbie
Now that we have that taken care of: The program has to be part of a problem we have, and then we solve it. I'm taking physics- and were on power and currents so voila - thats my problem! At my system.out.println I'm getting illegal start of expression and ")" expected. I'd appreciate any help or corrections anyone could offer. I'm not going to be a programmer- this was just sort of a fun course. I'm sorry if these are newb mistakes ( often I forget the simplest thing!). I'm really winging it here. Code:
/* This program will find the current and power if you have reistance and voltage, OR it will find voltage and power if you have current and reistance. */ import java.io.*; public class Phyiscs { double current; double power; double voltage; public static void main(String args[]) { System.out.println("This program will help you find the current and power if you have the reistance and voltage. OR It will find the voltage and power if you have the current and reistance."); System.out.println("Please type 0 if you have the resistance and voltage, or type 1 if you have the current and resistance"); String inputLine = keyboard.readLine(); if ( inputLine == 0 ) { System.out.println(" Please enter the Resistance: "); double resistance = keyboard.readLine(); System.out.println(" Please enter the voltage: "); double voltage = keyboard.readLine(); current=voltage/resitance; System.out.println( " The current is:"+current+); power=current*voltage; System.out.println( "The power is:"+power+); } else { System.out.println(" Please enter the current: "); double current = keyboard.readLine(); System.outprintln(" Please enter the resistance: "); double resistance = keyboard.readLine(); voltage=current*resistance; System.out.println("The voltage is: " +voltage+); power=current*voltage; System.out.println("The Power is:" +power+); } } } Last edited by Kalibah; 03-14-2005 at 09:57 PM.. |
03-14-2005, 10:14 PM | #2 (permalink) |
aka: freakylongname
Location: South of the Great While North
|
try this... Keep in mind that you will need to check for bad entries etc... but I got what you had to compile and run with some changes... you can't assign a string to a double, you had extra + chars in the print lines... to name a few...
Code:
import java.io.*; import java.lang.Double.*; public class Phyiscs { static double current; static double power; static double voltage; static double resistance; public static void main(String args[]) { System.out.println("This program will help you find the current and power if you have the reistance and voltage. OR It will find the voltage and power if you have the current and reistance."); System.out.println("Please type 0 if you have the resistance and voltage, or type 1 if you have the current and resistance"); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); try{ String inputLine = keyboard.readLine(); if ( Double.parseDouble(inputLine) == 0 ) { System.out.println(" Please enter the Resistance: "); resistance = Double.parseDouble(keyboard.readLine()); System.out.println(" Please enter the voltage: "); voltage = Double.parseDouble(keyboard.readLine()); current=voltage/resistance; System.out.println( " The current is:"+current); power=current*voltage; System.out.println( "The power is:"+power); } else { System.out.println(" Please enter the current: "); current = Double.parseDouble(keyboard.readLine()); System.out.println(" Please enter the resistance: "); resistance = Double.parseDouble(keyboard.readLine()); voltage=current*resistance; System.out.println("The voltage is: " +voltage); power=current*voltage; System.out.println("The Power is:" +power); } } catch(Exception e){ System.out.println("error"); } } }
__________________
"Reality is just a crutch for people who can't cope with drugs." Robin Williams. |
03-15-2005, 01:00 AM | #3 (permalink) |
Insane
Location: Padded Playhouse
|
Finally got it working thanks for help
After furthur reading of assignment yes yes I know I should read it ALL before I started- but I took one look at it and said OMG i will never be able to do this- put it down. Went to work on some physics and bam it hit me what to do. Next thing I know I start working it out - only to find out I HAVE TO USE EasyReader for user input, as well as having multiple methods So after some tweaking I got it figured out thanks for help |
Tags |
input, java, line |
|
|