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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic Java Program Help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Thanked 0 Times in 0 Posts

    Default Basic Java Program Help

    Hello, I really need some help on some Java programming homework that is due tonight. Here is the homework:

    Your task in this homework is to compute the roots of a quadratic, using the quadratic formula. Your program should read in three double values, the coefficients of the x-squared term, the linear term and the constant term respectively. These values will be separated by blanks in the test input. It should compute the two real roots and display them, one to a line. The first root displayed should be the one where you add the square root of the discriminant. You may assume that the quadratics tested will all have real roots. These roots should be displayed to exactly 4 places past the decimal point, with a leading zero shown before the decimal point if it is between -1.0 and 1.0. Your program should display a single blank line as a prompt to make it possible to type in numbers.

    I think I understand how to do this all except for the numbers. All three numbers will be written on the same line separated by a space. So, I think that I should use blankLoc. I understand how to use this for the first blank, but what is the code that I should use to get the second and the third numbers. Any help with this program will be greatly appreciated.

    Here is my code so far:
    public class Quadratic
      public static void main(String[] args)
          //Declare and Initialize Scanner
          Scanner scan = new Scanner(in);
          //Read in Values
          double values = scan.nextDouble();
          //Separate Values
          double blankLoc = values.indexOf(' ');
          double a = values.substring(0, blankLoc);
          double b = values.substring(blankLoc, blankLoc);
          double c = values.substring(blankLoc
          //Discriminant: (b*b) - (4*a*c)+
          //Display Answers
    I am confused where the double b and double c variables are being separated from the input. Please Help.

    Thank You,
    Last edited by Freaky Chris; October 6th, 2009 at 04:02 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Basic Java Program Help

    Do you have to read in the entire formula? Or can you ask the user for separate values? The latter is going to be much much easier.

    public static void main(String[] args)
         Scanner scan = new Scanner(System.in);
         System.out.println("Enter a: ");
         double a = scan.nextDouble();
         System.out.println("Enter b: ");
         double b = scan.nextDouble();
         System.out.println(Enter c: ");
         double c = scan.nextDouble();
         // compute quadratic ...

    If you must extract all the numbers from a single string, then you can still use the Scanner so long as they are separated somehow (usually spaces), and there is nothing else in the string.

    String values = "1.0 2.3 4.5"; // a = 1, b = 2.3, c = 4.5
    Scanner reader = new Scanner(values);
    double a = values.nextDouble();
    double b = values.nextDouble();
    double c = values.nextDouble();

    However, if the given string is like this:
    "1.0X^2 + 3.5X + 2 = 0"
    , you'll have an extremely hard time extracting the numbers you want. It's not impossible, but I doubt that's what your teacher wants from you. If you really want to know, try doing a google search on regular expressions.

Similar Threads

  1. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  2. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM
  3. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM
  4. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 06:52 AM
  5. help us in eclipse basic codes
    By bil_Imma in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2009, 06:02 PM