Click to See Complete Forum and Search --> : Creating .txt files based off of input (beginner java)


Noappendixboy
03-26-2007, 02:57 PM
I am trying to create a program for my class that takes your input and designs a spiral and outputs the x and y coordinates to a .txt file and a graph. I'm focusing on the .txt file right now and I'm having a lot of difficulty figuring out how to get it to work. I'm very new at this and chances are there are multiple mistakes all causing me problems that I haven't even discovered yet and I might even be taking the complete wrong approach. As of right now the program compiles but isn't creating a .txt file. Any help is very much appreciated.


import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.lang.Math.*;
import java.io.*;


public class Spiral {


private int currentX;

private int currentY;

/* The PrintWriter object is used to create the output file */
private PrintWriter out;

/**
* Default constructor for the RecycleDays class. Opens the
* Recycle.txt file.
*/
public Spiral() {
try{
out = new PrintWriter (new FileWriter ("prog3spiral.txt"));
}
catch (IOException e) {
System.out.println ("Unable to open file: " + "prog3spiral.txt" + e);
}
}
/**
* Calculations to create the Spiral
*
*
*/

public void createSpiral(String[] args) {


//Sets the first user input as delta.
double delta = Double.parseDouble(args[0]);

//Sets the second user input as ratio.
double ratio = Double.parseDouble(args[1]);

//Sets the third user input as steps.
int steps = Integer.parseInt(args[2]);

//Sets the fourth user input as maxX.
int maxX = Integer.parseInt(args[3]);

//Sets the fifth user input as maxY.
int maxY = Integer.parseInt(args[4]);


double angle = 0;

double radius = 0;

int currentX = 0;

int currentY = 0;

int currentStep = 0;

double doubleX = 0;

double doubleY = 0;






while (currentStep < steps)
{
currentStep++;

if (currentX > maxX) {

}
else

if (currentY > maxY) {

}

else

if (currentStep < steps) {

doubleX = radius * Math.cos(angle);

doubleY = radius * Math.sin(angle);

angle = angle + delta;

radius = radius + (delta * ratio);

currentX = (int) doubleX;

currentY = (int) doubleY;

writeOneLine( currentX, currentY, maxX, maxY);

}

}

}


/**
*
* Format and write one line to prog3spiral.txt file
* @param month the month we are writing
* @param firstTuesday the number of the first Tuesday of the month
* @param thirdTuesday the number of the third Tuesday of the month
*/
private void writeOneLine (double currentX, double currentY, int maxX, int maxY) {

out.printf("%6.2f %6.2f\n", currentX, currentY);

}
}

potterd64
03-26-2007, 06:25 PM
Is this the whole program? Remember you need a main method to get the program running.

Add this to your Spiral class


public static void main(String[] args){
Spiral s = new Spiral();
s.createSpiral(...);
...
}