Web Hosting Talk







View Full Version : An Apparrently Really Badly Made Java Script


Falco1199
03-04-2003, 08:41 PM
Here's my code... just a simple practice thing.

import java.io.*;

public class Rectangle
{

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

public int height, width;

public Rectangle()
{
height = 5;
width = 5;
}
public Rectangle(int _height, int _width)
{
height = _height;
width = _width;
}

public static void main(String[] args)
{
if(args.length >= 2)
Rectangle rect = new Rectangle(args[0], args[1]);
else
Rectangle rect = new Rectangle();
rect.run;
}

public void changeHeight() throws Exception
{
System.out.print("Enter New Hieght: ");
String newH = br.readLine();
System.out.println("");
try
{
Integer.parseInt(newH);
}
catch (Exception e)
{
System.out.println("That's not an integer!");
}
height = newH;
System.out.println("The height has been changed to " + height + ".");
}

public void changeWidth() throws Exception
{
System.out.print("Enter New Width: "); // line 50
String newW = br.readLine();
System.out.println("");
try
{
Integer.parseInt(newW);
}
catch (Exception e)
{
System.out.println("That's not an integer!");
}
width = newW;
System.out.println("The height has been changed to " + width + ".");
}

public void drawRect() throws Exception
{
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
System.out.println("*");
}
}
}

public void run() throws Exception
{
private boolean exit = false;

while(!exit)
{
System.out.println("");
System.out.println("Menu:");
System.out.println("[1] Draw Current Rectangle");
System.out.println("[2] Change Height");
System.out.println("[3] Change Width");
System.out.println("[4] Exit Program");
System.out.print("Enter your choice: ")
String input = br.readLine();
System.out.println("");
if(input.equals("1"))
rect.drawRect();
else if(input.equals("2"))
rect.changeWidth();
else if(input.equals("3"))
rect.changeHeight();
else if(input.equals("4"))
{
exit = true;
System.out.print("Exiting Program..."); // line 100
}
}
}
}

I can't copy errors from a command prompt, but does anyone see anything I should fix immediately? If not, I can always just manually copy them, but I'd rather not. Thanks, if anyone can... there were 18 errors by the way. :o

Falco1199
03-05-2003, 09:01 AM
OK... here are some of the errors:

Rectangle.java:25: not a statement (pointer under the R in the first Rectangle)

Rectangle.java:25: semicolon expected (pointer under the r in rect)

These two errors are also in line 27 (same pointers, basically).

Rectangle.java:28: not a statement (pointer in the period after rect)

Rectangle.java:78: illegal start of expression (pointer under p in private boolean expression)

And then some more... Can anyone help me with these?

CJ.Hammond
03-05-2003, 09:15 AM
Erm, well, I'm not too good at OO languages, but wouldn't you need a forward declaration of Rectangle to do a

Rectangle rect = new Rectangle()

within a class method? As long as the class definition isn't complete, it is still "unknown" by the compiler (just a thought though).

For another, does Java do automatic type conversions from String args[0] to int _height ?

Yet, from the error message, I'd say that the word Rectangle isn't recognized as a class' name as long as the class' endind '}' wasn't seen.

In C++ you could use a header declaration and a code definition within two parts, like

class Rectangle {
(... NEW/DESTROY here)
Rectangle main(...);
}

Rectangle Rectangle::main(...) {
(code here)
}

Yet, I don't know Java at all, so what the heck am I telling you ;-)

/jochen

sylow
03-05-2003, 09:52 AM
you dont know yet the difference between java and javascript

probonic
03-05-2003, 10:11 AM
Well I have ironed out the errors in the code. It appears to be working ok, with the exception of the actual drawing routine (which I presume you knew wasn't right :) ).

I have put comments in to say what was wrong. If you have any other questions just ask :) .

Falco1199
03-05-2003, 04:36 PM
sylow: You can write a script in java. What I have is a Java Script, IE, a script in Java. What you're talking about probably wouldn't have the space in between Java and Script; it would just be a Javascript. Your post is complete spam, btw. :)

Thank you probonic. Yes, I know the drawing system doesn't work... yet. :D

krumms
03-06-2003, 07:31 AM
Java is a programming language.

Calling it a Java Script as opposed to a Java program is misleading for very obvious reasons.

Your program is a program, not a script.

Don't flame him for pointing out your confusing, self-invented, generally bad terminology.