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

Thread: scanner questions

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default scanner questions

    Thank you for your help in advance!

    I'm new to programming and decided that I would make a basic console based ohms law calculator as practice. The way my code works currently is that you have to answer at least one value as "0" to find for that value... I've been trying to find a way to allow a user to skip past the desired value during the input phase, but have not come up with a way to do that and still use a "double" variable. Is there a way to allow the user to just press [Enter] to continue on to the next input value and still tell the program to find for that value?

    Also, I've noticed that when I am finding for voltage and any other value I get a dummy voltage value (examples shown below)... anyone know where I went wrong? Can you point me in the right direction to research this issue?

    Again, Thank you for your help.

    Shaun



    My Code:
    package ohmslaw;
     
    import java.util.Scanner;
     
    public class Ohmslaw {
     
     
    	public static void main(String[] args) {
     
    		Scanner value = new Scanner (System.in);
     
    		//declaring base variables for voltage (v), current (i), resistance (r), and power (p).
    		double v;
    		double i;
    		double r;
    		double p;
     
    		//declaring v,i,r,p formula names.
    		double voltage;
    		double pivoltage;
    		double prvoltage;
     
    		double current;
    		double pvcurrent;
    		double prcurrent;
     
    		double resistance;
    		double vpresistance;
    		double piresistance;
     
    		double power;
    		double irpower;
    		double vrpower;
     
    		//scanner inputs to initialize base variables for formulas.
    		System.out.println("Voltage?");
    		v = value.nextDouble();
     
    		System.out.println("Current?");
    		i = value.nextDouble();
     
    		System.out.println("Resistance?");
    		r = value.nextDouble();
     
    		System.out.println("Power?");
    		p = value.nextDouble();
     
    		//v,i,r,p formulas.
    		voltage = i*r;
    		pivoltage = p/i;
    		prvoltage = Math.pow(p*r, 0.5);
     
    		current = v/r;
    		pvcurrent = p/v;
    		prcurrent = Math.pow(p/r, 0.5);
     
    		resistance = v/i;
    		vpresistance = (v*v)/p;
    		piresistance = p/(i*i);
     
    		power = v*i;
    		irpower = (i*i)*r;
    		vrpower = (v*v)/r;
     
     
    		// print statements and formula logic.
     
    		//voltage
    		if(v==0){
    			System.out.println("Voltage: " + voltage);
    		}
    		if(v==0 && r==0){
    			System.out.println("Voltage: " + pivoltage);
    		}
    		if(v==0 && i==0){
    			System.out.println("Voltage: " + prvoltage);
    		}
     
    		//current
    		if(i==0){
    			System.out.println("Current: " + current);
    		}
    		if(i==0 && r==0){
    			System.out.println("Current: " + pvcurrent);
    		}
    		if(i==0 && v==0){
    			System.out.println("Current: " + prcurrent);
    		}
     
    		//resistance
    		if(r==0){
    			System.out.println("Resistance: " + resistance);
    		}  
    		if(r==0 && i==0){
    			System.out.println("Resistance: " + vpresistance);
    		}
    		if(r==0 && v==0){
    			System.out.println("Resistance: " + piresistance);
    		}
     
    		//power
    		if(p==0){
    			System.out.println("Power: " + power);
    		}
    		if(p==0 && v==0){
    			System.out.println("Power: " + irpower);
    		}
    		if(p==0 && i==0){
    			System.out.println("Power: " + vrpower);
    		}
     
    	}
     
    }







    Example of double output (testing for voltage and resistance):

    Voltage?
    0
    Current?
    2.5
    Resistance?
    0
    Power?
    15
    Voltage: 0.0
    Voltage: 6.0
    Resistance: 0.0
    Resistance: 2.4


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: scanner questions

    Quote Originally Posted by Silverknight View Post
    I've been trying to find a way to allow a user to skip past the desired value during the input phase, but have not come up with a way to do that and still use a "double" variable.
    Use nextLine(), if the line is "empty" it means the value is skipped. If non-empty, convert to double using Double.parseDouble(String) method.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    Silverknight (January 15th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: scanner questions

    Thank you, after testing this out I think I need to do a bit more research on converting strings to a double using your Double.parseDouble(String) example.

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: scanner questions

    Quote Originally Posted by Silverknight View Post
    Thank you, after testing this out I think I need to do a bit more research on converting strings to a double using your Double.parseDouble(String) example.
    It's sufficient the javadoc parseDouble
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: scanner questions

    @Silverknight: Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Silverknight (January 15th, 2014)

  8. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: scanner questions

    So, after making a few changes, (not sure if I'm actually using the Double.parseDouble(String) method correctly.) I'm left with the same issue... I think the issue now lies within my logic where I ask for 0 such as v2==0.

    When I try to press [Enter] when there isn't a value given I get this error:

    Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at ohmslaw.Ohmslaw.main(Ohmslaw.java:44)

    Here is what my code looks like now:
    package ohmslaw;
     
    import java.util.Scanner;
     
    public class Ohmslaw {
     
     
    	public static void main(String[] args) {
     
    		Scanner value = new Scanner (System.in);
     
     
    		//declaring base variables for voltage (v), current (i), resistance (r), and power (p).
     
    		String v;
    		String i;
    		String r;
    		String p;
     
    		//declaring v,i,r,p formula names.
    		double voltage;
    		double pivoltage;
    		double prvoltage;
     
    		double current;
    		double pvcurrent;
    		double prcurrent;
     
    		double resistance;
    		double vpresistance;
    		double piresistance;
     
    		double power;
    		double irpower;
    		double vrpower;
     
    		//scanner inputs to initialize base variables for formulas.
    		System.out.println("Voltage?");
    		v = value.nextLine();
    		double v2 = Double.parseDouble(v);
     
    		System.out.println("Current?");
    		i = value.nextLine();
    		double i2 = Double.parseDouble(i);
     
    		System.out.println("Resistance?");
    		r = value.nextLine();
    		double r2 = Double.parseDouble(r);
     
    		System.out.println("Power?");
    		p = value.nextLine();
    		double p2 = Double.parseDouble(p);
     
    		//v,i,r,p formulas.
    		voltage = i2*r2;
    		pivoltage = p2/i2;
    		prvoltage = Math.pow(p2*r2, 0.5);
     
    		current = v2/r2;
    		pvcurrent = p2/v2;
    		prcurrent = Math.pow(p2/r2, 0.5);
     
    		resistance = v2/i2;
    		vpresistance = (v2*v2)/p2;
    		piresistance = p2/(i2*i2);
     
    		power = v2*i2;
    		irpower = (i2*i2)*r2;
    		vrpower = (v2*v2)/r2;
     
     
    		// print statements and formula logic.
     
    		//voltage
    		if(v2==0){
    			System.out.println("Voltage: " + voltage);
    		}
    		if(v2==0 && r2==0){
    			System.out.println("Voltage: " + pivoltage);
    		}
    		if(v2==0 && i2==0){
    			System.out.println("Voltage: " + prvoltage);
    		}
     
    		//current
    		if(i2==0){
    			System.out.println("Current: " + current);
    		}
    		if(i2==0 && r2==0){
    			System.out.println("Current: " + pvcurrent);
    		}
    		if(i2==0 && v2==0){
    			System.out.println("Current: " + prcurrent);
    		}
     
    		//resistance
    		if(r2==0){
    			System.out.println("Resistance: " + resistance);
    		}  
    		if(r2==0 && i2==0){
    			System.out.println("Resistance: " + vpresistance);
    		}
    		if(r2==0 && v2==0){
    			System.out.println("Resistance: " + piresistance);
    		}
     
    		//power
    		if(p2==0){
    			System.out.println("Power: " + power);
    		}
    		if(p2==0 && v2==0){
    			System.out.println("Power: " + irpower);
    		}
    		if(p2==0 && i2==0){
    			System.out.println("Power: " + vrpower);
    		}
     
    	}
     
    }

  9. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: scanner questions

    Quote Originally Posted by Silverknight View Post
    I think the issue now lies within my logic
    Yes, it's also wrong about the point where you do calculations.

    1) Initialize double variables to 0.
    2) For each input, use nextLine() and assign to a String variable. If and only if the string is non-empty, use Double.parseDouble and assign to the double variable (otherwise it remains 0). You will not use the default 0 because in the successive code you need to re-test if a String is non-empty. As alternative approach, keep a boolean variable to state if the input value is present or not.
    3) After all inputs start tests and computations. By theory, you need exactly 2 values, otherwise it has no sense. You need to do all cases using a slightly complex set of "if" and sub-"if" (or a compound expression in a single if, but I don't recommend this).
    4) For each entity, verify if the value is present (I repeat, re-test the String or use a boolean). If the value is not present, it is to be calculated. So you need exactly 2 of the other 3 values. You need to do nested tests on the 3 values. If there is some inconsistency, tell the user and stop any further calculation.

    Repeat 4) for the other entities.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  10. The Following User Says Thank You to andbin For This Useful Post:

    Silverknight (January 21st, 2014)

Similar Threads

  1. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  2. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  3. Scanner not working?
    By ryno893 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 13th, 2012, 08:07 AM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM