Although I'm a novice in Java I am to make a pool simulator in java with the ability to handle any arbitrary number of balls (i have created a Ball object, balls will be stored in the Ball array called balls). All the parameters are to be read in from an input file. I wrote some code hoping it would read in the data and create the balls array.
Unfortunately all I get is an exception, I have no clue why. Please tell me what's wrong and how to fix it.
The input file is built as shown, if more balls are required more of the input for ball blocks are to be added...
Here is my code:
*Note that Vector is my self-written 2d vector class, not the usual java class, I won't be needing it in this simulation.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Input {

    public static void main(String[] args) throws IOException, FileNotFoundException {
	/// initializing general parameters
	double tablewidth = 0;
	double tableheight = 0;
	double  pocketnumber = 0;
	double  pocketradius = 0;
	double ballnumber = 0;

	/// initializing ball parameters

	double ballXpos = 0;
	double ballYpos = 0;
	double ballVmag = 0;
	double ballVangle = 0;
	double ballmass = 0;
	double ballcolour = 0;
	double ballradius = 0;
	double ballmju = 0;
	// seting up file reader to read in parameters
        FileReader inputfile = new FileReader("input.txt");

        BufferedReader in = new BufferedReader(inputfile);
        String line;
	///reading in parameters
	    while ((line = in.readLine()) != null) {
		if (line.startsWith("Width of pool table")){
		    tablewidth = Double.valueOf(line.substring(20)).doubleValue();

		if (line.startsWith("Height of pool table")){
		    tableheight = Double.valueOf(line.substring(21)).doubleValue();
		if (line.startsWith("Number of pockets")){
		    pocketnumber = Double.valueOf(line.substring(18)).doubleValue();
		if (line.startsWith("Radius of pockets")){ 
		    pocketradius = Double.valueOf(line.substring(18)).doubleValue();
		if (line.startsWith("Number of balls")){
		    ballnumber = Double.valueOf(line.substring(16)).doubleValue();
		///create array of balls for the specified number of balls
		Ball balls[] = new Ball[(int)ballnumber]; 
		double VelX, VelY;

		// the for loop will fill the array of balls with parameters.
		for(int i=0; i < ballnumber; i++){

		    if (line.startsWith("Initial x position")){
			ballXpos = Double.valueOf(line.substring(19)).doubleValue();
		    if (line.startsWith("Initial y position")){
			ballYpos = Double.valueOf(line.substring(19)).doubleValue();
		    Vector ballposition = new Vector (ballXpos, ballYpos);

		    if (line.startsWith("Initial velocity magnitude")){
			ballVmag = Double.valueOf(line.substring(27)).doubleValue();
		    if (line.startsWith("Angle of projection")){
			ballVangle = Double.valueOf(line.substring(20)).doubleValue();
		    // calculating the components of initial velocity
		    VelY = ballVmag*Math.sin(ballVangle*(Math.PI/180.0));
		    VelX = ballVangle*Math.cos(ballVangle*(Math.PI/180.0));

		    Vector ballvelocity = new Vector(VelX, VelY);

		    if (line.startsWith("Mass")){
			ballmass = Double.valueOf(line.substring(5)).doubleValue();
		    if (line.startsWith("Radius")){
			ballradius = Double.valueOf(line.substring(7)).doubleValue();
		    if (line.startsWith("Colour")){
			ballcolour = Double.valueOf(line.substring(7)).doubleValue();
		    if (line.startsWith("Friction coefficient")){
			ballmju = Double.valueOf(line.substring(21)).doubleValue();
		    balls[i] =new Ball(ballposition, ballvelocity, ballmass, ballradius, ballcolour, ballmju);



	/// exception to deal with invalid input in the parameters file
	catch (Exception e){
	    System.out.println("invalid input");




And the input file "input.txt" starts here:


Width of pool table 600
Height of pool table 400
Number of pockets 6
Radius of pockets 15
Number of balls 2


Initial x position 306
Initial y position 48
Initial velocity magnitude 30
Angle of projection 0
Radius 13
Colour 3
Friction coefficient 0.2

Initial x position 234
Initial y position 220
Initial velocity magnitude 35
Angle of projection 30
Mass 0.5
Radius 13
Colour 5
Friction coefficient 0

8 Years
Discussion Span
Last Post by BestJewSinceJC

Undoubtedly it's because you are attempting to read the file incorrectly (for example, you're using the String class' substring method, when you should be reading in a double). But you should use print statements and a catch statement to print out your exceptions/errors.

ballXpos = Double.valueOf(line.substring(19)).doubleValue();

^ doesn't work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.