Click to See Complete Forum and Search --> : [RESOLVED] input problem


SepetPhalanx
04-05-2005, 10:59 PM
most of the errors are commented out so that the program can run correctly (without prompted input)

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

public class Sphere
{
//private String strInputH;
//private double doubleInputH;
//public static int intInputH;
//private String strInputV;
//private double doubleInputV;
//public static int intInputV;

public static void main(String args[])
{
Windows win = new Windows();
win.setSize(700, 700);
win.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e){System.exit(0);}});
win.show();
}
/*public void setInputH(int intInputH)
{
strInputH = JOptionPane.showInputDialog(
"Enter horizontal input (1-200).");
doubleInputH = Double.parseDouble(strInputH);
Math.round(doubleInputH);
intInputH = (int) doubleInputH;
}
public static int getInputH()
{
return intInputH;
}
public void setInputV(int intInputV)
{
strInputV = JOptionPane.showInputDialog(
"Enter vertical input (1-200). For best result it is recommended that " +
"the\nvertical input be the same as the horizontal input, but you are " +
"free\nto experiment. If you want the vertical input to be the same as " +
"the\nhorizontal retype the number you just did or type 'same' " +
"(all lowercase) below.");
if (strInputV == "same")
intInputV = getInputH();
else
{
doubleInputV = Double.parseDouble(strInputV);
Math.round(doubleInputV);
intInputV = (int) doubleInputV;
}
}
public static int getInputV()
{
return intInputV;
}*/
}


class Windows extends Frame
{
public void paint(Graphics s)
{
sphere(s);
}
public void sphere(Graphics s)
{
int horizontalInputOne = 15;//Sphere.getInputH(); //horizontal input; shouldn't have to be altered
int verticalInputOne = 15;//Sphere.getInputV(); //vertical input; shouldn't have to be altered
int horizontalA = 50; //x axis location
int horizontalB = 50; //y axis location
int horizontalC = 600; //width of "inital circle"
int horizontalD = 600; //height of "inital circle"
int verticalA = horizontalA; //do not alter
int verticalB = horizontalB; //do not alter
int verticalC = horizontalC; //do not alter
int verticalD = horizontalD; //do not alter
//yes, there's two "inital circles" but one's directly on top of the other so it doesn't matter
int horizontalInputTwo = horizontalInputOne * 2; //math; do not alter
int verticalInputTwo = verticalInputOne * 2; //math; do not alter

if (horizontalInputOne > 0 && verticalInputOne > 0 && horizontalInputOne < 201 && verticalInputOne < 201)
//acceptable inputs are 1-200 for horizontal and 1-200 for vertical
{
do //horizontal
{
s.drawOval(horizontalA, horizontalB, horizontalC, horizontalD);
horizontalA += horizontalInputOne; //x axis location of next circle; y axis shouldn't change
horizontalC -= horizontalInputTwo; //width of next circle; height shouldn't change
}
while (horizontalC >= 0); //ends the loop when height is zero
do //vertical
{
s.drawOval(verticalA, verticalB, verticalC, verticalD);
verticalB += verticalInputOne; //y axis location of next circle; x axis shouldn't change
verticalD -= verticalInputTwo; //width of next circle; height shouldn't change
}
while (verticalD >= 0); //ends the loop when width is zero
}
/*JOptionPane.showMessageDialog(null, "Inputs are...\nHorizontal: " + horizontalInputOne + "\nVertical: " + verticalInputOne, "Inputs", JOptionPane.INFORMATION_MESSAGE);
if (horizontalInputOne <= 0) //minimum horizontal input is 1
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input must be greater than zero.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (horizontalInputOne >= 201) //maximum horizontal input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input must be less than or equal to 200", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (verticalInputOne <= 0) //minimum vertical input is 1
JOptionPane.showMessageDialog(null, "Error:\nVertical input must be greater than zero.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (verticalInputOne >= 201) //maximum vertical input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nVertical input must be less than or equal to 200", "ERROR", JOptionPane.INFORMATION_MESSAGE);
*/
}
}

buntine
04-06-2005, 12:33 AM
Can you elaborate? Whats actually happening?

This if (strInputV == "same") should be if (strInputV.equals("same")) .

Regards.

SepetPhalanx
04-06-2005, 08:51 AM
well...
what's commented out is script to bring up a alert-type window with a prompt and then and input for the distance between the grid lines.
try running the program as it is and then change the horizontalInputOne and verticalInputOne variables and look at the difference
ex:
currently-
horizontalInputOne = 15;
verticalInputOne = 15;
change-
horizontalInputOne = 30;
verticalInputOne = 30;

All that is fine, my problem occurs when I try to run it with a BufferReader with the variables Sphere.getInputH() and Sphere.getInputG from the above Sphere.class file (above Window.class).
I haven't messed with it in a while but if you take out the comments that are actual code, including...
-commenting out int horizontalInputOne and replacing it with (located in the comment next to it) Spere.getInputH
-commenting out int verticalInputOne and replacing it with (located in the comment next to it) Spere.getInputV

the two specified above is what actually allows for a prompted input rather than and actually changing the code and then recompiling...
My ride to school is here, I can elaborate more once I get my hands on a computer at school...

SepetPhalanx
04-06-2005, 12:35 PM
for simplicity, this is the code for the program when it runs with the prompted input error(s)

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

public class Sphere
{
private String strInputH;
private double doubleInputH;
public static int intInputH;
private String strInputV;
private double doubleInputV;
public static int intInputV;

public static void main(String args[])
{
Windows win = new Windows();
win.setSize(700, 700);
win.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e){System.exit(0);}});
win.show();
}
public void setInputH(int intInputH)
{
strInputH = JOptionPane.showInputDialog(
"Enter horizontal input (1-200).");
doubleInputH = Double.parseDouble(strInputH);
Math.round(doubleInputH);
intInputH = (int) doubleInputH;
}
public static int getInputH()
{
return intInputH;
}
public void setInputV(int intInputV)
{
strInputV = JOptionPane.showInputDialog(
"Enter vertical input (1-200). For best result it is recommended that " +
"the\nvertical input be the same as the horizontal input, but you are " +
"free\nto experiment. If you want the vertical input to be the same as " +
"the\nhorizontal retype the number you just did or type 'same' " +
"(all lowercase) below.");
if (strInputV == "same")
intInputV = getInputH();
else
{
doubleInputV = Double.parseDouble(strInputV);
Math.round(doubleInputV);
intInputV = (int) doubleInputV;
}
}
public static int getInputV()
{
return intInputV;
}
}


class Windows extends Frame
{
public void paint(Graphics s)
{
sphere(s);
}
public void sphere(Graphics s)
{
//int horizontalInputOne = 15; //the following line is the prompted horizontal input
Sphere.getInputH(); //horizontal input; shouldn't have to be altered
//int verticalInputOne = 15; //following line is the prompted vertical input
Sphere.getInputV(); //vertical input; shouldn't have to be altered
int horizontalA = 50; //x axis location
int horizontalB = 50; //y axis location
int horizontalC = 600; //width of "inital circle"
int horizontalD = 600; //height of "inital circle"
int verticalA = horizontalA; //do not alter
int verticalB = horizontalB; //do not alter
int verticalC = horizontalC; //do not alter
int verticalD = horizontalD; //do not alter
//yes, there's two "inital circles" but one's directly on top of the other so it doesn't matter
int horizontalInputTwo = horizontalInputOne * 2; //math; do not alter
int verticalInputTwo = verticalInputOne * 2; //math; do not alter

if (horizontalInputOne > 0 && verticalInputOne > 0 && horizontalInputOne < 201 && verticalInputOne < 201)
//acceptable inputs are 1-200 for horizontal and 1-200 for vertical
{
do //horizontal
{
s.drawOval(horizontalA, horizontalB, horizontalC, horizontalD);
horizontalA += horizontalInputOne; //x axis location of next circle; y axis shouldn't change
horizontalC -= horizontalInputTwo; //width of next circle; height shouldn't change
}
while (horizontalC >= 0); //ends the loop when height is zero
do //vertical
{
s.drawOval(verticalA, verticalB, verticalC, verticalD);
verticalB += verticalInputOne; //y axis location of next circle; x axis shouldn't change
verticalD -= verticalInputTwo; //width of next circle; height shouldn't change
}
while (verticalD >= 0); //ends the loop when width is zero
}
JOptionPane.showMessageDialog(null, "Inputs are...\nHorizontal: " + horizontalInputOne + "\nVertical: " + verticalInputOne, "Inputs", JOptionPane.INFORMATION_MESSAGE);
if (horizontalInputOne <= 0) //minimum horizontal input is 1
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input must be greater than zero.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (horizontalInputOne >= 201) //maximum horizontal input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input must be less than or equal to 200", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (verticalInputOne <= 0) //minimum vertical input is 1
JOptionPane.showMessageDialog(null, "Error:\nVertical input must be greater than zero.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
if (verticalInputOne >= 201) //maximum vertical input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nVertical input must be less than or equal to 200", "ERROR", JOptionPane.INFORMATION_MESSAGE);

}
}

It compiles just fine so I guess it's a runtime error (?) except it runs fine with the exception that it doesn't do what I want it to do. You might have to run it yourself to see what I mean. If not then I can try and describe what it's doing compared to what I want it to do, if you request that I do so.

Khalid Ali
04-06-2005, 01:28 PM
I think it will save time for all of us, if you explained what is it you expect and what does your program do wrong

SepetPhalanx
04-06-2005, 01:35 PM
alright.
Well when it runs it brings up the window screen to draw on and the first prompt window as planned but after that (even with an acceptable input) the same prompt window displays again. If it's an unacceptable input the error messages don't display and instead it brings up the same window.
Eventually the planned second input window appears and goes through the same routine.
Last time I check the error messages don't display and I don't think it ever draws anything.

Khalid Ali
04-06-2005, 05:08 PM
ok here is what I understood.
1. you want to get user input at startup
2. use this input the draw the sphere.
let me know if my understanding is correct(because if it is then there are few logical + syntactical changes you need to make and I'll guide you thru)

SepetPhalanx
04-06-2005, 05:33 PM
I wouldn't be surprised if its a logic error somewhere but for the most part what you've said is correct

Khalid Ali
04-06-2005, 06:59 PM
I will take a look at it again tomorrow morning and respond to ya...(unless some one else finds the time for it)

SepetPhalanx
04-06-2005, 08:50 PM
alright, thanks.

Khalid Ali
04-08-2005, 09:36 AM
Here is the example, I have taken the liberty to make some core changes to your code.
take a look at it, try to uderstand it and if you have any questions post back..:-)

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Sphere {
private String strInputH;
private double doubleInputH;
public static int intInputH;
private String strInputV;
private double doubleInputV;
public static int intInputV;

/**
*
*/
public Sphere(){
setInputH(0);
setInputV(0);

Windows win = new Windows();
win.setSize(700, 700);
win.addWindowListener(new WindowAdapter() {
public void
windowClosing(WindowEvent e) {
System.exit(0);
}
});
win.show();
}

public static void main(String args[]) {
new Sphere();
}

/**
*
* @param intInputH
*/
public void setInputH(int intInputH) {
do{
strInputH = JOptionPane.showInputDialog("Enter horizontal input (1-200).");
doubleInputH = Double.parseDouble(strInputH);
Math.round(doubleInputH);
this.intInputH = (int) doubleInputH;
}while(this.intInputH<=0 || this.intInputH>200);
}

/**
*
* @return int
*/
public int getInputH() {
return intInputH;
}

/**
*
* @param intInputV
*/
public void setInputV(int intInputV) {
do{
strInputV = JOptionPane.showInputDialog("Enter vertical input (1-200)");
doubleInputV = Double.parseDouble(strInputV);
Math.round(doubleInputV);
this.intInputV = (int) doubleInputV;
}while(this.intInputV<=0 || this.intInputV>200);

}

/**
*
* @return int
*/
public int getInputV() {
return intInputV;
}

/**
*
*/
class Windows extends Frame {
public void paint(Graphics s) {
sphere(s);
}

/**
* Draws sphere
* @param s
*/
public void sphere(Graphics s) {
int horizontalInputOne = getInputH();
int verticalInputOne = getInputV();

int horizontalA = 50; //x axis location
int horizontalB = 50; //y axis location
int horizontalC = 600; //width of "inital circle"
int horizontalD = 600; //height of "inital circle"
int verticalA = horizontalA; //do not alter
int verticalB = horizontalB; //do not alter
int verticalC = horizontalC; //do not alter
int verticalD = horizontalD; //do not alter
//yes, there's two "inital circles" but one's directly on top of the other so it doesn't matter
int horizontalInputTwo = horizontalInputOne * 2; //math; do not alter
int verticalInputTwo = verticalInputOne * 2; //math; do not alter

if (horizontalInputOne > 0 && verticalInputOne > 0 && horizontalInputOne < 201 && verticalInputOne < 201) {
//acceptable inputs are 1-200 for horizontal and 1-200 for vertical
do { //horizontal
s.drawOval(horizontalA, horizontalB, horizontalC, horizontalD);
horizontalA += horizontalInputOne; //x axis location of next circle; y axis shouldn't change
horizontalC -= horizontalInputTwo; //width of next circle; height shouldn't change
} while (horizontalC >= 0); //ends the loop when height is zero
do { //vertical
s.drawOval(verticalA, verticalB, verticalC, verticalD);
verticalB += verticalInputOne; //y axis location of next circle; x axis shouldn't change
verticalD -= verticalInputTwo; //width of next circle; height shouldn't change
} while (verticalD >= 0); //ends the loop when width is zero
}
}
}

}

SepetPhalanx
04-08-2005, 10:07 PM
Thanks a Bunch.
Here's what i looks like after i tweeked your suggestion :)

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
*AUTHOR: SepetPhalanx
*/

public class Sphere
{
private String strInputH;
private double doubleInputH;
public static int intInputH;
private String strInputV;
private double doubleInputV;
public static int intInputV;

public Sphere()
{
setInputH(0);
setInputV(0);

Windows win = new Windows();
win.setSize(700, 700);
win.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
win.show();
}

public static void main(String args[])
{
new Sphere();
}

public void setInputH(int intInputH)
{
do
{
strInputH = JOptionPane.showInputDialog("Enter horizontal input (1-200).");
doubleInputH = Double.parseDouble(strInputH);
Math.round(doubleInputH);
this.intInputH = (int) doubleInputH;
if (this.intInputH <= 0) //minimum horizontal input is 1
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input is " + this.intInputH + "\nHorizontal input must be greater than or equal to 1.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
if (this.intInputH >= 201) //maximum horizontal input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nHorizontal input is " + this.intInputH + "\nHorizontal input must be less than or equal to 200.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
}
while(this.intInputH < 0 || this.intInputH > 201);
}

public int getInputH()
{
return intInputH;
}

public void setInputV(int intInputV)
{
do
{
strInputV = JOptionPane.showInputDialog("Enter vertical input (1-200).");
doubleInputV = Double.parseDouble(strInputV);
Math.round(doubleInputV);
this.intInputV = (int) doubleInputV;
if (this.intInputV <= 0) //minimum vertical input is 1
JOptionPane.showMessageDialog(null, "Error:\nVertical input is " + this.intInputV + "\nVertical input must be greater than or equal to 1.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
if (this.intInputV >= 201) //maximum vertical input is 600 but 200-600 are about the same on the output screen
JOptionPane.showMessageDialog(null, "Error:\nVertical input is " + this.intInputV + "\nVertical input must be less than or equal to 200.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
}
while(this.intInputV < 0 || this.intInputV > 201);
}

public int getInputV()
{
return intInputV;
}

class Windows extends Frame
{
public void paint(Graphics s)
{
sphere(s);
}

public void sphere(Graphics s)
{
int horizontalInputOne = getInputH();
int verticalInputOne = getInputV();
int horizontalA = 50; //x axis location
int horizontalB = 50; //y axis location
int horizontalC = 600; //width of "inital circle"
int horizontalD = 600; //height of "inital circle"
int verticalA = horizontalA; //do not alter
int verticalB = horizontalB; //do not alter
int verticalC = horizontalC; //do not alter
int verticalD = horizontalD; //do not alter
//yes, there's two "inital circles" but one's directly on top of the other so it doesn't matter
int horizontalInputTwo = horizontalInputOne * 2; //math; do not alter
int verticalInputTwo = verticalInputOne * 2; //math; do not alter

if (horizontalInputOne >= 1 && horizontalInputOne <= 200 && verticalInputOne >= 1 && verticalInputOne <= 200)
//acceptable inputs are 1-200 for horizontal and 1-200 for vertical
{
do //horizontal
{
s.drawOval(horizontalA, horizontalB, horizontalC, horizontalD);
horizontalA += horizontalInputOne; //x axis location of next circle; y axis shouldn't change
horizontalC -= horizontalInputTwo; //height of next circle; width shouldn't change
}
while (horizontalC >= 0); //ends the loop when height is zero
do //vertical
{
s.drawOval(verticalA, verticalB, verticalC, verticalD);
verticalB += verticalInputOne; //y axis location of next circle; x axis shouldn't change
verticalD -= verticalInputTwo; //width of next circle; height shouldn't change
}
while (verticalD >= 0); //ends the loop when width is zero
}
}
}
}

Khalid Ali
04-08-2005, 11:10 PM
you are welcome..glad to be of any help...