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

Thread: A couple of issues with my quadratic equation solver.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default A couple of issues with my quadratic equation solver.

    import java.util.Scanner;
    public class Andregradsformel {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		Double a;
    		Double b;
    		Double c;
    		System.out.println("This program solves quadratic equations.");
    		System.out.println("Use integers.");
    		System.out.println("Enter a: ");
    		a = in.nextDouble();
    		System.out.println("Enter b: ");
    		b = in.nextDouble();
    		System.out.println("Enter c: ");
    		c = in.nextDouble();
    		System.out.println("First solution:");
    		System.out.println((-b+Math.sqrt(b*b-4*a*c))/2*a);
    		System.out.println("Second solution:");
    		System.out.println((-b-Math.sqrt(b*b-4*a*c))/2*a);
     
    	}
     
    }

    While it works, I have two issues with it:

    I have come to understand that one can use decimal numbers while working with doubles. How come my program does not accept that as the user input?

    Furthermore, once the program is finished, I want the program to run again. I have looked up some tutorials on loops, but I do not understand how I would go about implementing it. Thanks.

    EDIT: Tried changing from Double to Float. Still works with integers, but decimalnumbers are still defect.

    EDIT2: For some reason, user-input has to entered as "3,4", not "3.4". Solved.


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: A couple of issues with my quadratic equation solver.

    Quote Originally Posted by Zaphod_b View Post
    It's a Locale thing.

    What do you get if you put the following statement at the beginning of main()?
            System.out.println("Locale: " + Locale.getDefault().getDisplayName());

    If you want all of your input and output floating point numbers to use '.' decimal points instead of commas, you can try the following at the beginning of the main() function:
        public static void main(String [] args) {
     
            Locale.setDefault(Locale.US);
    .
    .
    .
    Note that, depending on your Locale, this can affect lots of other things, not just the "decimal point."


    If you want the input to use the '.' instead of ',' for the "decimal point" but have the output print "normally" for your Locale, you can try the following:
        public static void main(String [] args) {
     
            Scanner in = new Scanner(System.in);
            in.useLocale(Locale.US);

    References:

    Locale Class for Locale.getDefault(), getDisplayName(), setDefault()

    and

    Scanner Class for useLocale()



    Cheers!

    Z
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Locale cannot be resolved to a variable

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: A couple of issues with my quadratic equation solver.

    Quote Originally Posted by EatMyBible View Post
    ...
    EDIT2: For some reason, user-input has to entered as "3,4", not "3.4". Solved.
    It's a Locale thing.

    What do you get if you put the following statement at the beginning of main()?
            System.out.println("Locale: " + Locale.getDefault().getDisplayName());

    If you want all of your input and output floating point numbers to use '.' decimal points instead of commas, you can try the following at the beginning of the main() function:
        public static void main(String [] args) {
     
            Locale.setDefault(Locale.US);
    .
    .
    .
    Note that, depending on your Locale, this can affect lots of other things, not just the "decimal point."


    If you want the input to use the '.' instead of ',' for the "decimal point" but have the output print "normally" for your Locale, you can try the following:
        public static void main(String [] args) {
     
            Scanner in = new Scanner(System.in);
            in.useLocale(Locale.US);

    References:

    Locale Class for Locale.getDefault(), getDisplayName(), setDefault()

    and

    Scanner Class for useLocale()



    Quote Originally Posted by EatMyBible View Post
    ...once the program is finished, I want the program to run again....
    One way is to make an "infinite loop." Something like this:
            System.out.println("This program solves quadratic equations.");
            while(true) {
                System.out.print("Enter a: ");
                Double a = in.nextDouble();
    .
    .        // Read other inputs and calculate and print outputs
    .
    .
                System.out.println("\nNext problem:");
            }

    The code will run until you enter something other than a numerical quantity for a coefficient, in which case it will unceremoniously crash with an InputMismatchException.

    You can catch the exception and use that as a condition for breaking out of the loop, or you could make a special case of user inputs (all zeros, for example) that would break out of the loop so that the program can terminate more gracefully.

    Maybe something like:
        public static void main(String [] args) {
    .
    .
    .
            System.out.println("This program solves quadratic equations.");
            boolean fini = false;
            while(!fini) {
    .
    .        // Code to get values of a, b, and c
    .
     
                System.out.printf("\nYou entered %f %f %f\n\n", a, b, c); // For debugging: Show what the program is seeing
                if (/* Code to test value of a and b and c to see if they are all equal to zero )*/ {
                    fini = true;
                }
                else {
                    // Code to calculate and print output values.
                } // End if if..else.. statement for all zeros
            } // End of if(!fini) loop
            System.out.println("Goodbye for now.");


    Cheers!

    Z

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: A couple of issues with my quadratic equation solver.

    Quote Originally Posted by EatMyBible View Post
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Locale cannot be resolved to a variable
    Did you read the Locale Class reference that I linked?

    You have to import java.util.Locale to use it.

    In fact, instead of importing java.util.Scanner and java.util.Locale separately, the way this program uses things, you can just do this:
    import java.util.*;
    public class Whatever {

    Cheers!

    Z

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    EatMyBible (January 26th, 2013)

Similar Threads

  1. A couple of questions.
    By Sylis in forum Java Theory & Questions
    Replies: 0
    Last Post: October 29th, 2012, 08:32 AM
  2. Quadratic Probing
    By Herah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2012, 01:12 PM
  3. quadratic equation solver help!
    By overlord in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2011, 11:39 AM
  4. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 07:27 PM
  5. help!! quadratic program
    By dscrudato21xo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 18th, 2009, 05:13 PM