Sign in to follow this  
WheelsRCool

Need Help With a Simple Java Program

Recommended Posts

Hey all, I just started programming and am currently working on Java and C++. My question here pertains to a Java program I was writing from a Java textbook. I am right now writing the code in Notepad and compiling and running the programs from the Command Prompt window. Here is what happens after I compile the code. I type javac Ch3PoleHeight.java and it compiles, then it says, Ch3PoleHeight.java:21: cannot find symbol symbol : method create(java.io.InputStream) location: class java.util.Scanner Scanner scanner = Scanner.create(System.in); Ch3PoleHeight.java:22: getProperties() in java.lang.System cannot be applied to (java.lang.String) scanner.useDelimeter(System.getProperties("line.useDelimeter")); 2 errors Note: under the Scanner.create(System.in) is a symbol ^ under the . separating "Scanner" and "create." The ^ is also under the . separating "System" and "getProperties" of scanner.useDelimeter(System.getProperties("line.useDelimeter")) Here is the code itself of the program: /* Chapter 3 Sample Program: Estimate the Pole Height File: Ch3PoleHeight.java */ import java.text.*; import java.util.*; class Ch3PoleHeight { public static void main( String[] args ) { double height; // height of the pole double distance; // distance between points A and B double alpha; // angle measured at Point A double beta; // angle measured at Point B double alphaRad; // angle Alpha in radians double betaRad; // angle Beta in radians Scanner scanner = Scanner.create(System.in); scanner.useDelimeter(System.getProperties("line.separator")); // Get three input values System.out.print("Angle alpha (in degrees):"); alpha = scanner.nextDouble(); System.out.print("Angle beta (in degrees):"); beta = scanner.nextDouble(); System.out.print("Distance between points A and B (ft):"); distance = scanner.nextDouble(); // Compute the height of the tower alphaRad = Math.toRadians(alpha); betaRad = Math.toRadians(beta); height = ( distance * Math.sin(alphaRad) * Math.sin(betaRad) ) / Math.sqrt( Math.sin(alphaRad + betaRad) * Math.sin(alphaRad - betaRad) ); DecimalFormat df = new DecimalFormat("0.000"); System.out.println("Estimating the height of the statue" + "\n\n" + "Angle at point A (deg): " + df.format(alpha) + "\n" + "Angle at point B (deg): " + df.format(beta) + "\n" + "Distance between A and B (ft): " + df.format(distance)+ "\n" + "Estimate height (ft): " + df.format(height)); } } Note: the code won't display on here the way I actually typed it in, with indentations and such. The book didn't write everything all with no indentations, as shown. For example,I have gone through this code, but I can't spot what is wrong. And I didn't even write it really, I just copied it directly out of the book as I am only learning the very basics for now. I am pretty sure this is the identical copy from the book. Any help is appreciated, thanks.

Share this post


Link to post
Share on other sites
Well I'm not going to be much help, mostly b/c I don't know Java, however, I can offer a little advice.

1 the err you recived

Quote:
Ch3PoleHeight.java:21: cannot find symbol
symbol : method create(java.io.InputStream)


the .java:21 I beleave refers to line 21 which is:

Quote:
Scanner scanner = Scanner.create(System.in);


not entirely shure what the problem is, however I do see a space between "Scanner scanner..." not sure if this is the problem or if the space is intentional, but it could be the problem(if it is not intentional, or if the book has it that way, it could be a typo, I'd start with trying
"Scanner.scanner..." but I'm shooting in the dark). The susequent err is most likely due to the err on line 21 b/c you are accessing something that was not created.


Alternativly, if Scanner is a type, and that line is a declaration line, that you are init-ing on. In this event I'm not too sure, however, it is usualy not a good idea(in practice) to use differant cases to differentiate vars and calls. Just added advice.

edit:
After some google-ing I found that I'm am just a moron(several sites that had your simular code, with the exception of the line
Quote:
scanner.useDelimeter(System.getProperties("line.separator"));

You may try removing that line(which is also the line of the second err) b/c most applications did not have that line, I am unsure if it is necessary, and could quite possibly be understood by the compiler. Once again shooting the dark!!

You can ignore most of what I said, but you could use my thought process as an example for debuging(if you find it useful)

Not shure if thats the problem, just my guess

[Edited by - darkman738 on July 19, 2006 7:17:09 PM]

Share this post


Link to post
Share on other sites
To the first error:

There is no method named create(...) at all in Scanner, not even a non-static one.
You should be using the constructor that takes an InputStream, like this:
Scanner scanner = new Scanner(System.in);


To the second, I'm not sure what you want to do, but if you look at the API documentation you will see that there is no method named getProperties that takes a String. There is getProperties() with no argument and there is getProperty(String key).

I think you just need to read the documentation a bit more closly :)

Edit: If you are just reading from the book, and don't know where to find the documentation it's here: http://java.sun.com/j2se/1.5.0/docs/api/
That's just to j2se (Java 2 standard edition) and there are some other editions, but I myself has never used anything else and I don't know really what's in them. So, you should be alright with that.

2nd Edit: When you are programming Java, you should look up everything you need at the above mentioned site, for two reasons. 1) It's very easy to do. 2) It's the truth. If you have a book that says something different from the documentation, you can bet your life on that the documentation is right.

When I get errors like Symbol not found I always look it up in the documentation if I can't figure it out right away, there simply is no other way, unless it's your own class that you are trying to use :P

[Edited by - tufflax on July 20, 2006 7:38:46 AM]

Share this post


Link to post
Share on other sites
Alright, the first error was fixed (thanks tufflax), however, the code in the book specifically says, "Scanner scanner = Scanner.create(System.in);" Maybe this was of a previous edition of Java? This book is from a year or two ago, and I am sure that a professor of Comp. Sci from the Naval Post Graduate School would not make up code out of nowhere, so it's probably like you said, just something changed in the docs.

The second error is still occuring (ARGH!), but I will work on it today; thanx for the tip on the documentation, I was actually kind of clueless about that; every Comp. Sci major I know says, "You really need to get used to viewing the documentation," so I will follow suit.

Thanx for the information to all who replied.

Share this post


Link to post
Share on other sites
Quote:
Original post by WheelsRCool
Alright, the first error was fixed (thanks tufflax), however, the code in the book specifically says, "Scanner scanner = Scanner.create(System.in);" Maybe this was of a previous edition of Java? This book is from a year or two ago, and I am sure that a professor of Comp. Sci from the Naval Post Graduate School would not make up code out of nowhere, so it's probably like you said, just something changed in the docs.


You'd be surprised. People often don't verify code that they type up for books, and human memory is fallible. (Not to mention errors in transcription when the book is typeset/published.)

Share this post


Link to post
Share on other sites
Yeah, Scanner.create(InputStream) seems like a strange method in the first place. If you want a new scanner, you use the constructor. There is no reason why there should be a create method. Also, old methods are, as far as I know, left so old programs still compile. Could be wrong about that though, but it is still a silly method in the first place.

Share this post


Link to post
Share on other sites
Quote:
Original post by tufflax
Yeah, Scanner.create(InputStream) seems like a strange method in the first place. If you want a new scanner, you use the constructor. There is no reason why there should be a create method.

Factory pattern, pretty common in Java.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this