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

Thread: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    Hi guys, I'm very new to programming in general, and two days old in my java experience.

    I've been messing around with many guides and been creating my own programs with the guides. Right now, I thought it would be interesting to create a program solving my favorite mathematical formula -- the Quadratic formula. Ya know that one with the song!

    Anyways, for some reason I'm having a NaN outcome for my current line in the program.
    Also! I have been having a lot of problems with if- statements and strings. My last program had the same issue.

    I would love for some feedback here.

    import java.util.Scanner;
     
    public class calc_Quadratic {
    		public static void main(String[] args) {
    			Scanner keyboard = new Scanner(System.in);
     
    			String reply = null;
    			String hint = null;
    			int a = 0;
    			int b = 0;
    			int c = 0;
     
    			System.out.println("Welcome to the homemade Quadratic Formula Calculator.");
    			System.out.println("");
    			System.out.println("This program will help you solve for the possible zeroes in your specific equations.");
    			System.out.println("The quadratic formula is as follows -- ((ax^2) + bx + c)/2a");
    			System.out.println("If your equation is in the said formula, please continue and fill out the following variables with their corresponding numbers.");
     
    			System.out.println("");
    			System.out.println("");
    			System.out.println("(ax^2) [otherwise known as a x squared]: ");
    			a = keyboard.nextInt();
     
    			System.out.println("");
    			System.out.println("bx: ");
    			b = keyboard.nextInt();
     
    			System.out.println("");
    			System.out.println("c: ");
    			c = keyboard.nextInt();
     
     
    			System.out.println("");
    			System.out.println("");
    			System.out.println("Are you ready for the answer?");
    			reply = keyboard.next(); 
    			if (reply == "yes") {
    				System.out.println("First we must take the opposite of b (positive/negative): " + -b);
    			} else {
    				System.out.println("Please type in 'yes' when you ready.");
    			}
     
    			System.out.println("");
    			System.out.println("We should then take the square root of ((b^2) - 4ac): " + (Math.sqrt(Math.pow(b, 2) - 4*a*c)));		
    	}
    }




    Thanks!


  2. #2
    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: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    And what values are you feeding it for a, b, and c? Ensure that the values you've chosen result in a valid polynomial that can be solved with the quadratic equation. From there, I don't think you've completely solved the quadratic equation. You've gotten pieces of it. You might check the value of those pieces and then put them together into the answer.

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

    UhKindaAwkward (December 22nd, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    Sorry if I'm not making sense, programming is still a foreign language to me.

    If what you said was regarding the specific user input that i plugged in to get NaN, would that mean I'd have to use a loop?

    As for the program not being finished. I am aware there are other ways I could do it. However, I wanted to program it with the skills I already have. That said, I wanted to take the program step by step. And also with that, I didn't want to finish the program until I have the issue fixed.

    Also, are you aware of the problem I am having with the if statements and string variables?

  5. #4
    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: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    At your stage it might be helpful to print out the intermediate results OFTEN, so that you can see what the program is doing. I don't know what issues you're having. I'm not sure you're having any. The result NaN could be happening because a, b, and c don't result in a number that has a square root. That was the purpose of my first question. Posting a sample run might look like:
    Welcome to the homemade Quadratic Formula Calculator.
     
    This program will help you solve for the possible zeroes in your specific equations.
    The quadratic formula is as follows -- ((ax^2) + bx + c)/2a
    If your equation is in the said formula, please continue and fill out the following variables with their corresponding numbers.
     
     
    (ax^2) [otherwise known as a x squared]: 
    1
     
    bx: 
    4
     
    c: 
    4
     
     
    Are you ready for the answer?
    y
    Please type in 'yes' when you ready.
     
    We should then take the square root of ((b^2) - 4ac): 0.0
    Is that wrong?
    Also, are you aware of the problem I am having with the if statements and string variables?
    What problem?

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

    UhKindaAwkward (December 22nd, 2013)

  7. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    Yes that makes sense thank you.

    I'll have to find a way for it to work in a way that the formula is performed no matter what results. That might take some more time though. You've been cry helpful!

    As for that problem I want the user to type in yes. If they don't, they will simply be shown the message "please type yes to continue… And this will allow the user to check their answers along the way.

    However instead. No matter what I type in, the message "please type yes… will show up.

    I'm realizing that maybe it should have been a while loop, but I'm wondering if it's a common error.

    	while (reply != "yes") {
    				System.out.println("Please type in 'yes' when you ready.");
    				reply = keyboard.next();
    			}
    			System.out.println("First we must take the opposite of b (positive/negative): " + -b);

    Actually this is an error I keep seeming to have. If I type yes it still write the message that shows if reply != yes.

  8. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    I'm somewhat new to Java myself, but one possibility is that your != function isn't designed to work with Strings. Try the function "reply.equals("yes")" or the more useful "reply.equalsIgnoreCase("yes")", which will also allow "Yes" or "yes" or "YES"

  9. The Following User Says Thank You to Iantaylora For This Useful Post:

    UhKindaAwkward (December 22nd, 2013)

  10. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    I was thinking it might be something like that since I read somewhere that strings do not work without '==' but that caused an error when I wrote '!=='.

    If I can't find a not equal function, I'll work around it with this new information you've given me. Thanks a lot!

  11. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    For a not equal function, it should be as simple as !reply.equals("yes"). I believe the ! inverter works with all boolean arguments, so there's always a way to work it into an if statement.

    Also, I did some research to make sure that what I said was right, and the == is almost never the right option, since it doesn't compare the data held by the string, it actually compares the strings themselves by checking their space in memory. So if you had two strings, X and Y, which both read "Hello", == would still think they're different. It's tricky to use effectively, so it's probably best to stick with .equals. I hope that helps!

  12. The Following User Says Thank You to Iantaylora For This Useful Post:

    UhKindaAwkward (December 22nd, 2013)

  13. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    Great! I had actually trailed that right after you informed me of the rply.equals function. It worked great! Fantastic!


    As far as what you said about the ==. I'll be completely honest, I can't fully grasp the concept but it will help me in the long run when I take a programming class in college, so I have some prior knowledge. I really appreciate the concern and help!

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    Here's another look at how variables and their values can be compared.
    The == operator compares the contents of the variables.
    For primitive variables, the value is in the variable itself. The == operator compares their values as expected.
    For objects like a String, the value is in the instance of the object that is located somewhere in memory. The String variable contains the address of where in memory that object is located. Using the == operator with String variables compares the memory addresses of where the contents are stored. It does not compare the contents of the object. The equals() method compares the contents of two objects stored somewhere in memory.

    Some times you want to know if two reference variables point/refer to the same object. That is when you would use the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    UhKindaAwkward (December 22nd, 2013)

  16. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Couldn't find an appropriate home for this question. [Beginner] [Basic Mathematic formula and if-statements]

    I might actually be able to come up with an example where it would be used; the '=='
    You guys have been very helpful.

    I was wondering if someone can point me in the direction of what i should be looking into when I want to used complex numbers?

    Here's an obviously wrong code for it i have so far.
     
    double sqrt = (Math.sqrt(Math.pow(b, 2) - 4*a*c));
    			//System.out.println("We should then take the square root of ((b^2) - 4ac): " + sqrt);
    			//if (sqrt < 0) {
    			//	Math.abs(Math.sqrt(Math.pow(b, 2) - 4*a*c));
    			//}
     
    			if (sqrt < 0) {
    				System.out.println("You have entered an equation with one or more complex numbers.  i = the sqare root of -1");
    				double numImaginary = (sqrt * Math.pow(1, -2));
    				System.out.println("We should then take the square root of ((b^2) - 4ac)= " + sqrt + "i");
    			}
    			else {
    				System.out.println("We should then take the square root of ((b^2) - 4ac)= " + sqrt);
    			}




    FULL CODE

     
    import java.util.Scanner;
     
    public class calc_Quadratic {
    		public static void main(String[] args) {
    			Scanner keyboard = new Scanner(System.in);
     
    			String reply = null;
    			String hint = null;
    			double a = 0;
    			double b = 0;
    			double c = 0;
     
    			System.out.println("Welcome to the homemade Quadratic Formula Calculator.");
    			System.out.println("");
    			System.out.println("This program will help you find the possible zeroes in your specific equations.");
    			System.out.println("The quadratic formula is as follows -- ((ax^2) + bx + c)/2a");
    			System.out.println("If your equation is in the said formula, please continue and fill out the following variables with their corresponding numbers.");
     
    			System.out.println("");
    			System.out.println("");
    			System.out.println("(ax^2) [otherwise known as a x squared]: ");
    			a = keyboard.nextInt();
     
    			System.out.println("");
    			System.out.println("bx: ");
    			b = keyboard.nextInt();
     
    			System.out.println("");
    			System.out.println("c: ");
    			c = keyboard.nextInt();
     
     
    			System.out.println("");
    			System.out.println("");
    			System.out.println("Are you ready for the next step?");
    			reply = keyboard.next();
    			while (!reply.equalsIgnoreCase("yes")) {
    				System.out.println("Please type in 'yes' when you are ready.");
    				reply = keyboard.next();
    			}
     
    			System.out.println("First we must take the opposite of b (positive/negative): " + -b);
    			double oppositeB = -b;
     
     
    			System.out.println("");
    			System.out.println("Are you ready for the next step?");
    			reply = keyboard.next();
    			while (!reply.equalsIgnoreCase("yes")) {
    				System.out.println("Please type in 'yes' when you are ready.");
    				reply = keyboard.next();
    			}
     
    			double sqrt = (Math.sqrt(Math.pow(b, 2) - 4*a*c));
    			//System.out.println("We should then take the square root of ((b^2) - 4ac): " + sqrt);
    			//if (sqrt < 0) {
    			//	Math.abs(Math.sqrt(Math.pow(b, 2) - 4*a*c));
    			//}
     
    			if (sqrt < 0) {
    				System.out.println("You have entered an equation with one or more complex numbers.  i = the sqare root of -1");
    				double numImaginary = (sqrt * Math.pow(1, -2));
    				System.out.println("We should then take the square root of ((b^2) - 4ac)= " + sqrt + "i");
    			}
    			else {
    				System.out.println("We should then take the square root of ((b^2) - 4ac)= " + sqrt);
    			}
     
     
    			System.out.println("");
    			System.out.println("Are you ready for the next step?");
    			reply = keyboard.next();
    			while (!reply.equalsIgnoreCase("yes")) {
    				System.out.println("Please type in 'yes' when you are ready.");
    				reply = keyboard.next();
    			}
     
    			//...
    			double addTop = oppositeB + sqrt;  
    			double minusTop = oppositeB - sqrt;
    			//...
     
    			System.out.println("Now we will have to add our result of the first step: " + oppositeB + ", to our result from the second step: " + sqrt + ", to get: " + addTop);
    			System.out.println("We will then have to replace the addition sign to subtract our second result, " + sqrt + ", from our 1st result, " + oppositeB + ", to get: " + minusTop);
     
     
    			System.out.println("");
    			System.out.println("Are you ready for the next step?");
    			reply = keyboard.next();
    			while (!reply.equalsIgnoreCase("yes")) {
    				System.out.println("Please type in 'yes' when you are ready.");
    				reply = keyboard.next();
    			}
     
    			//...
    			double divPositive = addTop/(2*a);
    			double divNegative = minusTop/(2*a);
    			//...
     
    			System.out.println("Alright.  Next we will have to divide 2a (2 * a), " + (2 * a) + ", from the result of step 3, " + addTop + ", to get: x = " + divPositive);  
    			System.out.println("Finally we will have to divide 2a (2 * a), " + (2 * a) + ", from the result of step 4, " + minusTop  + ", to get: x= " + divNegative);  
    			System.out.println("");
    			System.out.println("");
     
     
    			if (divPositive == divNegative) {
    				System.out.println("You only have one zero.  It is: x = " + divPositive);
    			}else {
    				System.out.println( "Your zeroes are: x = " + divPositive + " and x = " + divNegative);
    			}
     
     
    			//Must add conditional statement for imaginary numbers
    			//Must add conditional statements if both x's = each other done~
    			//Add statement if only one zero
    			//Possibly edit program to have user enter correct answer in order to continue*

Similar Threads

  1. Replies: 9
    Last Post: March 18th, 2013, 05:49 PM
  2. Replies: 1
    Last Post: February 3rd, 2012, 12:00 PM
  3. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  4. Basic Beginner Help
    By SRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:27 PM