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: Determining Linear Approximations

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default Determining Linear Approximations

    I'm trying to code a program that will read a set of data poitns ( x & y) from a text file and then perform a Linear Least Squares Analysis to determine the a0 and a1 of the linear regression line, as well as the Square Error value.

    this is what I have so far without any of the equations but some of it doesnt seem to make sense. Can anyone let me know how it looks so far?

    import java.io.*;
    import java.util.Scanner;
    public class FileScannerTest
    // Since the could be an error in obtain a path to the input file,
    // the "throws FileNotFoundException" clause must be added.
    public static void main(String[ ] args) throws FileNotFoundException
    Scanner scan = new Scanner (System.in); 
    System.out.println ("Enter x value (0 to terminate): ");
    int input = scan.nextInt(); // Variable for user input.
    //method to check if numeric or not.
    if ("-2324.00".matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
    		 }else {
    			System.out.println("Value is NOT numeric!");
    // arrays to hold the values for x and y
    String[] x = new String[25];
    double sum = Double.parseDouble(x[1]);
    for (int i = 0; i < x.length; i++) { 
        sum += x[i];
    String[] y = new String[25];
    // Declare an Employee "counter" and initialize it to zero.
    int count = 0;
    // Declares the file path to Assignment5_data
    String currentDir = System.getProperty("user.dir");
    String fileName = currentDir + "\\Assignment5_data.txt";
    // Set up the input file, the scanner file stream, and the data string delimiter.
    // In this case, the delimiter is set at one space, which is what separates the two data points
    File inFile = new File(fileName);
    Scanner fileScanner = new Scanner(inFile);
    String delimiter = " ";
    // Declare a variable to hold each line of file input.
    String line;
    //Use fileScanner to read each line.
    while ( fileScanner.hasNextLine() )
    // Read each line of text and split it up based upon the delimiter.
    // The x string is placed in temp[0],
    // the y string in temp[1],
    line = fileScanner.nextLine();
    String [] temp = line.split(delimiter);
    // and increment the value of count.
    x [count] = temp[0];
    y [count] = temp[1];
    // Close the file stream.
    for (int index = 0; index < count; index++)
    System.out.println(x[index] );
    System.out.println(y[index] );
    sum += Integer.parseInt(y [index]);
    Last edited by helloworld922; March 13th, 2011 at 09:38 AM.

  2. #2
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default Re: Determining Linear Approximations

    You probably could have gotten an answer in the Programming Assignment 5 Questions Forum on moodle. ;D

Similar Threads

  1. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 06:32 AM